Menu.vue 18.4 KB
<template>
    <div>
        <!-- ant-menu-inline-collapsed -->
        <ul class="menu ant-menu-dark ant-menu-root ant-menu ant-menu-inline" :updateMenu="updateMenu">
            <!-- ant-menu-submenu-active ant-menu-submenu-open ant-menu-submenu-selected -->
            <li class="ant-menu-submenu ant-menu-submenu-inline"
                :class="{'ant-menu-submenu-open': item.showSubList}"
                v-for="item in menuList" 
                :key="item.origin.menuNo"
                @click="titleClick(item)" >
                <div class="ant-menu-submenu-title" style="padding-left: 24px;">
                    <DesktopOutlined v-if="item.origin.icon === 'DesktopOutlined'" />
                    <CarOutlined v-if="item.origin.icon === 'CarOutlined'" />
                    <SettingOutlined v-if="!item.origin.icon" />
                    <span>{{item.origin.menuName}}</span>
                    <i class="ant-menu-submenu-arrow"></i>
                </div>

                <!-- ant-menu-item-selected ant-menu-item-active -->
                <!-- ant-motion-collapse-legacy ant-motion-collapse-legacy-active -->
                <ul class="ant-menu-sub ant-menu ant-menu-inline" 
                    v-if="item.showSubList"
                    >
                    <div v-for="cItem in item.children" 
                              :key="cItem.origin.menuNo">
                        <!-- 三级菜单 -->
                        <li v-if="cItem.children && cItem.children.length > 0"
                            class="ant-menu-submenu ant-menu-submenu-inline"
                             :class="{'ant-menu-submenu-open': cItem.showSubList}"
                            @click.stop="cItemClick(cItem.origin.frontRoute || '', cItem, item)">
                            <div aria-haspopup="true"
                                 @click="titleClick(cItem)"
                                 title="Submenu" class="ant-menu-submenu-title"
                                 aria-expanded="true"
                                 aria-owns="sub3$Menu" style="padding-left: 48px;">
                                {{cItem.origin.menuName}}
                                <i class="ant-menu-submenu-arrow"></i>
                            </div>
                            <template v-if="cItem.showSubList">
                                <ul class="ant-menu ant-menu-inline ant-menu-sub"
                                    v-for="thirdChildrenItem in cItem.children"
                                    :key="thirdChildrenItem.origin.menuNo">
                                    <li class="ant-menu-item" 
                                        :class="{'ant-menu-item-selected': thirdChildrenItem.subItemActive}"
                                        style="padding-left: 72px;"
                                        @click.stop="subMenuClicked(thirdChildrenItem, cItem, item)">{{thirdChildrenItem.origin.menuName}}</li>
                                </ul>
                            </template>
                        </li>
                         <!-- 二级菜单 -->
                        <li v-else
                            class="ant-menu-item"
                            :class="{'ant-menu-item-selected': cItem.subItemActive}"
                            style="padding-left: 48px;"
                            @click.stop="cItemClick(cItem.origin.frontRoute || '', cItem, item)">
                            {{cItem.origin.menuName}}
                        </li>
                    </div>
                </ul>
            </li>
        </ul>
    </div>
</template>

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

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

