|
|
@@ -15,6 +15,7 @@ import {getUserMenu, IntergrationMenu} from "@/api/settings/menu-settings";
|
|
|
import {createVNode, defineComponent, h, Ref} from "vue";
|
|
|
import {stringNotBlank} from "@/utils/blank-utils";
|
|
|
import {useUserStore} from "@/pinia/user-store";
|
|
|
+import XEUtils from "xe-utils";
|
|
|
|
|
|
const router = createRouter({
|
|
|
history: createWebHistory(),
|
|
|
@@ -27,28 +28,111 @@ vueFile.Layout = import.meta.glob('../layout/index.vue')['../layout/index.vue']
|
|
|
let asyncFinished = false
|
|
|
export const routerMenus: Ref<IntergrationMenu[]> = ref([])
|
|
|
|
|
|
+const 一级路由 = []
|
|
|
+const 二级路由 = []
|
|
|
+
|
|
|
+function emptyRouter(routers: IntergrationMenu[]) {
|
|
|
+ routers.forEach((item) => {
|
|
|
+ const hasChildren = item.children !== null && item.children.length > 0
|
|
|
+ if (['EmptyRouter', 'Layout'].includes(item.component) && hasChildren) {
|
|
|
+ item.children?.forEach(child => {
|
|
|
+ child.path = item.path + '/' + child.path
|
|
|
+ 一级路由.push(child)
|
|
|
+ })
|
|
|
+ } else if (hasChildren) {
|
|
|
+ item.children!.forEach(child => {
|
|
|
+ child.path = child.path.replace(item.path + '/', '')
|
|
|
+ })
|
|
|
+ 二级路由.push(item)
|
|
|
+ }
|
|
|
+
|
|
|
+ if (item.children !== null && item.children.length > 0) {
|
|
|
+ emptyRouter(item.children)
|
|
|
+ }
|
|
|
+ })
|
|
|
+}
|
|
|
+
|
|
|
+function 转化为路由(value: IntergrationMenu[]) {
|
|
|
+ const routers: RouteRecordRaw[] = []
|
|
|
+
|
|
|
+ value.forEach(item => {
|
|
|
+ const meta: { [key: string]: any } = {}
|
|
|
+ for (let key in item) {
|
|
|
+ if (key.startsWith("meta")) {
|
|
|
+ const metaKey = capitalizeFirstLower(key.replace("meta", ""))
|
|
|
+ meta[metaKey] = item[key]
|
|
|
+ }
|
|
|
+ }
|
|
|
+ let path = item.path;
|
|
|
+ if (item.pathParams?.length > 0) {
|
|
|
+ const paths = item.path.split("/$")
|
|
|
+ path = paths[0]
|
|
|
+ }
|
|
|
+ const data: RouteRecordRaw = {
|
|
|
+ path: path + item.pathParams,
|
|
|
+ name: item.name,
|
|
|
+ redirect: item.redirect,
|
|
|
+ meta: meta,
|
|
|
+ componentName: item.component,
|
|
|
+ component: createNameComponent(item)
|
|
|
+ };
|
|
|
+ if (item.children && item.children.length > 0) {
|
|
|
+ data.children = 转化为路由(item.children);
|
|
|
+ }
|
|
|
+ routers.push(data)
|
|
|
+ })
|
|
|
+ return routers
|
|
|
+}
|
|
|
+
|
|
|
async function beforeAddRoutes() {
|
|
|
await useUserStore().getUserInfo
|
|
|
routerMenus.value = await getUserMenu()
|
|
|
|
|
|
- const routers = addRoutes('',routerMenus.value)
|
|
|
- const homeRouter: Ref<IntergrationMenu> = ref({})
|
|
|
- for (let i:IntergrationMenu = 0; i < routers.length; i++) {
|
|
|
- let item = routers[i]
|
|
|
- if (item.name === 'Home') {
|
|
|
- item.children = item.children?.concat(finalRouters.value);
|
|
|
- homeRouter.value = item
|
|
|
- } else {
|
|
|
- if (item.componentName === 'Layout') {
|
|
|
- if (item.children && item.children.length > 0) {
|
|
|
- homeRouter.value.children.push(...item.children)
|
|
|
- }
|
|
|
- } else {
|
|
|
- homeRouter.value.children.push(item)
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- router.addRoute(homeRouter.value)
|
|
|
+ emptyRouter(XEUtils.clone(routerMenus.value, true))
|
|
|
+ const data = 转化为路由([{
|
|
|
+ completePath: "/",
|
|
|
+ pathParams: '',
|
|
|
+ component: 'Layout',
|
|
|
+ icon: "",
|
|
|
+ id: 0,
|
|
|
+ metaLink: false,
|
|
|
+ metaTitle: "",
|
|
|
+ name: "",
|
|
|
+ path: "/",
|
|
|
+ sort: 0,
|
|
|
+ type: 0,
|
|
|
+ children: [
|
|
|
+ {
|
|
|
+ "id": 260,
|
|
|
+ "type": 2,
|
|
|
+ "path": "dashboard",
|
|
|
+ "icon": "",
|
|
|
+ "name": "dashboard",
|
|
|
+ "parentId": 259,
|
|
|
+ "metaTitle": "首页",
|
|
|
+ "metaHideTabs": false,
|
|
|
+ "metaPassRule": true,
|
|
|
+ "metaShowMenu": true,
|
|
|
+ "sort": 0,
|
|
|
+ "component": "../views/dashboard/index.vue",
|
|
|
+ "redirect": "",
|
|
|
+ "pathParams": "",
|
|
|
+ "mainCard": false,
|
|
|
+ "mainOverflowAuto": false,
|
|
|
+ "metaLink": null,
|
|
|
+ "completePath": "dashboard",
|
|
|
+ "cascaders": [
|
|
|
+ 259
|
|
|
+ ],
|
|
|
+ "children": null,
|
|
|
+ "metaNeedToken": true
|
|
|
+ },
|
|
|
+ ...一级路由, ...二级路由
|
|
|
+ ],
|
|
|
+ }])
|
|
|
+ data.forEach(item => {
|
|
|
+ router.addRoute(item)
|
|
|
+ })
|
|
|
}
|
|
|
|
|
|
function createNameComponent(item) {
|
|
|
@@ -98,81 +182,6 @@ const capitalizeFirstLower = (word: string) => {
|
|
|
return word.charAt(0).toLowerCase() + word.slice(1);
|
|
|
}
|
|
|
|
|
|
-const finalRouters: IntergrationMenu[] = ref([])
|
|
|
-
|
|
|
-function addRoutes(parentPath: string, menus: IntergrationMenu[]) {
|
|
|
- const routers: RouteRecordRaw[] = []
|
|
|
- menus.forEach(item => {
|
|
|
- const meta: { [key: string]: any } = {}
|
|
|
- for (let key in item) {
|
|
|
- if (key.startsWith("meta")) {
|
|
|
- const metaKey = capitalizeFirstLower(key.replace("meta", ""))
|
|
|
- meta[metaKey] = item[key]
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if (!item.path.startsWith('/') && !parentPath.endsWith("/")) {
|
|
|
- item.path = parentPath + '/' + item.path;
|
|
|
- }
|
|
|
- let path = item.path;
|
|
|
- if (item.pathParams?.length > 0) {
|
|
|
- const paths = item.path.split("/$")
|
|
|
- path = paths[0]
|
|
|
- }
|
|
|
-
|
|
|
- const data: RouteRecordRaw = {
|
|
|
- path: path + item.pathParams,
|
|
|
- name: item.name,
|
|
|
- redirect: item.redirect,
|
|
|
- meta: meta,
|
|
|
- componentName: item.component,
|
|
|
- component: createNameComponent(item)
|
|
|
- };
|
|
|
-
|
|
|
- if (data.componentName === 'EmptyRouter') {
|
|
|
- if (item.children && item.children.length > 0) {
|
|
|
- recurseChildren(item.path, item.children);
|
|
|
- }
|
|
|
- } else {
|
|
|
- if (item.children && item.children.length > 0) {
|
|
|
- data.children = addRoutes(item.path,item.children);
|
|
|
- }
|
|
|
- routers.push(data)
|
|
|
- }
|
|
|
- })
|
|
|
- return routers
|
|
|
-}
|
|
|
-
|
|
|
-function recurseChildren(parentPath: string, menus: IntergrationMenu[]) {
|
|
|
- menus.forEach(item => {
|
|
|
- if (item.children && item.children.length > 0) {
|
|
|
- recurseChildren(item.path, item.children);
|
|
|
- } else {
|
|
|
- const meta: { [key: string]: any } = {}
|
|
|
- for (let key in item) {
|
|
|
- if (key.startsWith("meta")) {
|
|
|
- const metaKey = capitalizeFirstLower(key.replace("meta", ""))
|
|
|
- meta[metaKey] = item[key]
|
|
|
- }
|
|
|
- }
|
|
|
- let path = item.path;
|
|
|
- if (item.pathParams?.length > 0) {
|
|
|
- const paths = item.path.split("/$")
|
|
|
- path = paths[0]
|
|
|
- }
|
|
|
-
|
|
|
- const data: RouteRecordRaw = {
|
|
|
- path: parentPath + '/' + path + item.pathParams,
|
|
|
- name: item.name,
|
|
|
- redirect: item.redirect,
|
|
|
- meta: meta,
|
|
|
- component: createNameComponent(item)
|
|
|
- };
|
|
|
- finalRouters.value.push(data)
|
|
|
- }
|
|
|
- })
|
|
|
-}
|
|
|
-
|
|
|
router.beforeEach(async (to, _from, next) => {
|
|
|
NProgress.start();
|
|
|
const hasToken = stringNotBlank(localStorage.token)
|