forked from Tencent/tdesign-vue-next-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpermission-fe.ts
More file actions
65 lines (60 loc) · 2.17 KB
/
permission-fe.ts
File metadata and controls
65 lines (60 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// 前端 roles 控制菜单权限 通过登录后的角色对菜单就行过滤处理
// 如果需要前端 roles 控制菜单权限 请使用此文件代码替换 permission.ts 的内容
import cloneDeep from 'lodash/cloneDeep';
import { defineStore } from 'pinia';
import type { RouteRecordRaw } from 'vue-router';
import router, { fixedRouterList, homepageRouterList } from '@/router';
import { store } from '@/store';
// 严格模式 true: 路由无roleCode不可访问 false: 路由无roleCode可访问
const CHECK_ROLE_STRICT = false;
function filterPermissionsRouters(routes: Array<RouteRecordRaw>, roles: Array<string>): Array<RouteRecordRaw> {
if (routes.length === 0) return [];
const accessedRouters: Array<RouteRecordRaw> = [];
routes.forEach((route) => {
const roleCode = route.meta?.roleCode;
const hasPermission = CHECK_ROLE_STRICT ? roles.includes(roleCode) : !roleCode || roles.includes(roleCode);
if (!hasPermission) {
return;
}
if (!route.children || route.children.length === 0) {
accessedRouters.push(route);
return;
}
const accessedChildren = filterPermissionsRouters(route.children, roles);
route.children = accessedChildren;
accessedRouters.push(route);
});
return accessedRouters;
}
const removeRouteFnSet = new Set<() => void>();
export const usePermissionStore = defineStore('permission', {
state: () => ({
routers: [],
}),
actions: {
async initRoutes(roles: Array<string>) {
let accessedRouters: Array<RouteRecordRaw> = [];
const allRoutes = cloneDeep([...homepageRouterList, ...fixedRouterList]);
// special token
if (roles.includes('all')) {
accessedRouters = allRoutes;
} else {
accessedRouters = filterPermissionsRouters(allRoutes, roles);
}
for (const route of accessedRouters) {
removeRouteFnSet.add(router.addRoute(route));
}
this.routers = accessedRouters;
},
async restoreRoutes() {
for (const removeRoute of removeRouteFnSet) {
removeRoute();
}
removeRouteFnSet.clear();
this.routers = [];
},
},
});
export function getPermissionStore() {
return usePermissionStore(store);
}