AdminLayout.vue 6.47 KB
Newer Older
wb-ct393452's avatar
wb-ct393452 committed
1 2
<template>
    <a-layout :class="['admin-layout', 'beauty-scroll']">
水落(YangLei)'s avatar
水落(YangLei) committed
3 4 5
        <side-menu
            :class="[fixedSideBar ? 'fixed-side' : '']"
            :theme="theme.mode"
6
            v-if="layout === 'side' || layout === 'mix'"
水落(YangLei)'s avatar
水落(YangLei) committed
7 8 9 10
            :menuData="sideMenuData"
            :collapsed="collapsed"
            :collapsible="true"
        />
wb-ct393452's avatar
wb-ct393452 committed
11

水落(YangLei)'s avatar
水落(YangLei) committed
12
        <div
13
            v-if="fixedSideBar"
水落(YangLei)'s avatar
水落(YangLei) committed
14 15
            :style="`width: ${sideMenuWidth}; min-width: ${sideMenuWidth};max-width: ${sideMenuWidth};`"
            class="virtual-side"
16
        />
wb-ct393452's avatar
wb-ct393452 committed
17 18 19 20 21 22 23 24 25

        <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
26 27 28 29 30 31
            <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
32
                @menuSelect="menuSelect"
水落(YangLei)'s avatar
水落(YangLei) committed
33 34 35 36 37 38 39
            />
            <a-layout-header
                :class="[
                    'virtual-header',
                    { 'fixed-tabs': fixedTabs, 'fixed-header': fixedHeader, 'multi-page': multiPage },
                ]"
                v-show="fixedHeader"
水落(YangLei)'s avatar
水落(YangLei) committed
40 41 42
            />
            <a-layout-content class="admin-layout-content">
                <slot />
wb-ct393452's avatar
wb-ct393452 committed
43 44 45 46 47 48
            </a-layout-content>
        </a-layout>
    </a-layout>
</template>

<script>
水落(YangLei)'s avatar
水落(YangLei) committed
49
import LayoutTopHeader from '../components/header/LayoutTopHeader.vue';
wb-ct393452's avatar
wb-ct393452 committed
50
import Drawer from '@/components/tool/Drawer';
51
import SideMenu from '@/components/menu/SideMenu.vue';
wb-ct393452's avatar
wb-ct393452 committed
52 53
import Setting from '../components/setting/Setting';
import { mapState, mapMutations, mapGetters } from 'vuex';
54
import { convertListToTree, getUserInfo } from '@/utils';
wb-ct393452's avatar
wb-ct393452 committed
55 56 57 58 59

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

