1
2
import type { AppRouteRecordRaw, Menu } from '/@/router/types';
Vben
authored
4 years ago
3
4
5
6
import { defineStore } from 'pinia';
import { store } from '/@/store';
import { useI18n } from '/@/hooks/web/useI18n';
import { useUserStore } from './user';
vben
authored
4 years ago
7
import { useAppStoreWithOut } from './app';
Vben
authored
4 years ago
8
import { toRaw } from 'vue';
Vben
authored
4 years ago
9
10
import { transformObjToRoute, flatMultiLevelRoutes } from '/@/router/helper/routeHelper';
import { transformRouteToMenu } from '/@/router/helper/menuHelper';
11
Vben
authored
4 years ago
12
import projectSetting from '/@/settings/projectSetting';
Vben
authored
4 years ago
13
14
15
import { PermissionModeEnum } from '/@/enums/appEnum';
vben
authored
5 years ago
16
import { asyncRoutes } from '/@/router/routes';
Vben
authored
4 years ago
17
18
import { ERROR_LOG_ROUTE, PAGE_NOT_FOUND_ROUTE } from '/@/router/routes/basic';
19
import { filter } from '/@/utils/helper/treeHelper';
Vben
authored
4 years ago
20
21
22
import { getMenuList } from '/@/api/sys/menu';
import { getPermCode } from '/@/api/sys/user';
23
24
import { useMessage } from '/@/hooks/web/useMessage';
25
import { PageEnum } from '/@/enums/pageEnum';
vben
authored
5 years ago
26
Vben
authored
4 years ago
27
interface PermissionState {
vben
authored
5 years ago
28
// Permission code list
vben
authored
4 years ago
29
permCodeList: string[] | number[];
30
// Whether the route has been dynamically added
Vben
authored
4 years ago
31
isDynamicAddedRoute: boolean;
vben
authored
5 years ago
32
// To trigger a menu update
Vben
authored
4 years ago
33
lastBuildMenuTime: number;
vben
authored
5 years ago
34
// Backstage menu list
Vben
authored
4 years ago
35
backMenuList: Menu[];
vben
authored
4 years ago
36
frontMenuList: Menu[];
Vben
authored
4 years ago
37
38
39
40
41
42
43
44
45
46
47
}
export const usePermissionStore = defineStore({
id: 'app-permission',
state: (): PermissionState => ({
permCodeList: [],
// Whether the route has been dynamically added
isDynamicAddedRoute: false,
// To trigger a menu update
lastBuildMenuTime: 0,
// Backstage menu list
backMenuList: [],
vben
authored
4 years ago
48
49
// menu List
frontMenuList: [],
Vben
authored
4 years ago
50
51
}),
getters: {
vben
authored
4 years ago
52
getPermCodeList(): string[] | number[] {
Vben
authored
4 years ago
53
54
return this.permCodeList;
},
vben
authored
4 years ago
55
getBackMenuList(): Menu[] {
Vben
authored
4 years ago
56
57
return this.backMenuList;
},
vben
authored
4 years ago
58
59
60
getFrontMenuList(): Menu[] {
return this.frontMenuList;
},
vben
authored
4 years ago
61
getLastBuildMenuTime(): number {
Vben
authored
4 years ago
62
63
return this.lastBuildMenuTime;
},
vben
authored
4 years ago
64
getIsDynamicAddedRoute(): boolean {
Vben
authored
4 years ago
65
66
67
68
69
70
71
72
73
74
return this.isDynamicAddedRoute;
},
},
actions: {
setPermCodeList(codeList: string[]) {
this.permCodeList = codeList;
},
setBackMenuList(list: Menu[]) {
this.backMenuList = list;
75
list?.length > 0 && this.setLastBuildMenuTime();
Vben
authored
4 years ago
76
77
},
vben
authored
4 years ago
78
79
80
81
setFrontMenuList(list: Menu[]) {
this.frontMenuList = list;
},
Vben
authored
4 years ago
82
83
84
85
86
87
88
89
90
91
92
93
94
setLastBuildMenuTime() {
this.lastBuildMenuTime = new Date().getTime();
},
setDynamicAddedRoute(added: boolean) {
this.isDynamicAddedRoute = added;
},
resetState(): void {
this.isDynamicAddedRoute = false;
this.permCodeList = [];
this.backMenuList = [];
this.lastBuildMenuTime = 0;
},
95
96
async changePermissionCode() {
const codeList = await getPermCode();
Vben
authored
4 years ago
97
98
this.setPermCodeList(codeList);
},
99
async buildRoutesAction(): Promise<AppRouteRecordRaw[]> {
Vben
authored
4 years ago
100
101
const { t } = useI18n();
const userStore = useUserStore();
vben
authored
4 years ago
102
const appStore = useAppStoreWithOut();
Vben
authored
4 years ago
103
104
let routes: AppRouteRecordRaw[] = [];
vben
authored
4 years ago
105
const roleList = toRaw(userStore.getRoleList) || [];
Vben
authored
4 years ago
106
const { permissionMode = projectSetting.permissionMode } = appStore.getProjectConfig;
vben
authored
4 years ago
107
108
109
110
111
112
113
114
const routeFilter = (route: AppRouteRecordRaw) => {
const { meta } = route;
const { roles } = meta || {};
if (!roles) return true;
return roleList.some((role) => roles.includes(role));
};
115
116
117
118
119
120
const routeRmoveIgnoreFilter = (route: AppRouteRecordRaw) => {
const { meta } = route;
const { ignoreRoute } = meta || {};
return !ignoreRoute;
};
121
122
123
124
125
/**
* @description 根据设置的首页path,修正routes中的affix标记(固定首页)
* */
const patchHomeAffix = (routes: AppRouteRecordRaw[]) => {
if (!routes || routes.length === 0) return;
126
let homePath: string = userStore.getUserInfo.homePath || PageEnum.BASE_HOME;
127
128
129
function patcher(routes: AppRouteRecordRaw[], parentPath = '') {
if (parentPath) parentPath = parentPath + '/';
routes.forEach((route: AppRouteRecordRaw) => {
130
const { path, children, redirect } = route;
131
132
const currentPath = path.startsWith('/') ? path : parentPath + path;
if (currentPath === homePath) {
133
134
135
136
137
138
if (redirect) {
homePath = route.redirect! as string;
} else {
route.meta = Object.assign({}, route.meta, { affix: true });
throw new Error('end');
}
139
140
141
142
143
144
145
146
147
148
149
150
}
children && children.length > 0 && patcher(children, currentPath);
});
}
try {
patcher(routes);
} catch (e) {
// 已处理完毕跳出循环
}
return;
};
vben
authored
4 years ago
151
152
153
154
155
156
157
158
159
160
161
switch (permissionMode) {
case PermissionModeEnum.ROLE:
routes = filter(asyncRoutes, routeFilter);
routes = routes.filter(routeFilter);
// Convert multi-level routing to level 2 routing
routes = flatMultiLevelRoutes(routes);
break;
case PermissionModeEnum.ROUTE_MAPPING:
routes = filter(asyncRoutes, routeFilter);
routes = routes.filter(routeFilter);
Eric
authored
4 years ago
162
const menuList = transformRouteToMenu(routes, true);
163
164
routes = filter(routes, routeRmoveIgnoreFilter);
routes = routes.filter(routeRmoveIgnoreFilter);
vben
authored
4 years ago
165
166
167
menuList.sort((a, b) => {
return (a.meta?.orderNo || 0) - (b.meta?.orderNo || 0);
});
Vben
authored
4 years ago
168
vben
authored
4 years ago
169
170
171
172
173
this.setFrontMenuList(menuList);
// Convert multi-level routing to level 2 routing
routes = flatMultiLevelRoutes(routes);
break;
Vben
authored
4 years ago
174
// If you are sure that you do not need to do background dynamic permissions, please comment the entire judgment below
vben
authored
4 years ago
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
case PermissionModeEnum.BACK:
const { createMessage } = useMessage();
createMessage.loading({
content: t('sys.app.menuLoading'),
duration: 1,
});
// !Simulate to obtain permission codes from the background,
// this function may only need to be executed once, and the actual project can be put at the right time by itself
let routeList: AppRouteRecordRaw[] = [];
try {
this.changePermissionCode();
routeList = (await getMenuList()) as AppRouteRecordRaw[];
} catch (error) {
console.error(error);
}
// Dynamically introduce components
routeList = transformObjToRoute(routeList);
// Background routing to menu structure
const backMenuList = transformRouteToMenu(routeList);
this.setBackMenuList(backMenuList);
200
201
202
203
// remove meta.ignoreRoute item
routeList = filter(routeList, routeRmoveIgnoreFilter);
routeList = routeList.filter(routeRmoveIgnoreFilter);
vben
authored
4 years ago
204
205
206
routeList = flatMultiLevelRoutes(routeList);
routes = [PAGE_NOT_FOUND_ROUTE, ...routeList];
break;
207
}
vben
authored
4 years ago
208
Vben
authored
4 years ago
209
routes.push(ERROR_LOG_ROUTE);
210
patchHomeAffix(routes);
Vben
authored
4 years ago
211
212
213
214
215
216
return routes;
},
},
});
// Need to be used outside the setup
vben
authored
4 years ago
217
export function usePermissionStoreWithOut() {
Vben
authored
4 years ago
218
return usePermissionStore(store);
219
}