vben
authored
|
1
|
import type { Menu, MenuModule } from '/@/router/types';
|
|
2
|
import type { RouteRecordNormalized } from 'vue-router';
|
vben
authored
|
3
|
|
vben
authored
|
4
|
import { useAppStoreWithOut } from '/@/store/modules/app';
|
Vben
authored
|
5
|
import { usePermissionStore } from '/@/store/modules/permission';
|
vben
authored
|
6
|
import { transformMenuModule, getAllParentPath } from '/@/router/helper/menuHelper';
|
|
7
|
import { filter } from '/@/utils/helper/treeHelper';
|
Vben
authored
|
8
|
import { isUrl } from '/@/utils/is';
|
Vben
authored
|
9
|
import { router } from '/@/router';
|
|
10
|
import { PermissionModeEnum } from '/@/enums/appEnum';
|
vben
authored
|
11
|
import { pathToRegexp } from 'path-to-regexp';
|
vben
authored
|
12
|
|
vben
authored
|
13
|
const modules = import.meta.globEager('./modules/**/*.ts');
|
vben
authored
|
14
|
|
vben
authored
|
15
|
const menuModules: MenuModule[] = [];
|
|
16
|
|
vben
authored
|
17
|
Object.keys(modules).forEach((key) => {
|
vben
authored
|
18
19
20
|
const mod = modules[key].default || {};
const modList = Array.isArray(mod) ? [...mod] : [mod];
menuModules.push(...modList);
|
vben
authored
|
21
22
|
});
|
|
23
24
25
|
// ===========================
// ==========Helper===========
// ===========================
|
vben
authored
|
26
27
28
29
30
|
const getPermissionMode = () => {
const appStore = useAppStoreWithOut();
return appStore.getProjectConfig.permissionMode;
};
|
vben
authored
|
31
|
const isBackMode = () => {
|
vben
authored
|
32
33
34
35
36
37
38
39
40
|
return getPermissionMode() === PermissionModeEnum.BACK;
};
const isRouteMappingMode = () => {
return getPermissionMode() === PermissionModeEnum.ROUTE_MAPPING;
};
const isRoleMode = () => {
return getPermissionMode() === PermissionModeEnum.ROLE;
|
vben
authored
|
41
|
};
|
|
42
43
44
45
46
47
|
const staticMenus: Menu[] = [];
(() => {
menuModules.sort((a, b) => {
return (a.orderNo || 0) - (b.orderNo || 0);
});
|
vben
authored
|
48
|
|
|
49
50
51
52
53
54
|
for (const menu of menuModules) {
staticMenus.push(transformMenuModule(menu));
}
})();
async function getAsyncMenus() {
|
Vben
authored
|
55
|
const permissionStore = usePermissionStore();
|
vben
authored
|
56
|
if (isBackMode()) {
|
|
57
|
return permissionStore.getBackMenuList.filter((item) => !item.meta?.hideMenu && !item.hideMenu);
|
vben
authored
|
58
59
60
61
62
|
}
if (isRouteMappingMode()) {
return permissionStore.getFrontMenuList.filter((item) => !item.hideMenu);
}
return staticMenus;
|
|
63
64
|
}
|
vben
authored
|
65
|
export const getMenus = async (): Promise<Menu[]> => {
|
|
66
|
const menus = await getAsyncMenus();
|
vben
authored
|
67
68
69
70
71
|
if (isRoleMode()) {
const routes = router.getRoutes();
return filter(menus, basicFilter(routes));
}
return menus;
|
|
72
73
74
75
76
|
};
export async function getCurrentParentPath(currentPath: string) {
const menus = await getAsyncMenus();
const allParentPath = await getAllParentPath(menus, currentPath);
|
vben
authored
|
77
|
return allParentPath?.[0];
|
|
78
79
|
}
|
Vben
authored
|
80
|
// Get the level 1 menu, delete children
|
vben
authored
|
81
|
export async function getShallowMenus(): Promise<Menu[]> {
|
|
82
83
|
const menus = await getAsyncMenus();
const shallowMenuList = menus.map((item) => ({ ...item, children: undefined }));
|
vben
authored
|
84
85
86
87
88
|
if (isRoleMode()) {
const routes = router.getRoutes();
return shallowMenuList.filter(basicFilter(routes));
}
return shallowMenuList;
|
|
89
90
|
}
|
Vben
authored
|
91
|
// Get the children of the menu
|
|
92
|
export async function getChildrenMenus(parentPath: string) {
|
Vben
authored
|
93
|
const menus = await getMenus();
|
|
94
|
const parent = menus.find((item) => item.path === parentPath);
|
vben
authored
|
95
96
97
98
99
100
101
102
|
if (!parent || !parent.children || !!parent?.meta?.hideChildrenInMenu) {
return [] as Menu[];
}
if (isRoleMode()) {
const routes = router.getRoutes();
return filter(parent.children, basicFilter(routes));
}
return parent.children;
|
|
103
104
105
106
|
}
function basicFilter(routes: RouteRecordNormalized[]) {
return (menu: Menu) => {
|
vben
authored
|
107
|
const matchRoute = routes.find((route) => {
|
Vben
authored
|
108
|
if (isUrl(menu.path)) return true;
|
vben
authored
|
109
|
|
vben
authored
|
110
111
|
if (route.meta?.carryParam) {
return pathToRegexp(route.path).test(menu.path);
|
vben
authored
|
112
|
}
|
vben
authored
|
113
114
115
116
|
const isSame = route.path === menu.path;
if (!isSame) return false;
if (route.meta?.ignoreAuth) return true;
|
vben
authored
|
117
|
|
vben
authored
|
118
|
return isSame || pathToRegexp(route.path).test(menu.path);
|
vben
authored
|
119
120
121
|
});
if (!matchRoute) return false;
|
Vben
authored
|
122
|
menu.icon = (menu.icon || matchRoute.meta.icon) as string;
|
|
123
124
125
126
|
menu.meta = matchRoute.meta;
return true;
};
}
|