permission.ts
3.98 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import type { AppRouteRecordRaw, Menu } from '/@/router/types';
import store from '/@/store/index';
import { hotModuleUnregisterModule } from '/@/utils/helper/vuexHelper';
import { VuexModule, Mutation, Module, getModule, Action } from 'vuex-module-decorators';
import { PermissionModeEnum } from '/@/enums/appEnum';
import { appStore } from '/@/store/modules/app';
import { userStore } from '/@/store/modules/user';
import { asyncRoutes } from '/@/router/routes';
import { filter } from '/@/utils/helper/treeHelper';
import { toRaw } from 'vue';
import { getMenuListById } from '/@/api/sys/menu';
import { transformObjToRoute } from '/@/router/helper/routeHelper';
import { transformRouteToMenu } from '/@/router/helper/menuHelper';
import { useMessage } from '/@/hooks/web/useMessage';
// import { useI18n } from '/@/hooks/web/useI18n';
import { ERROR_LOG_ROUTE, PAGE_NOT_FOUND_ROUTE } from '/@/router/constant';
const { createMessage } = useMessage();
const NAME = 'app-permission';
hotModuleUnregisterModule(NAME);
@Module({ dynamic: true, namespaced: true, store, name: NAME })
class Permission extends VuexModule {
// Permission code list
private permCodeListState: string[] = [];
// Whether the route has been dynamically added
private isDynamicAddedRouteState = false;
// To trigger a menu update
private lastBuildMenuTimeState = 0;
// Backstage menu list
private backMenuListState: Menu[] = [];
get getPermCodeListState() {
return this.permCodeListState;
}
get getBackMenuListState() {
return this.backMenuListState;
}
get getLastBuildMenuTimeState() {
return this.lastBuildMenuTimeState;
}
get getIsDynamicAddedRouteState() {
return this.isDynamicAddedRouteState;
}
@Mutation
commitPermCodeListState(codeList: string[]): void {
this.permCodeListState = codeList;
}
@Mutation
commitBackMenuListState(list: Menu[]): void {
this.backMenuListState = list;
}
@Mutation
commitLastBuildMenuTimeState(): void {
this.lastBuildMenuTimeState = new Date().getTime();
}
@Mutation
commitDynamicAddedRouteState(added: boolean): void {
this.isDynamicAddedRouteState = added;
}
@Mutation
commitResetState(): void {
this.isDynamicAddedRouteState = false;
this.permCodeListState = [];
this.backMenuListState = [];
this.lastBuildMenuTimeState = 0;
}
@Action
async buildRoutesAction(id?: number | string): Promise<AppRouteRecordRaw[]> {
// const { t } = useI18n();
let routes: AppRouteRecordRaw[] = [];
const roleList = toRaw(userStore.getRoleListState);
const { permissionMode = PermissionModeEnum.ROLE } = appStore.getProjectConfig;
// role permissions
if (permissionMode === PermissionModeEnum.ROLE) {
routes = filter(asyncRoutes, (route) => {
const { meta } = route as AppRouteRecordRaw;
const { roles } = meta || {};
if (!roles) return true;
return roleList.some((role) => roles.includes(role));
});
// If you are sure that you do not need to do background dynamic permissions, please comment the entire judgment below
} else if (permissionMode === PermissionModeEnum.BACK) {
createMessage.loading({
content: 'Loading menu...',
// content: 't('sys.app.menuLoading')',
duration: 1,
});
// Here to get the background routing menu logic to modify by yourself
const paramId = id || userStore.getUserInfoState.userId;
if (!paramId) {
throw new Error('paramId is undefined!');
}
let routeList = (await getMenuListById({ id: paramId })) as AppRouteRecordRaw[];
// Dynamically introduce components
routeList = transformObjToRoute(routeList);
// Background routing to menu structure
const backMenuList = transformRouteToMenu(routeList);
this.commitBackMenuListState(backMenuList);
routes = [PAGE_NOT_FOUND_ROUTE, ...routeList];
}
routes.push(ERROR_LOG_ROUTE);
return routes;
}
}
export const permissionStore = getModule<Permission>(Permission);