|
| 1 | +import LayoutDsl from '@/components/layout/LayoutDsl.vue' |
| 2 | +import { i18n } from '@/i18n' |
| 3 | +import { useUserStore } from '@/stores/user' |
| 4 | +import type { Router } from 'vue-router' |
| 5 | +const userStore = useUserStore() |
| 6 | +const t = i18n.global.t |
| 7 | + |
| 8 | +const dynamicRouterList = [ |
| 9 | + { |
| 10 | + path: '/ds', |
| 11 | + component: LayoutDsl, |
| 12 | + name: 'ds-menu', |
| 13 | + redirect: '/ds/index', |
| 14 | + children: [ |
| 15 | + { |
| 16 | + path: 'index', |
| 17 | + name: 'ds', |
| 18 | + component: import('@/views/ds/index.vue'), |
| 19 | + meta: { title: t('menu.Data Connections'), iconActive: 'ds', iconDeActive: 'noDs' }, |
| 20 | + }, |
| 21 | + ], |
| 22 | + }, |
| 23 | +] as any[] |
| 24 | + |
| 25 | +const reduceRouters = (router: Router, invalid_router_name_list: string[]) => { |
| 26 | + const tree = router.getRoutes() |
| 27 | + const invalid_name_set = [] as any |
| 28 | + invalid_router_name_list.forEach((router_name: string) => { |
| 29 | + router.removeRoute(router_name) |
| 30 | + invalid_name_set.push(router_name) |
| 31 | + }) |
| 32 | + |
| 33 | + const pathsSet = new Set(invalid_router_name_list) |
| 34 | + |
| 35 | + function processNode(node: any): boolean { |
| 36 | + if (node.name && pathsSet.has(node.name)) { |
| 37 | + return true |
| 38 | + } |
| 39 | + if (node.children) { |
| 40 | + const newChildren: any[] = [] |
| 41 | + |
| 42 | + for (const child of node.children) { |
| 43 | + const shouldRemove = processNode(child) |
| 44 | + if (!shouldRemove) { |
| 45 | + newChildren.push(child) |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + if (newChildren.length > 0) { |
| 50 | + node.children = newChildren |
| 51 | + return false |
| 52 | + } else { |
| 53 | + node.children = undefined |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + return false |
| 58 | + } |
| 59 | + |
| 60 | + let i = 0 |
| 61 | + while (i < tree.length) { |
| 62 | + if (processNode(tree[i])) { |
| 63 | + tree.splice(i, 1) |
| 64 | + } else { |
| 65 | + i++ |
| 66 | + } |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +export const generateDynamicRouters = (router: Router) => { |
| 71 | + if (userStore.isAdmin || userStore.isSpaceAdmin) { |
| 72 | + dynamicRouterList.forEach((item: any) => router.addRoute(item)) |
| 73 | + } else { |
| 74 | + const router_name_list = [] as string[] |
| 75 | + const stack = [...dynamicRouterList] |
| 76 | + while (stack.length) { |
| 77 | + const item = stack.pop() |
| 78 | + if (item.name) { |
| 79 | + router_name_list.push(item.name) |
| 80 | + } |
| 81 | + if (item.children?.length) { |
| 82 | + item.children.forEach((child: any) => { |
| 83 | + stack.push(child) |
| 84 | + }) |
| 85 | + } |
| 86 | + } |
| 87 | + reduceRouters(router, router_name_list) |
| 88 | + } |
| 89 | +} |
0 commit comments