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
| import { defineStore } from 'pinia';
| import { store } from '@/store';
| // import { RouteConfigs } from "@/layout/types";
| import { constantRoutes, Route } from '@/router';
| import { addAsyncRoutes, ascending } from '@/router/utils';
| import * as menuServices from '@/services/api/menu';
|
| export interface PermissonState {
| routes: Route[];
| addRoutes: Route[];
| }
|
| export const usePermissionStore = defineStore({
| id: 'permission',
| state: (): PermissonState => ({
| // 静态路由生成的菜单
| routes: [],
| // 动态路由
| addRoutes: [],
| }),
| getters: {
| permission_routes(state) {
| return state.routes;
| },
| },
| actions: {
| getModuleList() {
| return new Promise<Route[]>(async (resolve, reject) => {
| try {
| const res = await menuServices.getMenus(
| {
| userType: EnumUserType.Operation,
| clientType: EnumClientType.PcWeb,
| },
| {
| showLoading: false,
| }
| );
| const accessedRoutes = ascending(addAsyncRoutes(formatModuleList(res))); // createAccessedRoutes(userMenuList, userInfo)
| //@ts-ignore
| accessedRoutes.push({ path: '/:pathMatch(.*)*', redirect: '/404', hidden: true });
|
| this.addRoutes = accessedRoutes;
| this.routes = constantRoutes.concat(accessedRoutes);
|
| resolve(accessedRoutes);
| } catch (e) {
| reject(e);
| }
| });
| },
| // 清空缓存页面
| resetModuleList() {
| this.addRoutes = [];
| this.routes = [];
| },
| },
| });
|
| export function usePermissionStoreHook() {
| return usePermissionStore(store);
| }
|
| function formatModuleList(moduleList: API.GetMenusQueryResultItem[]) {
| return moduleList.map((item) => {
| const isRootModule = !item.parentId && item.type == EnumMenuType.Menu;
| //@ts-ignore
| let asyncRoute: Route = isRootModule
| ? {
| path: formatPath(item.url),
| name: item.code,
| redirect: 'noRedirect',
| alwaysShow: true,
| hidden: item.type != EnumMenuType.Menu,
| meta: {
| title: item.name,
| icon: item.icon,
| moduleId: item.id,
| rank: item?.sort,
| rootMenu: true,
| },
| }
| : {
| path: formatPath(item.url),
| name: item.code,
| hidden: item.type != EnumMenuType.Menu,
| alwaysShow: true,
| meta: {
| title: item.name,
| moduleId: item.id,
| noCache: !item.isCache,
| rank: item?.sort,
| },
| };
| const children = formatModuleList(item.children);
| asyncRoute.children = children;
| return asyncRoute;
| });
| }
|
| function formatPath(path: string) {
| return path.startsWith('/') ? path : '/' + path;
| }
|
|