AdminLayout.vue 7.1 KB
Newer Older
wb-ct393452's avatar
wb-ct393452 committed
1 2 3
<template>
    <a-layout :class="['admin-layout', 'beauty-scroll']">
        <drawer v-if="isMobile" v-model="drawerOpen">
水落(YangLei)'s avatar
水落(YangLei) committed
4 5 6 7 8 9 10
            <side-menu
                :theme="theme.mode"
                :menuData="menuData"
                :collapsed="false"
                :collapsible="false"
                @menuSelect="onMenuSelect"
            />
wb-ct393452's avatar
wb-ct393452 committed
11 12
        </drawer>

水落(YangLei)'s avatar
水落(YangLei) committed
13 14 15 16 17 18 19 20
        <side-menu
            :class="[fixedSideBar ? 'fixed-side' : '']"
            :theme="theme.mode"
            v-else-if="layout === 'side' || layout === 'mix'"
            :menuData="sideMenuData"
            :collapsed="collapsed"
            :collapsible="true"
        />
wb-ct393452's avatar
wb-ct393452 committed
21

水落(YangLei)'s avatar
水落(YangLei) committed
22 23 24 25
        <div
            v-if="fixedSideBar && !isMobile"
            :style="`width: ${sideMenuWidth}; min-width: ${sideMenuWidth};max-width: ${sideMenuWidth};`"
            class="virtual-side"
26
        />
wb-ct393452's avatar
wb-ct393452 committed
27 28 29 30 31 32 33 34 35

        <drawer v-if="!hideSettingExtend" v-model="showSetting" placement="right">
            <div class="setting" slot="handler">
                <a-icon :type="showSetting ? 'close' : 'setting'" />
            </div>
            <setting />
        </drawer>

        <a-layout class="admin-layout-main beauty-scroll">
水落(YangLei)'s avatar
水落(YangLei) committed
36 37 38 39 40 41
            <layout-top-header
                :class="[{ 'fixed-tabs': fixedTabs, 'fixed-header': fixedHeader, 'multi-page': multiPage }]"
                :style="headerStyle"
                :menuData="headMenuData"
                :collapsed="collapsed"
                @toggleCollapse="toggleCollapse"
水落(YangLei)'s avatar
水落(YangLei) committed
42
                @menuSelect="menuSelect"
水落(YangLei)'s avatar
水落(YangLei) committed
43 44 45 46 47 48 49
            />
            <a-layout-header
                :class="[
                    'virtual-header',
                    { 'fixed-tabs': fixedTabs, 'fixed-header': fixedHeader, 'multi-page': multiPage },
                ]"
                v-show="fixedHeader"
水落(YangLei)'s avatar
水落(YangLei) committed
50 51 52
            />
            <a-layout-content class="admin-layout-content">
                <slot />
wb-ct393452's avatar
wb-ct393452 committed
53 54 55 56 57 58
            </a-layout-content>
        </a-layout>
    </a-layout>
</template>

<script>
水落(YangLei)'s avatar
水落(YangLei) committed
59
import LayoutTopHeader from '../components/header/LayoutTopHeader.vue';
wb-ct393452's avatar
wb-ct393452 committed
60
import Drawer from '@/components/tool/Drawer';
61
import SideMenu from '@/components/menu/SideMenu.vue';
wb-ct393452's avatar
wb-ct393452 committed
62 63
import Setting from '../components/setting/Setting';
import { mapState, mapMutations, mapGetters } from 'vuex';
64
import { convertListToTree, getUserInfo } from '@/utils';
wb-ct393452's avatar
wb-ct393452 committed
65 66 67 68 69

// const minHeight = window.innerHeight - 64 - 122

