Menu.vue 8.91 KB
<template>
    <a-menu class="menu" 
            ref="menuRef" 
            theme="dark" 
            mode="inline" 
            v-model:selectedKeys="selectedKeys"
            v-model:openKeys="openKeys"
            >
        <a-sub-menu v-for="item in menuList" :key="item.origin.menuNo" @titleClick="titleClick(item)">
            <template v-slot:title>
                <span>
                    <img v-if="item.origin.icon" :src="item.origin.icon" />
                    <UserOutlined v-else />
                    <span>{{item.origin.menuName}}</span>
                </span>
            </template>
            <a-menu-item v-for="cItem in item.children" :key="cItem.origin.menuNo" @click="cItemClick(cItem.origin.path || '', cItem)">
                {{cItem.origin.menuName}}
            </a-menu-item>
        </a-sub-menu>
    </a-menu>
</template>

<script>
import {
  UserOutlined,
  TeamOutlined,
  FileOutlined,
} from '@ant-design/icons-vue';
import { reactive, toRefs, onMounted, computed } from 'vue';
import { useStore } from 'vuex';

import MenuService from 'services/Menu';
import { menuTree as menuTreeLocalList } from 'config/menuTree';

export default {
    name: 'Menu',
    components: {
        UserOutlined,
        TeamOutlined,
        FileOutlined,
    },
    setup(props, context) {
        const store = useStore();
        const state = reactive({
            openKeys: [],
            selectedKeys: [],
            menuList: menuTreeLocalList,
            curMainTitle: '',
            curBreadCrumb: {
                parent: {},
                children: []
            }
        });

        // 点击主菜单标题
        const titleClick = (item) => {
            state.curMainTitle = item.origin.menuName;
        };

        // 点击二级菜单标题
        const cItemClick = (path, cItem) => {
            const cOrigin = cItem.origin;
            state.curBreadCrumb.parent['menuName'] = state.curMainTitle;
            state.curBreadCrumb.children = [{
                menuName: cOrigin.menuName,
                path: cOrigin.path || '',
                active: true
            }];
            history.pushState(null, path, path);
        }

        // 合并本地和后台返回的菜单数据
        const mergeData = (server, localArr) => {
            var serverMenuNo = server.origin.menuNo;
            var localEle;
            for (const key in localArr) {
                var localMenuNo = localArr[key].origin.menuNo;
                if (serverMenuNo == localMenuNo) {
                    localEle = localArr[key]
                    break;
                }
            }
            if (!localEle) {
                return server;
            } else {
                var children = [];
                for (const key in server.children) {
                    var sMenuNo = server.children[key].origin.menuNo;
                    for (const key in localEle.children) {
                        var localChildrenEle = localEle.children[key].origin;
                        if(sMenuNo == localChildrenEle.menuNo) {
                            children.push({origin: localChildrenEle});
                            break;
                        }
                        if (key == localEle.children.length - 1) {
                            children.push({origin: server.children[key].origin});
                        }
                    }
                }
                return {origin: localEle.origin, 'children': children}
            }
        }

        // 遍历菜单列表并合并
        const getMenuTree = (serverArr, localArr) => {
            var arr = []
            for (const key in serverArr) {
                arr.push(mergeData(serverArr[key], localArr));
            }
            console.log(arr);
            return arr;
        }

        // 获取权限菜单(假数据)
        // const res = {"code":200,"data":[{"children":[{"children":[],"id":20101,"origin":{"menuNo":20101,"menuName":"ODB管理","parentId":201,"type":2,"icon":null,"frontRoute":null,"orderNum":1,"checked":false},"order":1,"depth":null,"parentId":201,"label":"ODB管理"}],"id":201,"origin":{"menuNo":201,"menuName":"设备管理","parentId":0,"type":1,"icon":null,"frontRoute":null,"orderNum":1,"checked":false},"order":1,"depth":null,"parentId":0,"label":"设备管理"},{"children":[{"children":[],"id":30101,"origin":{"menuNo":30101,"menuName":"测试菜单","parentId":301,"type":2,"icon":null,"frontRoute":"bbb","orderNum":1,"checked":false},"order":1,"depth":null,"parentId":301,"label":"测试菜单"}],"id":301,"origin":{"menuNo":301,"menuName":"测试菜单","parentId":0,"type":1,"icon":null,"frontRoute":"bbb","orderNum":1,"checked":false},"order":1,"depth":null,"parentId":0,"label":"测试菜单"},{"children":[{"children":[],"id":10101,"origin":{"menuNo":10101,"menuName":"用户管理","parentId":101,"type":2,"icon":null,"frontRoute":null,"orderNum":1,"checked":false},"order":1,"depth":null,"parentId":101,"label":"用户管理"},{"children":[],"id":10102,"origin":{"menuNo":10102,"menuName":"用户组管理","parentId":101,"type":2,"icon":null,"frontRoute":null,"orderNum":2,"checked":false},"order":2,"depth":null,"parentId":101,"label":"用户组管理"},{"children":[],"id":10103,"origin":{"menuNo":10103,"menuName":"角色管理","parentId":101,"type":2,"icon":null,"frontRoute":null,"orderNum":3,"checked":false},"order":3,"depth":null,"parentId":101,"label":"角色管理"},{"children":[],"id":10104,"origin":{"menuNo":10104,"menuName":"菜单管理","parentId":101,"type":2,"icon":null,"frontRoute":null,"orderNum":4,"checked":false},"order":4,"depth":null,"parentId":101,"label":"菜单管理"},{"children":[],"id":10105,"origin":{"menuNo":10105,"menuName":"组织管理","parentId":101,"type":2,"icon":null,"frontRoute":null,"orderNum":5,"checked":false},"order":5,"depth":null,"parentId":101,"label":"组织管理"},{"children":[{"children":[],"id":1010607,"origin":{"menuNo":1010607,"menuName":"数据字典3","parentId":10106,"type":2,"icon":null,"frontRoute":"bbb","orderNum":1,"checked":false},"order":1,"depth":null,"parentId":10106,"label":"数据字典3"},{"children":[],"id":1010608,"origin":{"menuNo":1010608,"menuName":"数据字典4","parentId":10106,"type":2,"icon":null,"frontRoute":"bbb","orderNum":1,"checked":false},"order":1,"depth":null,"parentId":10106,"label":"数据字典4"},{"children":[],"id":1010609,"origin":{"menuNo":1010609,"menuName":"数据字典5","parentId":10106,"type":2,"icon":null,"frontRoute":"bbb","orderNum":1,"checked":false},"order":1,"depth":null,"parentId":10106,"label":"数据字典5"},{"children":[],"id":1010610,"origin":{"menuNo":1010610,"menuName":"数据字典6","parentId":10106,"type":2,"icon":null,"frontRoute":"bbb","orderNum":1,"checked":false},"order":1,"depth":null,"parentId":10106,"label":"数据字典6"}],"id":10106,"origin":{"menuNo":10106,"menuName":"字典管理","parentId":101,"type":2,"icon":null,"frontRoute":null,"orderNum":6,"checked":false},"order":6,"depth":null,"parentId":101,"label":"字典管理"},{"children":[],"id":10107,"origin":{"menuNo":10107,"menuName":"操作日志","parentId":101,"type":2,"icon":null,"frontRoute":null,"orderNum":7,"checked":false},"order":7,"depth":null,"parentId":101,"label":"操作日志"}],"id":101,"origin":{"menuNo":101,"menuName":"系统管理","parentId":0,"type":1,"icon":null,"frontRoute":null,"orderNum":2,"checked":false},"order":2,"depth":null,"parentId":0,"label":"系统管理"}],"message":"success"};
        // const getNewTree = getMenuTree(res.data, menuTreeLocalList);
        
        // 获取菜单树接口
        const getServerTree = async () => {
            const menuService = new MenuService();
            menuService.getMenuTree().then( res => {
                if(res.code === 200) {
                    const resData = res.data;
                    const getNewTree = getMenuTree(res.data, menuTreeLocalList),
                          firstParent = getNewTree[0].origin,
                          firstChildren = getNewTree[0].children[0].origin;
                    console.log(getNewTree);
                    state.menuList = getNewTree;
                    state.openKeys = [firstParent.menuNo];
                    state.selectedKeys = [firstChildren.menuNo];
                    state.curBreadCrumb.parent['menuName'] = firstParent.menuName;
                    state.curBreadCrumb.children.push({
                        menuName: firstChildren.menuName,
                        path: firstChildren.path || '',
                        active: true
                    });
                    store.dispatch('menu/setBreadCrumb', state.curBreadCrumb);
                }
            }, err => {

            })
        };
        getServerTree();

        

        onMounted(() => {
            console.log(context)
        });

        return {
            ...toRefs(state),
            titleClick,
            cItemClick
        }
    }
    
}
</script>

<style lang="less" scoped>
@import 'styles/variables.less';

.menu {
    background-color: @defaultThemeColor;
}
</style>