export default {
    name: 'AdminLayout',
60
    components: { Setting, SideMenu, Drawer, LayoutTopHeader },
wb-ct393452's avatar
wb-ct393452 committed
61 62 63 64 65
    data() {
        return {
            minHeight: window.innerHeight - 64 - 122,
            collapsed: false,
            showSetting: false,
66
            menuData: [],
水落(YangLei)'s avatar
水落(YangLei) committed
67 68 69
            firstMenu: [],
            subMenu: [],
            menuList: [],
wb-ct393452's avatar
wb-ct393452 committed
70 71 72 73 74 75 76 77
        };
    },
    provide() {
        return {
            adminLayout: this,
        };
    },
    watch: {
水落(YangLei)'s avatar
水落(YangLei) committed
78 79 80
        layout(layout) {
            this.updateSildeMenu(layout);
        },
wb-ct393452's avatar
wb-ct393452 committed
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
    },
    computed: {
        ...mapState('settingModule', [
            '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 =
106
                this.fixedHeader && this.layout !== 'head' ? `calc(100% - ${this.sideMenuWidth})` : '100%';
wb-ct393452's avatar
wb-ct393452 committed
107 108 109 110 111 112 113 114 115 116 117 118 119
            let position = this.fixedHeader ? 'fixed' : 'static';
            return `width: ${width}; position: ${position};`;
        },
        headMenuData() {
            const { layout, menuData, firstMenu } = this;
            return layout === 'mix' ? firstMenu : menuData;
        },
        sideMenuData() {
            const { layout, menuData, subMenu } = this;
            return layout === 'mix' ? subMenu : menuData;
        },
    },
    methods: {
水落(YangLei)'s avatar
水落(YangLei) committed
120
        ...mapMutations('settingModule', ['correctPageMinHeight']),
wb-ct393452's avatar
wb-ct393452 committed
121 122 123
        toggleCollapse() {
            this.collapsed = !this.collapsed;
        },
水落(YangLei)'s avatar
水落(YangLei) committed
124

wb-ct393452's avatar
wb-ct393452 committed
125 126 127
        onMenuSelect() {
            this.toggleCollapse();
        },
水落(YangLei)'s avatar
水落(YangLei) committed
128 129 130
        updateSildeMenu(layout) {
            if (layout === 'mix') {
                const { path } = this.$route;
131
                let currentMenu = this.menuList.find((i) => i.menuUrl === path);
水落(YangLei)'s avatar
水落(YangLei) committed
132 133 134
                let parentMenuId = currentMenu.parentMenuId;

                while (parentMenuId !== 0) {
135
                    currentMenu = this.menuList.find((i) => i.menuId === parentMenuId);
水落(YangLei)'s avatar
水落(YangLei) committed
136
                    parentMenuId = currentMenu.parentMenuId;
wb-ct393452's avatar
wb-ct393452 committed
137
                }
水落(YangLei)'s avatar
水落(YangLei) committed
138
                this.subMenu = currentMenu.children || [];
wb-ct393452's avatar
wb-ct393452 committed
139 140
            }
        },
水落(YangLei)'s avatar
水落(YangLei) committed
141 142 143 144

        menuSelect(obj) {
            // 拿到 菜单id
            const menuId = obj.key;
145
            const currentMenu = this.menuList.find((i) => i.menuId === menuId);
水落(YangLei)'s avatar
水落(YangLei) committed
146 147 148 149 150 151 152 153

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

            this.subMenu = currentMenu.children || [];
        },
wb-ct393452's avatar
wb-ct393452 committed
154 155 156
    },
    created() {
        this.correctPageMinHeight(this.minHeight - 24);
水落(YangLei)'s avatar
水落(YangLei) committed
157 158
        const { menuList } = getUserInfo();
        const menuData = convertListToTree(menuList || [], false, true);
159
        this.menuData = menuData;
水落(YangLei)'s avatar
水落(YangLei) committed
160
        this.menuList = menuList;
161
        this.firstMenu = JSON.parse(JSON.stringify(menuData)).map((i) => {
水落(YangLei)'s avatar
水落(YangLei) committed
162 163 164
            delete i.children;
            return i;
        });
水落(YangLei)'s avatar
水落(YangLei) committed
165
        this.updateSildeMenu(this.layout);
wb-ct393452's avatar
wb-ct393452 committed
166 167 168 169 170 171 172 173 174
    },
    beforeDestroy() {
        this.correctPageMinHeight(-this.minHeight + 24);
    },
};
</script>

<style lang="less" scoped>
.admin-layout {
水落(YangLei)'s avatar
水落(YangLei) committed
175
    height: 100%;
wb-ct393452's avatar
wb-ct393452 committed
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
    .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
195 196 197
        display: flow-root;
        position: initial;

wb-ct393452's avatar
wb-ct393452 committed
198 199 200 201 202 203 204 205 206 207 208
        .admin-header {
            top: 0;
            right: 0;
            overflow: hidden;
            transition: all 0.2s;
            &.fixed-tabs.multi-page:not(.fixed-header) {
                height: 0;
            }
        }
    }
    .admin-layout-content {
209
        padding: 24px;
wb-ct393452's avatar
wb-ct393452 committed
210 211 212 213 214 215 216 217 218 219 220 221 222
    }
    .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>