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
29
// 权限代码列表
vben
authored
4 years ago
30
permCodeList: string[] | number[];
31
// Whether the route has been dynamically added
32
// 路由是否动态添加
Vben
authored
4 years ago
33
isDynamicAddedRoute: boolean;
vben
authored
5 years ago
34
// To trigger a menu update
35
// 触发菜单更新
Vben
authored
4 years ago
36
lastBuildMenuTime: number;
vben
authored
5 years ago
37
// Backstage menu list
38
// 后台菜单列表
Vben
authored
4 years ago
39
backMenuList: Menu[];
40
// 菜单列表
vben
authored
4 years ago
41
frontMenuList: Menu[];
Vben
authored
4 years ago
42
}
43
Vben
authored
4 years ago
44
45
46
export const usePermissionStore = defineStore({
id: 'app-permission',
state: (): PermissionState => ({
47
// 权限代码列表
Vben
authored
4 years ago
48
49
permCodeList: [],
// Whether the route has been dynamically added
50
// 路由是否动态添加
Vben
authored
4 years ago
51
52
isDynamicAddedRoute: false,
// To trigger a menu update
53
// 触发菜单更新
Vben
authored
4 years ago
54
55
lastBuildMenuTime: 0,
// Backstage menu list
56
// 后台菜单列表
Vben
authored
4 years ago
57
backMenuList: [],
vben
authored
4 years ago
58
// menu List
59
// 菜单列表
vben
authored
4 years ago
60
frontMenuList: [],
Vben
authored
4 years ago
61
62
}),
getters: {
63
64
getPermCodeList(state): string[] | number[] {
return state.permCodeList;
Vben
authored
4 years ago
65
},
66
67
getBackMenuList(state): Menu[] {
return state.backMenuList;
Vben
authored
4 years ago
68
},
69
70
getFrontMenuList(state): Menu[] {
return state.frontMenuList;
vben
authored
4 years ago
71
},
72
73
getLastBuildMenuTime(state): number {
return state.lastBuildMenuTime;
Vben
authored
4 years ago
74
},
75
76
getIsDynamicAddedRoute(state): boolean {
return state.isDynamicAddedRoute;
Vben
authored
4 years ago
77
78
79
80
81
82
83
84
85
},
},
actions: {
setPermCodeList(codeList: string[]) {
this.permCodeList = codeList;
},
setBackMenuList(list: Menu[]) {
this.backMenuList = list;
86
list?.length > 0 && this.setLastBuildMenuTime();
Vben
authored
4 years ago
87
88
},
vben
authored
4 years ago
89
90
91
92
setFrontMenuList(list: Menu[]) {
this.frontMenuList = list;
},
Vben
authored
4 years ago
93
94
95
96
97
98
99
100
101
102
103
104
105
setLastBuildMenuTime() {
this.lastBuildMenuTime = new Date().getTime();
},
setDynamicAddedRoute(added: boolean) {
this.isDynamicAddedRoute = added;
},
resetState(): void {
this.isDynamicAddedRoute = false;
this.permCodeList = [];
this.backMenuList = [];
this.lastBuildMenuTime = 0;
},
106
107
async changePermissionCode() {
const codeList = await getPermCode();
Vben
authored
4 years ago
108
109
this.setPermCodeList(codeList);
},
110
111
// 构建路由
112
async buildRoutesAction(): Promise<AppRouteRecordRaw[]> {
Vben
authored
4 years ago
113
114
const { t } = useI18n();
const userStore = useUserStore();
sanmu
authored
2 years ago
115
116
117
118
119
console.log(
'%c [ userStore ]-115',
'font-size:13px; background:pink; color:#bf2c9f;',
userStore,
);
vben
authored
4 years ago
120
const appStore = useAppStoreWithOut();
Vben
authored
4 years ago
121
122
let routes: AppRouteRecordRaw[] = [];
vben
authored
4 years ago
123
const roleList = toRaw(userStore.getRoleList) || [];
sanmu
authored
2 years ago
124
125
126
127
// dddd
// const { permissionMode = projectSetting.permissionMode } = appStore.getProjectConfig;
const permissionMode = projectSetting.permissionMode;
// const permissionMode = PermissionModeEnum.ROUTE_MAPPING;
128
// 路由过滤器 在 函数filter 作为回调传入遍历使用
vben
authored
4 years ago
129
130
const routeFilter = (route: AppRouteRecordRaw) => {
const { meta } = route;
131
// 抽出角色
vben
authored
4 years ago
132
133
const { roles } = meta || {};
if (!roles) return true;
134
// 进行角色权限判断
sanmu
authored
2 years ago
135
const user = userStore.getUserInfo;
sanmu
authored
2 years ago
136
return roles.includes(user?.roleSmallVO.code);
sanmu
authored
2 years ago
137
// return roleList.some((role) => roles.includes(role));
vben
authored
4 years ago
138
139
};
140
const routeRemoveIgnoreFilter = (route: AppRouteRecordRaw) => {
141
const { meta } = route;
142
// ignoreRoute 为true 则路由仅用于菜单生成,不会在实际的路由表中出现
143
const { ignoreRoute } = meta || {};
144
// arr.filter 返回 true 表示该元素通过测试
145
146
147
return !ignoreRoute;
};
148
149
150
151
152
/**
* @description 根据设置的首页path,修正routes中的affix标记(固定首页)
* */
const patchHomeAffix = (routes: AppRouteRecordRaw[]) => {
if (!routes || routes.length === 0) return;
153
let homePath: string = userStore.getUserInfo.homePath || PageEnum.BASE_HOME;
154
155
156
157
function patcher(routes: AppRouteRecordRaw[], parentPath = '') {
if (parentPath) parentPath = parentPath + '/';
routes.forEach((route: AppRouteRecordRaw) => {
158
const { path, children, redirect } = route;
159
160
const currentPath = path.startsWith('/') ? path : parentPath + path;
if (currentPath === homePath) {
161
162
163
164
165
166
if (redirect) {
homePath = route.redirect! as string;
} else {
route.meta = Object.assign({}, route.meta, { affix: true });
throw new Error('end');
}
167
168
169
170
}
children && children.length > 0 && patcher(children, currentPath);
});
}
171
172
173
174
175
176
177
178
try {
patcher(routes);
} catch (e) {
// 已处理完毕跳出循环
}
return;
};
vben
authored
4 years ago
179
switch (permissionMode) {
180
// 角色权限
vben
authored
4 years ago
181
case PermissionModeEnum.ROLE:
182
// 对非一级路由进行过滤
vben
authored
4 years ago
183
routes = filter(asyncRoutes, routeFilter);
184
// 对一级路由根据角色权限过滤
vben
authored
4 years ago
185
186
routes = routes.filter(routeFilter);
// Convert multi-level routing to level 2 routing
187
// 将多级路由转换为 2 级路由
vben
authored
4 years ago
188
routes = flatMultiLevelRoutes(routes);
sanmu
authored
2 years ago
189
190
routes = asyncRoutes;
vben
authored
4 years ago
191
192
break;
193
// 路由映射, 默认进入该case
vben
authored
4 years ago
194
case PermissionModeEnum.ROUTE_MAPPING:
195
// 对非一级路由进行过滤
vben
authored
4 years ago
196
routes = filter(asyncRoutes, routeFilter);
197
// 对一级路由再次根据角色权限过滤
vben
authored
4 years ago
198
routes = routes.filter(routeFilter);
199
// 将路由转换成菜单
Eric
authored
4 years ago
200
const menuList = transformRouteToMenu(routes, true);
201
// 移除掉 ignoreRoute: true 的路由 非一级路由
202
routes = filter(routes, routeRemoveIgnoreFilter);
203
// 移除掉 ignoreRoute: true 的路由 一级路由;
204
routes = routes.filter(routeRemoveIgnoreFilter);
sanmu
authored
2 years ago
205
206
// 对菜单进行排序
vben
authored
4 years ago
207
208
209
menuList.sort((a, b) => {
return (a.meta?.orderNo || 0) - (b.meta?.orderNo || 0);
});
Vben
authored
4 years ago
210
211
// 设置菜单列表
vben
authored
4 years ago
212
this.setFrontMenuList(menuList);
213
vben
authored
4 years ago
214
// Convert multi-level routing to level 2 routing
215
// 将多级路由转换为 2 级路由
vben
authored
4 years ago
216
217
218
routes = flatMultiLevelRoutes(routes);
break;
Vben
authored
4 years ago
219
// If you are sure that you do not need to do background dynamic permissions, please comment the entire judgment below
220
// 如果确定不需要做后台动态权限,请在下方注释整个判断
vben
authored
4 years ago
221
222
223
224
225
226
227
228
229
case PermissionModeEnum.BACK:
const { createMessage } = useMessage();
createMessage.loading({
content: t('sys.app.menuLoading'),
duration: 1,
});
// !Simulate to obtain permission codes from the background,
230
// 模拟从后台获取权限码,
vben
authored
4 years ago
231
// this function may only need to be executed once, and the actual project can be put at the right time by itself
232
// 这个功能可能只需要执行一次,实际项目可以自己放在合适的时间
vben
authored
4 years ago
233
234
let routeList: AppRouteRecordRaw[] = [];
try {
235
await this.changePermissionCode();
vben
authored
4 years ago
236
237
238
239
240
241
routeList = (await getMenuList()) as AppRouteRecordRaw[];
} catch (error) {
console.error(error);
}
// Dynamically introduce components
242
// 动态引入组件
vben
authored
4 years ago
243
244
245
routeList = transformObjToRoute(routeList);
// Background routing to menu structure
246
// 后台路由到菜单结构
vben
authored
4 years ago
247
248
249
const backMenuList = transformRouteToMenu(routeList);
this.setBackMenuList(backMenuList);
250
// remove meta.ignoreRoute item
251
// 删除 meta.ignoreRoute 项
252
253
routeList = filter(routeList, routeRemoveIgnoreFilter);
routeList = routeList.filter(routeRemoveIgnoreFilter);
254
vben
authored
4 years ago
255
256
257
routeList = flatMultiLevelRoutes(routeList);
routes = [PAGE_NOT_FOUND_ROUTE, ...routeList];
break;
258
}
vben
authored
4 years ago
259
Vben
authored
4 years ago
260
routes.push(ERROR_LOG_ROUTE);
261
patchHomeAffix(routes);
Vben
authored
4 years ago
262
263
264
265
266
267
return routes;
},
},
});
// Need to be used outside the setup
268
// 需要在设置之外使用
vben
authored
4 years ago
269
export function usePermissionStoreWithOut() {
Vben
authored
4 years ago
270
return usePermissionStore(store);
271
}