提交 | 用户 | 时间
|
820397
|
1 |
import { defineStore } from 'pinia' |
H |
2 |
import { store } from '@/store' |
|
3 |
import { cloneDeep } from 'lodash-es' |
|
4 |
import remainingRouter from '@/router/modules/remaining' |
|
5 |
import { flatMultiLevelRoutes, generateRoute } from '@/utils/routerHelper' |
271781
|
6 |
import {CACHE_KEY, useSessionCache} from '@/hooks/web/useCache' |
820397
|
7 |
|
271781
|
8 |
const { wsSessionCache } = useSessionCache() |
820397
|
9 |
|
H |
10 |
export interface PermissionState { |
|
11 |
routers: AppRouteRecordRaw[] |
|
12 |
addRouters: AppRouteRecordRaw[] |
|
13 |
menuTabRouters: AppRouteRecordRaw[] |
|
14 |
} |
|
15 |
|
|
16 |
export const usePermissionStore = defineStore('permission', { |
|
17 |
state: (): PermissionState => ({ |
|
18 |
routers: [], |
|
19 |
addRouters: [], |
|
20 |
menuTabRouters: [] |
|
21 |
}), |
|
22 |
getters: { |
|
23 |
getRouters(): AppRouteRecordRaw[] { |
|
24 |
return this.routers |
|
25 |
}, |
|
26 |
getAddRouters(): AppRouteRecordRaw[] { |
|
27 |
return flatMultiLevelRoutes(cloneDeep(this.addRouters)) |
|
28 |
}, |
|
29 |
getMenuTabRouters(): AppRouteRecordRaw[] { |
|
30 |
return this.menuTabRouters |
|
31 |
} |
|
32 |
}, |
|
33 |
actions: { |
|
34 |
async generateRoutes(): Promise<unknown> { |
|
35 |
return new Promise<void>(async (resolve) => { |
|
36 |
// 获得菜单列表,它在登录的时候,setUserInfoAction 方法中已经进行获取 |
|
37 |
let res: AppCustomRouteRecordRaw[] = [] |
3e359e
|
38 |
const roleRouters = wsSessionCache.get(CACHE_KEY.ROLE_ROUTERS) |
H |
39 |
if (roleRouters) { |
|
40 |
res = roleRouters as AppCustomRouteRecordRaw[] |
820397
|
41 |
} |
H |
42 |
const routerMap: AppRouteRecordRaw[] = generateRoute(res) |
|
43 |
// 动态路由,404一定要放到最后面 |
3e359e
|
44 |
// preschooler:vue-router@4以后已支持静态404路由,此处可不再追加 |
820397
|
45 |
this.addRouters = routerMap.concat([ |
H |
46 |
{ |
|
47 |
path: '/:path(.*)*', |
3e359e
|
48 |
// redirect: '/404', |
H |
49 |
component: () => import('@/views/Error/404.vue'), |
820397
|
50 |
name: '404Page', |
H |
51 |
meta: { |
|
52 |
hidden: true, |
|
53 |
breadcrumb: false |
|
54 |
} |
|
55 |
} |
|
56 |
]) |
|
57 |
// 渲染菜单的所有路由 |
|
58 |
this.routers = cloneDeep(remainingRouter).concat(routerMap) |
|
59 |
resolve() |
|
60 |
}) |
|
61 |
}, |
|
62 |
setMenuTabRouters(routers: AppRouteRecordRaw[]): void { |
|
63 |
this.menuTabRouters = routers |
|
64 |
} |
|
65 |
}, |
|
66 |
persist: false |
|
67 |
}) |
|
68 |
|
|
69 |
export const usePermissionStoreWithOut = () => { |
|
70 |
return usePermissionStore(store) |
|
71 |
} |