export default {
    name: 'Menu',
    components: {
        CarOutlined,
        DesktopOutlined,
        SettingOutlined,
    },
    setup(props, context) {
        const store = useStore();
        const state = reactive({
            menuList: menuTreeLocalList,
            curBreadCrumb: {
                parent: {},
                children: []
            }
        });

        // 点击主菜单标题
        const titleClick = (item) => {
            item.showSubList = !item.showSubList;
        };

        // 高亮被选中的子集标题
        const setActive = (arr) =>{
            arr.forEach(cEle => {
                cEle.subItemActive = false;
                if(cEle.children) {
                    setActive(cEle.children);
                }
            })
        }

        const filterPath = (path) => {
            return path && (path.includes('system') || path.includes('monitor') || path.includes('oEshield')) ? path : '/noData';
        }

        // 存储面包屑
        const saveBreadCrumbFunc = (data) => {
            store.dispatch('menu/setBreadCrumb', data);
        }

        // 点击三级菜单做存储
        const subMenuClicked = (thisItem, parentItem, item) => {
            let showEmpty = false;
            const pOrigin = parentItem.origin,
                  thisOrigin = thisItem.origin,
                  itemOrigin = item.origin;
                  // 高亮被选中的标题
            state.menuList.forEach(ele => {
                const children = ele.children;
                setActive(children);
            })
            thisItem.subItemActive = true;

            state.curBreadCrumb.parent = {
                menuName: itemOrigin.menuName,
                path: filterPath(itemOrigin.frontRoute)
            }
            state.curBreadCrumb.children = {
                menuName: pOrigin.menuName,
                path: filterPath(pOrigin.frontRoute),
                active: false,
                children: {
                    menuName: thisOrigin.menuName,
                    path: filterPath(thisOrigin.frontRoute),
                    active: true,
                }
            };

            saveBreadCrumbFunc(state.curBreadCrumb);

            let path = filterPath(thisOrigin.frontRoute);
            if(path === '/noData') {
                showEmpty = true;
            }
            store.dispatch('app/setCommonData', { showEmpty });
            history.pushState(null, path, path);
        }

        // 点击二级菜单标题
        const cItemClick = (path, cItem, parentItem) => {
            const cOrigin = cItem.origin;
            const pOrigin = parentItem.origin;
            let showEmpty = false;
            // 高亮被选中的标题
            state.menuList.forEach(ele => {
                const children = ele.children;
                setActive(children);
            })
            cItem.subItemActive = true;

            state.curBreadCrumb.parent = {
                menuName: pOrigin.menuName,
                path: filterPath(pOrigin.frontRoute)
            }

            state.curBreadCrumb.children = {
                menuName: cOrigin.menuName,
                path: filterPath(cOrigin.frontRoute),
                active: true
            };

            saveBreadCrumbFunc(state.curBreadCrumb);

            path = filterPath(path);
            if(path === '/noData') {
                showEmpty = true;
            }
            store.dispatch('app/setCommonData', { showEmpty });
            history.pushState(null, path, path);
        }

        // 遍历多个子级
        // const getChildrenArr = (server, localEle) => {
        //     var children = [];
        //     const serverChildren = server.children;
        //     const localChildren = localEle.children;
        //     for (const key1 in serverChildren) {
        //         var sMenuNo = serverChildren[key1].origin.menuNo;
        //         var serverNextChildren = serverChildren[key1].children;
        //         if(localChildren) {
        //             for (const key2 in localChildren) {
        //                 let nextChildren = [];
        //                 var localChildrenEle = localChildren[key2].origin;
        //                 var localNextChildren = localChildren[key2].children;
        //                 if(serverNextChildren) {
        //                     nextChildren = getChildrenArr(serverChildren[key1], localChildren[key2]);
        //                 }
        //                 if(sMenuNo == localChildrenEle.menuNo) {
        //                     children.push({origin: localChildrenEle, children: nextChildren});
        //                     break;
        //                 }
        //                 if (key2 == localChildren.length - 1) {
        //                     children.push({origin: serverChildren[key1].origin, children: nextChildren});
        //                 }
        //             }
        //         } else {
        //             children.push({ origin: serverChildren[key1].origin, children: serverNextChildren });
        //         }
        //     }
        //     return children;
        // }

        // 合并本地和后台返回的菜单数据
        // 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 = [];
        //         if(server.children) {
        //             children = getChildrenArr(server, localEle);
        //         }
        //         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 (isInit) => {
            const menuService = new MenuService();
            menuService.getMenuTree().then( res => {
                if(res.code === 200) {
                    
                    const resData = res.data;
                    // const getNewTree = getMenuTree(res.data, menuTreeLocalList)
                    state.menuList = resData;
                    // 初始化操作
                    const firstParent = resData[0].origin ? resData[0].origin : {},
                          firstChildren = resData[0].children.length > 0 ? resData[0].children[0].origin : {};
                    let showEmpty = false;
                    
                    // 同步面包屑
                    const getBreadCrumb = store.getters.curBreadCrumb;
                    const getCurChildren = getBreadCrumb.children ? getBreadCrumb.children : firstChildren;
                    let getCurParent = getBreadCrumb.parent ? getBreadCrumb.parent : firstParent,
                        childrenMenuName = getCurChildren.menuName,
                        childrenPath = getCurChildren.path || getCurChildren.frontRoute || '',
                        getCurNextChildren = getCurChildren.children;
                    if(isInit) { 
                        state.curBreadCrumb.parent = {
                            menuName: getCurParent.menuName,
                            path: getCurParent.path
                        };
                        let getCurNextChildrenArr = {
                            menuName: childrenMenuName,
                            path: childrenPath,
                            active: true
                        };
                        if(getCurNextChildren) {
                            getCurNextChildrenArr.active = false;
                            getCurNextChildrenArr.children = {
                                menuName: getCurNextChildren.menuName,
                                path: getCurNextChildren.path,
                                active: true
                            };
                        }
                        state.curBreadCrumb.children = getCurNextChildrenArr;
                    }

                    // 同步激活样式
                    resData.forEach((item, index) => {
                        item.showSubList = item.origin.menuName == getCurParent.menuName ? true: false;
                        item.children.forEach((cItem, cIndex) => {
                            cItem.subItemActive = cItem.origin.menuName === childrenMenuName ? true: false;
                            cItem.showSubList = cItem.origin.menuName == childrenMenuName ? true: false;
                            const nextChildren = cItem.children;
                            if(nextChildren && getCurNextChildren) {
                                nextChildren.forEach((nItem) => {
                                    nItem.subItemActive = nItem.origin.menuName === getCurNextChildren.menuName ? true: false;
                                })
                            }
                        })
                    });

                    if(isInit) {
                        let params = null;
                        if(childrenPath === '/noData' || childrenPath === '/' || childrenPath === ''
                        || (getCurNextChildren && getCurNextChildren.path === '/noData')
                        || (getCurNextChildren && getCurNextChildren.path === '/')) {
                            showEmpty = true;
                        }
                        store.dispatch('app/setCommonData', { showEmpty });
                        let thisPath = childrenPath;
                        if(getCurNextChildren) {
                            thisPath = getCurNextChildren.path;
                            params = getCurNextChildren.params ? getCurNextChildren.params : null;
                        }
                        history.pushState(null, thisPath, thisPath);
                        store.dispatch('menu/setBreadCrumb', state.curBreadCrumb);
                    }
                }
            }, err => {

            })
        };
        getServerTree(true);

        const updateMenu = computed(() => {
            const updateNow = !!store.getters.updateMenu;
            if(updateNow) {
                getServerTree(false);
            }
            return updateNow;
        });

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

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

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