export default {
    name: 'AdminLayout',
70
    components: { Setting, SideMenu, Drawer, LayoutTopHeader },
wb-ct393452's avatar
wb-ct393452 committed
71 72 73 74 75 76
    data() {
        return {
            minHeight: window.innerHeight - 64 - 122,
            collapsed: false,
            showSetting: false,
            drawerOpen: false,
77
            menuData: [],
水落(YangLei)'s avatar
水落(YangLei) committed
78 79 80
            firstMenu: [],
            subMenu: [],
            menuList: [],
wb-ct393452's avatar
wb-ct393452 committed
81 82 83 84 85 86 87 88 89 90 91 92 93
        };
    },
    provide() {
        return {
            adminLayout: this,
        };
    },
    watch: {
        isMobile(val) {
            if (!val) {
                this.drawerOpen = false;
            }
        },
水落(YangLei)'s avatar
水落(YangLei) committed
94 95 96
        layout(layout) {
            this.updateSildeMenu(layout);
        },
wb-ct393452's avatar
wb-ct393452 committed
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
    },
    computed: {
        ...mapState('settingModule', [
            'isMobile',
            'theme',
            'layout',
            'footerLinks',
            'copyright',
            'fixedHeader',
            'fixedSideBar',
            'fixedTabs',
            'hideSetting',
            'multiPage',
        ]),

        hideSettingExtend() {
            if (this.hideSetting === false && process.env.NODE_ENV === 'development') {
                return false;
            }
            return true;
        },
        sideMenuWidth() {
            return this.collapsed ? '80px' : '256px';
        },
        headerStyle() {
            let width =
                this.fixedHeader && this.layout !== 'head' && !this.isMobile
                    ? `calc(100% - ${this.sideMenuWidth})`
                    : '100%';
            let position = this.fixedHeader ? 'fixed' : 'static';
            return `width: ${width}; position: ${position};`;
        },
        headMenuData() {
            const { layout, menuData, firstMenu } = this;
水落(YangLei)'s avatar
水落(YangLei) committed
131
            console.log('first', firstMenu);
wb-ct393452's avatar
wb-ct393452 committed
132 133 134 135
            return layout === 'mix' ? firstMenu : menuData;
        },
        sideMenuData() {
            const { layout, menuData, subMenu } = this;
水落(YangLei)'s avatar
水落(YangLei) committed
136
            console.log('sub', subMenu);
wb-ct393452's avatar
wb-ct393452 committed
137 138 139 140
            return layout === 'mix' ? subMenu : menuData;
        },
    },
    methods: {
水落(YangLei)'s avatar
水落(YangLei) committed
141
        ...mapMutations('settingModule', ['correctPageMinHeight']),
wb-ct393452's avatar
wb-ct393452 committed
142 143 144
        toggleCollapse() {
            this.collapsed = !this.collapsed;
        },
水落(YangLei)'s avatar
水落(YangLei) committed
145

wb-ct393452's avatar
wb-ct393452 committed
146 147 148
        onMenuSelect() {
            this.toggleCollapse();
        },
水落(YangLei)'s avatar
水落(YangLei) committed
149 150 151 152 153 154 155 156 157
        updateSildeMenu(layout) {
            if (layout === 'mix') {
                const { path } = this.$route;
                let currentMenu = this.menuList.find(i => i.menuUrl === path);
                let parentMenuId = currentMenu.parentMenuId;

                while (parentMenuId !== 0) {
                    currentMenu = this.menuList.find(i => i.menuId === parentMenuId);
                    parentMenuId = currentMenu.parentMenuId;
wb-ct393452's avatar
wb-ct393452 committed
158
                }
水落(YangLei)'s avatar
水落(YangLei) committed
159
                this.subMenu = currentMenu.children || [];
wb-ct393452's avatar
wb-ct393452 committed
160 161
            }
        },
水落(YangLei)'s avatar
水落(YangLei) committed
162 163 164 165 166 167 168 169 170 171 172 173 174 175

        menuSelect(obj) {
            console.log('切换', obj);
            // 拿到 菜单id
            const menuId = obj.key;
            const currentMenu = this.menuList.find(i => i.menuId === menuId);

            if (currentMenu.menuType === 'MENU') {
                this.subMenu = [];
                return;
            }

            this.subMenu = currentMenu.children || [];
        },
wb-ct393452's avatar
wb-ct393452 committed
176 177 178
    },
    created() {
        this.correctPageMinHeight(this.minHeight - 24);
水落(YangLei)'s avatar
水落(YangLei) committed
179 180 181
        const { menuList } = getUserInfo();
        console.log(menuList);
        const menuData = convertListToTree(menuList || [], false, true);
182
        this.menuData = menuData;
水落(YangLei)'s avatar
水落(YangLei) committed
183 184 185 186 187
        this.menuList = menuList;
        this.firstMenu = JSON.parse(JSON.stringify(menuData)).map(i => {
            delete i.children;
            return i;
        });
wb-ct393452's avatar
wb-ct393452 committed
188 189 190 191 192 193 194 195 196
    },
    beforeDestroy() {
        this.correctPageMinHeight(-this.minHeight + 24);
    },
};
</script>

<style lang="less" scoped>
.admin-layout {
水落(YangLei)'s avatar
水落(YangLei) committed
197
    height: 100%;
wb-ct393452's avatar
wb-ct393452 committed
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
    .side-menu {
        &.fixed-side {
            position: fixed;
            height: 100vh;
            left: 0;
            top: 0;
        }
    }
    .virtual-side {
        transition: all 0.2s;
    }
    .virtual-header {
        transition: all 0.2s;
        opacity: 0;
        &.fixed-tabs.multi-page:not(.fixed-header) {
            height: 0;
        }
    }
    .admin-layout-main {
水落(YangLei)'s avatar
水落(YangLei) committed
217 218 219
        display: flow-root;
        position: initial;

wb-ct393452's avatar
wb-ct393452 committed
220 221 222 223 224 225 226 227 228 229 230
        .admin-header {
            top: 0;
            right: 0;
            overflow: hidden;
            transition: all 0.2s;
            &.fixed-tabs.multi-page:not(.fixed-header) {
                height: 0;
            }
        }
    }
    .admin-layout-content {
231
        padding: 24px;
wb-ct393452's avatar
wb-ct393452 committed
232 233 234 235 236 237 238 239 240 241 242 243 244
    }
    .setting {
        background-color: @primary-color;
        color: @base-bg-color;
        border-radius: 5px 0 0 5px;
        line-height: 40px;
        font-size: 22px;
        width: 40px;
        height: 40px;
        box-shadow: -2px 0 8px @shadow-color;
    }
}
</style>