vben
authored
|
1
|
import type { Menu, MenuModule } from '/@/router/types';
|
|
2
|
import type { RouteRecordNormalized } from 'vue-router';
|
vben
authored
|
3
|
|
Vben
authored
|
4
5
|
import { useAppStoreWidthOut } from '/@/store/modules/app';
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';
|
|
9
10
|
import router from '/@/router';
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
|
const isBackMode = () => {
|
Vben
authored
|
27
|
const appStore = useAppStoreWidthOut();
|
vben
authored
|
28
29
|
return appStore.getProjectConfig.permissionMode === PermissionModeEnum.BACK;
};
|
|
30
31
32
33
34
35
|
const staticMenus: Menu[] = [];
(() => {
menuModules.sort((a, b) => {
return (a.orderNo || 0) - (b.orderNo || 0);
});
|
vben
authored
|
36
|
|
|
37
38
39
40
41
42
|
for (const menu of menuModules) {
staticMenus.push(transformMenuModule(menu));
}
})();
async function getAsyncMenus() {
|
Vben
authored
|
43
44
|
const permissionStore = usePermissionStore();
return !isBackMode() ? staticMenus : permissionStore.getBackMenuList;
|
|
45
46
|
}
|
vben
authored
|
47
|
export const getMenus = async (): Promise<Menu[]> => {
|
|
48
49
|
const menus = await getAsyncMenus();
const routes = router.getRoutes();
|
Vben
authored
|
50
|
|
|
51
52
53
54
55
|
return !isBackMode() ? filter(menus, basicFilter(routes)) : menus;
};
export async function getCurrentParentPath(currentPath: string) {
const menus = await getAsyncMenus();
|
vben
authored
|
56
|
|
|
57
|
const allParentPath = await getAllParentPath(menus, currentPath);
|
vben
authored
|
58
|
|
vben
authored
|
59
|
return allParentPath?.[0];
|
|
60
61
|
}
|
Vben
authored
|
62
|
// Get the level 1 menu, delete children
|
vben
authored
|
63
|
export async function getShallowMenus(): Promise<Menu[]> {
|
|
64
65
66
67
68
69
|
const menus = await getAsyncMenus();
const routes = router.getRoutes();
const shallowMenuList = menus.map((item) => ({ ...item, children: undefined }));
return !isBackMode() ? shallowMenuList.filter(basicFilter(routes)) : shallowMenuList;
}
|
Vben
authored
|
70
|
// Get the children of the menu
|
|
71
|
export async function getChildrenMenus(parentPath: string) {
|
Vben
authored
|
72
|
const menus = await getMenus();
|
|
73
|
const parent = menus.find((item) => item.path === parentPath);
|
Vben
authored
|
74
|
if (!parent || !parent.children || !!parent?.meta?.hideChildrenInMenu) return [] as Menu[];
|
vben
authored
|
75
76
77
|
const routes = router.getRoutes();
return !isBackMode() ? filter(parent.children, basicFilter(routes)) : parent.children;
|
|
78
79
80
81
|
}
function basicFilter(routes: RouteRecordNormalized[]) {
return (menu: Menu) => {
|
vben
authored
|
82
|
const matchRoute = routes.find((route) => {
|
Vben
authored
|
83
|
if (isUrl(menu.path)) return true;
|
vben
authored
|
84
|
|
vben
authored
|
85
86
|
if (route.meta?.carryParam) {
return pathToRegexp(route.path).test(menu.path);
|
vben
authored
|
87
|
}
|
vben
authored
|
88
89
90
91
|
const isSame = route.path === menu.path;
if (!isSame) return false;
if (route.meta?.ignoreAuth) return true;
|
vben
authored
|
92
|
|
vben
authored
|
93
|
return isSame || pathToRegexp(route.path).test(menu.path);
|
vben
authored
|
94
95
96
|
});
if (!matchRoute) return false;
|
Vben
authored
|
97
|
menu.icon = (menu.icon || matchRoute.meta.icon) as string;
|
|
98
99
100
101
|
menu.meta = matchRoute.meta;
return true;
};
}
|