Skip to content

Commit 264607f

Browse files
perf: Display menu based on role
1 parent ef8fd8f commit 264607f

File tree

3 files changed

+94
-2
lines changed

3 files changed

+94
-2
lines changed

frontend/src/router/dynamic.ts

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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+
}

frontend/src/router/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,10 @@ export const routes = [
6464
},
6565
],
6666
},
67-
{
67+
/* {
6868
path: '/ds',
6969
component: LayoutDsl,
70+
name: 'ds-menu',
7071
redirect: '/ds/index',
7172
children: [
7273
{
@@ -76,7 +77,7 @@ export const routes = [
7677
meta: { title: t('menu.Data Connections'), iconActive: 'ds', iconDeActive: 'noDs' },
7778
},
7879
],
79-
},
80+
}, */
8081
{
8182
path: '/dashboard',
8283
component: LayoutDsl,

frontend/src/router/watch.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { useAppearanceStoreWithOut } from '@/stores/appearance'
44
import { useUserStore } from '@/stores/user'
55
import { request } from '@/utils/request'
66
import type { Router } from 'vue-router'
7+
import { generateDynamicRouters } from './dynamic'
78

89
const appearanceStore = useAppearanceStoreWithOut()
910
const userStore = useUserStore()
@@ -35,6 +36,7 @@ export const watchRouter = (router: Router) => {
3536
}
3637
if (!userStore.getUid) {
3738
await userStore.info()
39+
generateDynamicRouters(router)
3840
}
3941
if (to.path === '/' || accessCrossPermission(to)) {
4042
next('/chat')

0 commit comments

Comments
 (0)