vue-vben-admin/src/router/menus/index.ts

107 lines
3.4 KiB
TypeScript
Raw Normal View History

2020-10-16 22:03:44 +08:00
import type { Menu, MenuModule } from '/@/router/types';
2020-09-28 20:19:10 +08:00
import type { RouteRecordNormalized } from 'vue-router';
2020-12-15 14:59:22 +08:00
2020-09-28 20:19:10 +08:00
import { appStore } from '/@/store/modules/app';
import { permissionStore } from '/@/store/modules/permission';
2020-12-15 14:59:22 +08:00
import { transformMenuModule, getAllParentPath } from '/@/router/helper/menuHelper';
2020-09-28 20:19:10 +08:00
import { filter } from '/@/utils/helper/treeHelper';
import router from '/@/router';
import { PermissionModeEnum } from '/@/enums/appEnum';
2020-10-14 22:08:56 +08:00
import { pathToRegexp } from 'path-to-regexp';
2020-11-23 23:24:13 +08:00
2021-01-10 19:52:03 +08:00
const modules = import.meta.globEager('./modules/**/*.ts');
2020-12-15 14:59:22 +08:00
2020-10-30 21:32:05 +08:00
const menuModules: MenuModule[] = [];
2020-09-28 20:19:10 +08:00
2020-10-16 22:03:44 +08:00
Object.keys(modules).forEach((key) => {
2021-01-09 23:28:52 +08:00
const mod = modules[key].default || {};
const modList = Array.isArray(mod) ? [...mod] : [mod];
menuModules.push(...modList);
2020-10-16 22:03:44 +08:00
});
2021-01-09 23:28:52 +08:00
const reg = /(((https?:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+(?::\d+)?|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)$/;
2020-09-28 20:19:10 +08:00
// ===========================
// ==========Helper===========
// ===========================
2020-12-07 21:17:24 +08:00
const isBackMode = () => {
return appStore.getProjectConfig.permissionMode === PermissionModeEnum.BACK;
};
2020-09-28 20:19:10 +08:00
const staticMenus: Menu[] = [];
(() => {
menuModules.sort((a, b) => {
return (a.orderNo || 0) - (b.orderNo || 0);
});
2020-11-28 14:27:26 +08:00
2020-09-28 20:19:10 +08:00
for (const menu of menuModules) {
staticMenus.push(transformMenuModule(menu));
}
})();
async function getAsyncMenus() {
// 前端角色控制菜单 直接取菜单文件
2020-12-15 14:59:22 +08:00
return !isBackMode() ? staticMenus : permissionStore.getBackMenuListState;
2020-09-28 20:19:10 +08:00
}
// 获取菜单 树级
2020-12-10 23:58:11 +08:00
export const getMenus = async (): Promise<Menu[]> => {
2020-09-28 20:19:10 +08:00
const menus = await getAsyncMenus();
const routes = router.getRoutes();
return !isBackMode() ? filter(menus, basicFilter(routes)) : menus;
};
// 获取当前路径的顶级路径
export async function getCurrentParentPath(currentPath: string) {
const menus = await getAsyncMenus();
2020-09-28 20:19:10 +08:00
const allParentPath = await getAllParentPath(menus, currentPath);
2020-12-15 14:59:22 +08:00
return allParentPath?.[0];
2020-09-28 20:19:10 +08:00
}
// 获取1级菜单删除children
2020-12-10 23:58:11 +08:00
export async function getShallowMenus(): Promise<Menu[]> {
2020-09-28 20:19:10 +08:00
const menus = await getAsyncMenus();
const routes = router.getRoutes();
const shallowMenuList = menus.map((item) => ({ ...item, children: undefined }));
return !isBackMode() ? shallowMenuList.filter(basicFilter(routes)) : shallowMenuList;
}
// 获取菜单的children
export async function getChildrenMenus(parentPath: string) {
const menus = await getAsyncMenus();
const parent = menus.find((item) => item.path === parentPath);
if (!parent || !parent.children) return [] as Menu[];
const routes = router.getRoutes();
return !isBackMode() ? filter(parent.children, basicFilter(routes)) : parent.children;
2020-09-28 20:19:10 +08:00
}
// 通用过滤方法
function basicFilter(routes: RouteRecordNormalized[]) {
return (menu: Menu) => {
2020-10-14 22:08:56 +08:00
const matchRoute = routes.find((route) => {
2020-12-15 14:59:22 +08:00
const match = route.path.match(reg)?.[0];
if (match && match === menu.path) {
2020-12-07 23:56:57 +08:00
return true;
}
2020-12-15 14:59:22 +08:00
if (route.meta?.carryParam) {
return pathToRegexp(route.path).test(menu.path);
2020-10-14 22:08:56 +08:00
}
2020-12-15 14:59:22 +08:00
const isSame = route.path === menu.path;
if (!isSame) return false;
if (route.meta?.ignoreAuth) return true;
2020-12-07 23:56:57 +08:00
2020-12-15 14:59:22 +08:00
return isSame || pathToRegexp(route.path).test(menu.path);
2020-10-14 22:08:56 +08:00
});
if (!matchRoute) return false;
2021-02-28 23:05:37 +08:00
menu.icon = (menu.icon || matchRoute.meta.icon) as string;
2020-09-28 20:19:10 +08:00
menu.meta = matchRoute.meta;
return true;
};
}