From 7abf4bfa81322e9c6d30a55ab2b43d891b5cc90e Mon Sep 17 00:00:00 2001 From: TimSpan Date: Wed, 30 Apr 2025 15:30:44 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A8=A1=E6=8B=9F=E5=8A=A8=E6=80=81=E8=B7=AF?= =?UTF-8?q?=E7=94=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/router/dynamicRouters.ts | 77 ++-- src/router/getPages.ts | 555 +++++++++++++++++++++++++++ src/router/index.ts | 8 +- src/views/development/menu/index.vue | 10 +- src/views/system/dept/index.vue | 1 + 5 files changed, 602 insertions(+), 49 deletions(-) create mode 100644 src/router/getPages.ts diff --git a/src/router/dynamicRouters.ts b/src/router/dynamicRouters.ts index 7b576e9..228e88f 100644 --- a/src/router/dynamicRouters.ts +++ b/src/router/dynamicRouters.ts @@ -7,7 +7,7 @@ import { type Component } from "vue"; import router from "@/router"; import { LOGIN_ROUTER } from "@/config/constant"; import { type RouteRecordRaw } from "vue-router"; - +import { getPages } from "./getPages"; const modules: Record Promise> = import.meta.glob("@/views/**/*.vue"); @@ -15,14 +15,15 @@ export const initDynamicRouter = async () => { const userStore = useUserStore(); const permissionStore = usePermissionStore(); - //获取我授权的menu + // 获取我授权的menu + const resp = await getPages(); // const resp = await api.get("/management/auth/myAuthMenu"); - // if (isEmpty(resp.data)) { - // message.warning("您没有任何权限"); - // userStore.resetUserInfo(); - // await router.replace(LOGIN_ROUTER.path); - // return Promise.reject("当前用户暂无授权权限,请联系管理员进行添加"); - // } + if (isEmpty(resp.data)) { + message.warning("您没有任何权限"); + userStore.resetUserInfo(); + await router.replace(LOGIN_ROUTER.path); + return Promise.reject("当前用户暂无授权权限,请联系管理员进行添加"); + } const removeEmptyChildren = (data: AuthMenuTree[]) => { return data.map((item) => { if (item.type.value === "dir") { @@ -35,37 +36,37 @@ export const initDynamicRouter = async () => { return item; }); }; - // permissionStore.initAuthMenuList( - // removeEmptyChildren(resp.data as AuthMenuTree[]) - // ); - //扁平化路由 提取children - // const flatAuthMenu = flatMenuList(resp.data as AuthMenuTree[]); + permissionStore.initAuthMenuList( + removeEmptyChildren(resp.data as AuthMenuTree[]) + ); + // 扁平化路由 提取children + const flatAuthMenu = flatMenuList(resp.data as AuthMenuTree[]); // 动态添加路由 - // flatAuthMenu.forEach((item) => { - // const route: RouteRecordRaw = { - // name: item.snowFlakeId, - // path: item.path, - // redirect: item.redirect, - // meta: { - // btnList: item.btnList, - // title: item.name, - // icon: item.icon, - // isFixed: false, - // isKeepAlive: item.isKeepAlive.extData?.boolVal ?? false, - // isFull: item.isFull.extData?.boolVal ?? false, - // remark: item.remark, - // }, - // component: modules[`/src/views${item.path}/index.vue`], - // children: [], - // }; - // if (item.isFull.extData?.boolVal) { - // //全屏路由添加到根路由下 - // router.addRoute(route); - // } else { - // //非全屏路由添加到layout下 - // router.addRoute("layout", route); - // } - // }); + flatAuthMenu.forEach((item) => { + const route: RouteRecordRaw = { + name: item.snowFlakeId, + path: item.path, + redirect: item.redirect, + meta: { + btnList: item.btnList, + title: item.name, + icon: item.icon, + isFixed: false, + isKeepAlive: item.isKeepAlive.extData?.boolVal ?? false, + isFull: item.isFull.extData?.boolVal ?? false, + remark: item.remark, + }, + component: modules[`/src/views${item.path}/index.vue`], + children: [], + }; + if (item.isFull.extData?.boolVal) { + //全屏路由添加到根路由下 + router.addRoute(route); + } else { + //非全屏路由添加到layout下 + router.addRoute("layout", route); + } + }); }; const flatMenuList = ( diff --git a/src/router/getPages.ts b/src/router/getPages.ts new file mode 100644 index 0000000..b709779 --- /dev/null +++ b/src/router/getPages.ts @@ -0,0 +1,555 @@ +export const getPages = (): Promise => { + return new Promise((resolve) => { + resolve({ + code: 200, + data: [ + { + btnList: [], + children: [ + { + btnList: [], + children: [], + createBy: null, + createTime: null, + deleteFlag: null, + icon: "CodiconOrganization", + isFull: { + extData: { + boolVal: false, + }, + label: "否", + value: 1, + }, + isKeepAlive: { + extData: { + boolVal: true, + }, + label: "是", + value: 0, + }, + link: "", + name: "部门架构", + orderIndex: null, + parentId: "1907342465921191936", + path: "/personnelManagement/departmentStructure", + redirect: "", + remark: "", + snowFlakeId: "1907343323534725120", + status: null, + type: { + extData: null, + label: "菜单", + value: "menu", + }, + updateBy: null, + updateTime: null, + }, + { + btnList: [], + children: [], + createBy: null, + createTime: null, + deleteFlag: null, + icon: "LineMdAccount", + isFull: { + extData: { + boolVal: false, + }, + label: "否", + value: 1, + }, + isKeepAlive: { + extData: { + boolVal: true, + }, + label: "是", + value: 0, + }, + link: "", + name: "企业员工", + orderIndex: null, + parentId: "1907342465921191936", + path: "/personnelManagement/enterpriseStaff", + redirect: "", + remark: "", + snowFlakeId: "1907342952527564800", + status: null, + type: { + extData: null, + label: "菜单", + value: "menu", + }, + updateBy: null, + updateTime: null, + }, + { + btnList: [], + children: [], + createBy: null, + createTime: null, + deleteFlag: null, + icon: "LineMdAccount", + isFull: { + extData: { + boolVal: false, + }, + label: "否", + value: 1, + }, + isKeepAlive: { + extData: { + boolVal: true, + }, + label: "是", + value: 0, + }, + link: "", + name: "安保员工", + orderIndex: null, + parentId: "1907342465921191936", + path: "/personnelManagement/securityStaff", + redirect: "", + remark: "", + snowFlakeId: "1907344307979816960", + status: null, + type: { + extData: null, + label: "菜单", + value: "menu", + }, + updateBy: null, + updateTime: null, + }, + ], + createBy: null, + createTime: null, + deleteFlag: null, + icon: "LineMdAccount", + isFull: { + extData: { + boolVal: false, + }, + label: "否", + value: 1, + }, + isKeepAlive: { + extData: { + boolVal: true, + }, + label: "是", + value: 0, + }, + link: "", + name: "人事管理", + orderIndex: null, + parentId: "0", + path: "/personnelManagement", + redirect: "", + remark: "", + snowFlakeId: "1907342465921191936", + status: null, + type: { + extData: null, + label: "目录", + value: "dir", + }, + updateBy: null, + updateTime: null, + }, + { + btnList: [], + children: [ + { + btnList: [ + { + extData: { + remark: "测试按钮2", + }, + label: "测试按钮2", + value: "test2", + }, + ], + children: [], + createBy: null, + createTime: null, + deleteFlag: null, + icon: "EosIconsRoleBinding", + isFull: { + extData: { + boolVal: false, + }, + label: "否", + value: 1, + }, + isKeepAlive: { + extData: { + boolVal: false, + }, + label: "否", + value: 1, + }, + link: "", + name: "角色", + orderIndex: null, + parentId: "1904795567075102720", + path: "/system/role", + redirect: "", + remark: "", + snowFlakeId: "1904812573660811264", + status: null, + type: { + extData: null, + label: "菜单", + value: "menu", + }, + updateBy: null, + updateTime: null, + }, + ], + createBy: null, + createTime: null, + deleteFlag: null, + icon: "LineMdCogFilledLoop", + isFull: { + extData: { + boolVal: false, + }, + label: "否", + value: 1, + }, + isKeepAlive: { + extData: { + boolVal: false, + }, + label: "否", + value: 1, + }, + link: "", + name: "系统管理", + orderIndex: null, + parentId: "0", + path: "/system", + redirect: "/system/user", + remark: "", + snowFlakeId: "1904795567075102720", + status: null, + type: { + extData: null, + label: "目录", + value: "dir", + }, + updateBy: null, + updateTime: null, + }, + { + btnList: [], + children: [ + { + btnList: [], + children: [], + createBy: null, + createTime: null, + deleteFlag: null, + icon: "LineMdList", + isFull: { + extData: { + boolVal: false, + }, + label: "否", + value: 1, + }, + isKeepAlive: { + extData: { + boolVal: false, + }, + label: "否", + value: 1, + }, + link: "", + name: "菜单管理", + orderIndex: null, + parentId: "1905455414980382720", + path: "/development/menu", + redirect: "", + remark: "", + snowFlakeId: "1905455579321602048", + status: null, + type: { + extData: null, + label: "菜单", + value: "menu", + }, + updateBy: null, + updateTime: null, + }, + ], + createBy: null, + createTime: null, + deleteFlag: null, + icon: "LineMdDocumentCode", + isFull: { + extData: { + boolVal: false, + }, + label: "否", + value: 1, + }, + isKeepAlive: { + extData: { + boolVal: false, + }, + label: "否", + value: 1, + }, + link: "", + name: "开发管理", + orderIndex: null, + parentId: "0", + path: "/development", + redirect: "", + remark: "", + snowFlakeId: "1905455414980382720", + status: null, + type: { + extData: null, + label: "目录", + value: "dir", + }, + updateBy: null, + updateTime: null, + }, + { + btnList: [], + children: [ + { + btnList: [], + children: [], + createBy: null, + createTime: null, + deleteFlag: null, + icon: "LineMdList", + isFull: { + extData: { + boolVal: false, + }, + label: "否", + value: 1, + }, + isKeepAlive: { + extData: { + boolVal: false, + }, + label: "否", + value: 1, + }, + link: "", + name: "测试1菜单2", + orderIndex: null, + parentId: "1910252674549288960", + path: "/test1/test1menu2", + redirect: "", + remark: "", + snowFlakeId: "1910253280865292288", + status: null, + type: { + extData: null, + label: "菜单", + value: "menu", + }, + updateBy: null, + updateTime: null, + }, + { + btnList: [], + children: [], + createBy: null, + createTime: null, + deleteFlag: null, + icon: "LineMdList", + isFull: { + extData: { + boolVal: false, + }, + label: "否", + value: 1, + }, + isKeepAlive: { + extData: { + boolVal: false, + }, + label: "否", + value: 1, + }, + link: "", + name: "测试1菜单1", + orderIndex: null, + parentId: "1910252674549288960", + path: "/test1/test1menu1", + redirect: "", + remark: "", + snowFlakeId: "1910253160014811136", + status: null, + type: { + extData: null, + label: "菜单", + value: "menu", + }, + updateBy: null, + updateTime: null, + }, + { + btnList: [], + children: [ + { + btnList: [], + children: [], + createBy: null, + createTime: null, + deleteFlag: null, + icon: "LineMdList", + isFull: { + extData: { + boolVal: false, + }, + label: "否", + value: 1, + }, + isKeepAlive: { + extData: { + boolVal: false, + }, + label: "否", + value: 1, + }, + link: "", + name: "测试2菜单2", + orderIndex: null, + parentId: "1910252952203825152", + path: "/test1/test2/test2menu2", + redirect: "", + remark: "", + snowFlakeId: "1910253703755993088", + status: null, + type: { + extData: null, + label: "菜单", + value: "menu", + }, + updateBy: null, + updateTime: null, + }, + { + btnList: [], + children: [], + createBy: null, + createTime: null, + deleteFlag: null, + icon: "LineMdList", + isFull: { + extData: { + boolVal: false, + }, + label: "否", + value: 1, + }, + isKeepAlive: { + extData: { + boolVal: false, + }, + label: "否", + value: 1, + }, + link: "", + name: "测试2菜单1", + orderIndex: null, + parentId: "1910252952203825152", + path: "/test1/test2/test2menu1", + redirect: "", + remark: "", + snowFlakeId: "1910253563792068608", + status: null, + type: { + extData: null, + label: "菜单", + value: "menu", + }, + updateBy: null, + updateTime: null, + }, + ], + createBy: null, + createTime: null, + deleteFlag: null, + icon: "CarbonSubtractAlt", + isFull: { + extData: { + boolVal: false, + }, + label: "否", + value: 1, + }, + isKeepAlive: { + extData: { + boolVal: true, + }, + label: "是", + value: 0, + }, + link: "", + name: "测试2", + orderIndex: null, + parentId: "1910252674549288960", + path: "/test2", + redirect: "", + remark: "", + snowFlakeId: "1910252952203825152", + status: null, + type: { + extData: null, + label: "目录", + value: "dir", + }, + updateBy: null, + updateTime: null, + }, + ], + createBy: null, + createTime: null, + deleteFlag: null, + icon: "CarbonSubtractAlt", + isFull: { + extData: { + boolVal: false, + }, + label: "否", + value: 1, + }, + isKeepAlive: { + extData: { + boolVal: true, + }, + label: "是", + value: 0, + }, + link: "", + name: "测试1", + orderIndex: null, + parentId: "0", + path: "/test1", + redirect: "", + remark: "", + snowFlakeId: "1910252674549288960", + status: null, + type: { + extData: null, + label: "目录", + value: "dir", + }, + updateBy: null, + updateTime: null, + }, + ], + message: "操作成功!", + }); + }); +}; diff --git a/src/router/index.ts b/src/router/index.ts index d779975..1e97dc0 100644 --- a/src/router/index.ts +++ b/src/router/index.ts @@ -37,10 +37,10 @@ router.beforeEach(async (to, from, next) => { // } // // // 6.如果没有菜单列表,就重新请求菜单列表并添加动态路由 - // if (isEmpty(permissionStore.authMenuList)) { - // await initDynamicRouter(); - // return next({ ...to, replace: true }); - // } + if (isEmpty(permissionStore.authMenuList)) { + await initDynamicRouter(); + return next({ ...to, replace: true }); + } //放行 return next(); }); diff --git a/src/views/development/menu/index.vue b/src/views/development/menu/index.vue index 4aec967..c219c3b 100644 --- a/src/views/development/menu/index.vue +++ b/src/views/development/menu/index.vue @@ -1,11 +1,7 @@ - - - - + diff --git a/src/views/system/dept/index.vue b/src/views/system/dept/index.vue index 1312e13..688d098 100644 --- a/src/views/system/dept/index.vue +++ b/src/views/system/dept/index.vue @@ -1,3 +1,4 @@ +