diff --git a/src/components/Sidebar.vue b/src/components/Sidebar.vue index 9a1730c..3c41934 100644 --- a/src/components/Sidebar.vue +++ b/src/components/Sidebar.vue @@ -299,7 +299,85 @@ export default { methods: { setCurrentRoute() { console.log(this.$route.path); - this.activeIndex = this.$route.path; // 通过他就可以监听到当前路由状态并激活当前菜单 + this.$nextTick(() => { + this.activeIndex = this.$route.path; // 通过他就可以监听到当前路由状态并激活当前菜单 + console.log(this.activeIndex); + }); + }, + + getMenuArr(originalArray) { + const icons = { + stritl: "el-icon-s-home", + realTimeMonitor: "el-icon-camera", + pictureRotation: "el-icon-film", + photoAlarm: "el-icon-bell", + realTimeSearch: "el-icon-search", + // ... 其他图标 + default: "el-icon-default", // 默认图标 + }; + const assetManagementSubs = []; // 用于存储资产管理子菜单的对象 + const systemManagementSubs = []; // 用于存储系统管理子菜单的对象 + const newArray = []; + + // 遍历原始数组,根据条件构建新数组 + originalArray.forEach((item) => { + const { id, key, desc } = item; + console.log(key); + const index = key.startsWith("/") ? key.substring(1) : key; // 移除开头的斜杠 + const icon = icons[index] || icons["default"]; // 获取图标,如果未找到则使用默认图标 + + if ( + [ + "lineInformation", + "towerInformation", + "cameraChannel", + "photographicDevice", + "devicePhotoSchedule", + ].includes(index) + ) { + // 如果包含杆塔或线路信息,则添加到资产管理子菜单 + assetManagementSubs.push({ index: "/" + index, title: desc }); + } else if ( + [ + "userManagement", + "roleManagement", + "menuManagement", + "deviceUpgrade", + "globalTools", + "waterMark", + ].includes(index) + ) { + // 如果包含用户列表等,则添加到系统管理子菜单 + systemManagementSubs.push({ index: "/" + index, title: desc }); + } else { + // 其他菜单项直接添加到新数组中 + newArray.push({ icon, index: "/" + index, title: desc }); + } + }); + + // 如果存在资产管理子菜单项,则添加资产管理菜单 + if (assetManagementSubs.length > 0) { + newArray.push({ + icon: "el-icon-files", + index: "property", + title: "资产管理", + subs: assetManagementSubs, + }); + } + + // 如果存在系统管理子菜单项,则添加系统管理菜单 + if (systemManagementSubs.length > 0) { + newArray.push({ + icon: "el-icon-setting", // 假设的系统管理图标 + index: "system", + title: "系统管理", + subs: systemManagementSubs, + }); + } + console.log("我是最后的", newArray); + + //return newArray; + this.items = newArray; }, }, created() { @@ -308,12 +386,10 @@ export default { this.role = localStorage.getItem("role"); console.log("用户管理"); console.log(this.role); - // if (this.role == 0) { - // this.items = this.items; - // } else { - // this.menuList = JSON.parse(localStorage.getItem("menuPermission")); - // this.items = this.menuList; - // } + + // this.menuList = JSON.parse(localStorage.getItem("menuPermission")); + // console.log(this.menuList); + // this.getMenuArr(this.menuList); if (this.role == 0) { this.items = this.items; @@ -324,8 +400,8 @@ export default { } else if (this.role == 4) { this.items = this.hnjcitems; } - //this.activeIndex = this.items[0].index; - console.log(this.items); + // //this.activeIndex = this.items[0].index; + // console.log(this.items); console.log(this.activeIndex); }, };