index.ts 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import type { Menu, MenuModule } from '/@/router/types';
  2. import type { RouteRecordNormalized } from 'vue-router';
  3. import { appStore } from '/@/store/modules/app';
  4. import { permissionStore } from '/@/store/modules/permission';
  5. import { transformMenuModule, getAllParentPath } from '/@/router/helper/menuHelper';
  6. import { filter } from '/@/utils/helper/treeHelper';
  7. import { isUrl } from '/@/utils/is';
  8. import router from '/@/router';
  9. import { PermissionModeEnum } from '/@/enums/appEnum';
  10. import { pathToRegexp } from 'path-to-regexp';
  11. const modules = import.meta.globEager('./modules/**/*.ts');
  12. const menuModules: MenuModule[] = [];
  13. Object.keys(modules).forEach((key) => {
  14. const mod = modules[key].default || {};
  15. const modList = Array.isArray(mod) ? [...mod] : [mod];
  16. menuModules.push(...modList);
  17. });
  18. // ===========================
  19. // ==========Helper===========
  20. // ===========================
  21. const isBackMode = () => {
  22. return appStore.getProjectConfig.permissionMode === PermissionModeEnum.BACK;
  23. };
  24. const staticMenus: Menu[] = [];
  25. (() => {
  26. menuModules.sort((a, b) => {
  27. return (a.orderNo || 0) - (b.orderNo || 0);
  28. });
  29. for (const menu of menuModules) {
  30. staticMenus.push(transformMenuModule(menu));
  31. }
  32. })();
  33. async function getAsyncMenus() {
  34. return !isBackMode() ? staticMenus : permissionStore.getBackMenuListState;
  35. }
  36. export const getMenus = async (): Promise<Menu[]> => {
  37. const menus = await getAsyncMenus();
  38. const routes = router.getRoutes();
  39. return !isBackMode() ? filter(menus, basicFilter(routes)) : menus;
  40. };
  41. export async function getCurrentParentPath(currentPath: string) {
  42. const menus = await getAsyncMenus();
  43. const allParentPath = await getAllParentPath(menus, currentPath);
  44. return allParentPath?.[0];
  45. }
  46. // Get the level 1 menu, delete children
  47. export async function getShallowMenus(): Promise<Menu[]> {
  48. const menus = await getAsyncMenus();
  49. const routes = router.getRoutes();
  50. const shallowMenuList = menus.map((item) => ({ ...item, children: undefined }));
  51. return !isBackMode() ? shallowMenuList.filter(basicFilter(routes)) : shallowMenuList;
  52. }
  53. // Get the children of the menu
  54. export async function getChildrenMenus(parentPath: string) {
  55. const menus = await getAsyncMenus();
  56. const parent = menus.find((item) => item.path === parentPath);
  57. if (!parent || !parent.children) return [] as Menu[];
  58. const routes = router.getRoutes();
  59. return !isBackMode() ? filter(parent.children, basicFilter(routes)) : parent.children;
  60. }
  61. function basicFilter(routes: RouteRecordNormalized[]) {
  62. return (menu: Menu) => {
  63. const matchRoute = routes.find((route) => {
  64. if (isUrl(menu.path)) return true;
  65. if (route.meta?.carryParam) {
  66. return pathToRegexp(route.path).test(menu.path);
  67. }
  68. const isSame = route.path === menu.path;
  69. if (!isSame) return false;
  70. if (route.meta?.ignoreAuth) return true;
  71. return isSame || pathToRegexp(route.path).test(menu.path);
  72. });
  73. if (!matchRoute) return false;
  74. menu.icon = (menu.icon || matchRoute.meta.icon) as string;
  75. menu.meta = matchRoute.meta;
  76. return true;
  77. };
  78. }