AdminLayout.vue 6.55 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 42 43 44 45 46 47 48 49
            <layout-top-header
                :class="[{ 'fixed-tabs': fixedTabs, 'fixed-header': fixedHeader, 'multi-page': multiPage }]"
                :style="headerStyle"
                :menuData="headMenuData"
                :collapsed="collapsed"
                @toggleCollapse="toggleCollapse"
            />
            <a-layout-header
                :class="[
                    'virtual-header',
                    { 'fixed-tabs': fixedTabs, 'fixed-header': fixedHeader, 'multi-page': multiPage },
                ]"
                v-show="fixedHeader"
            ></a-layout-header>
wb-ct393452's avatar
wb-ct393452 committed
50 51 52 53 54 55 56 57 58 59 60 61
            <a-layout-content class="admin-layout-content" :style="`min-height: ${minHeight}px;`">
                <div style="position: relative">
                    <slot></slot>
                </div>
            </a-layout-content>
        </a-layout>
    </a-layout>
</template>

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

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

export default {
    name: 'AdminLayout',
71
    components: { Setting, SideMenu, Drawer, LayoutTopHeader },
wb-ct393452's avatar
wb-ct393452 committed
72 73 74 75 76 77
    data() {
        return {
            minHeight: window.innerHeight - 64 - 122,
            collapsed: false,
            showSetting: false,
            drawerOpen: false,
78
            menuData: [],
wb-ct393452's avatar
wb-ct393452 committed
79 80 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 106 107 108 109 110 111 112
        };
    },
    provide() {
        return {
            adminLayout: this,
        };
    },
    watch: {
        $route(val) {
            this.setActivated(val);
        },
        layout() {
            this.setActivated(this.$route);
        },
        isMobile(val) {
            if (!val) {
                this.drawerOpen = false;
            }
        },
    },
    computed: {
        ...mapState('settingModule', [
            'isMobile',
            'theme',
            'layout',
            'footerLinks',
            'copyright',
            'fixedHeader',
            'fixedSideBar',
            'fixedTabs',
            'hideSetting',
            'multiPage',
        ]),

113
        ...mapGetters('settingModule', ['firstMenu', 'subMenu']),
wb-ct393452's avatar
wb-ct393452 committed
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154

        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;
            return layout === 'mix' ? firstMenu : menuData;
        },
        sideMenuData() {
            const { layout, menuData, subMenu } = this;
            return layout === 'mix' ? subMenu : menuData;
        },
    },
    methods: {
        ...mapMutations('settingModule', ['correctPageMinHeight', 'setActivatedFirst']),
        toggleCollapse() {
            this.collapsed = !this.collapsed;
        },
        onMenuSelect() {
            this.toggleCollapse();
        },
        setActivated(route) {
            if (this.layout === 'mix') {
                let matched = route.matched;
                matched = matched.slice(0, matched.length - 1);
                const { firstMenu } = this;
                for (let menu of firstMenu) {
水落(YangLei)'s avatar
水落(YangLei) committed
155
                    if (matched.findIndex(item => item.path === menu.fullPath) !== -1) {
wb-ct393452's avatar
wb-ct393452 committed
156 157 158 159 160 161 162 163 164 165
                        this.setActivatedFirst(menu.fullPath);
                        break;
                    }
                }
            }
        },
    },
    created() {
        this.correctPageMinHeight(this.minHeight - 24);
        this.setActivated(this.$route);
166 167 168
        const userInfo = getUserInfo();
        const menuData = convertListToTree(userInfo?.menuList || [], false, true);
        this.menuData = menuData;
wb-ct393452's avatar
wb-ct393452 committed
169 170 171 172 173 174 175 176 177
    },
    beforeDestroy() {
        this.correctPageMinHeight(-this.minHeight + 24);
    },
};
</script>

<style lang="less" scoped>
.admin-layout {
水落(YangLei)'s avatar
水落(YangLei) committed
178
    height: 100%;
wb-ct393452's avatar
wb-ct393452 committed
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
    .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 {
        .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>