index.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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, flatMenus, getAllParentPath } from '/@/router/helper/menuHelper';
  6. import { filter } from '/@/utils/helper/treeHelper';
  7. import router from '/@/router';
  8. import { PermissionModeEnum } from '/@/enums/appEnum';
  9. import { pathToRegexp } from 'path-to-regexp';
  10. import modules from 'globby!/@/router/menus/modules/**/*.@(ts)';
  11. const menuModules: MenuModule[] = [];
  12. Object.keys(modules).forEach((key) => {
  13. const moduleItem = modules[key];
  14. const menuModule = Array.isArray(moduleItem) ? [...moduleItem] : [moduleItem];
  15. menuModules.push(...menuModule);
  16. });
  17. // ===========================
  18. // ==========Helper===========
  19. // ===========================
  20. const staticMenus: Menu[] = [];
  21. (() => {
  22. menuModules.sort((a, b) => {
  23. return (a.orderNo || 0) - (b.orderNo || 0);
  24. });
  25. for (const menu of menuModules) {
  26. staticMenus.push(transformMenuModule(menu));
  27. }
  28. })();
  29. const isBackMode = () => {
  30. return appStore.getProjectConfig.permissionMode === PermissionModeEnum.BACK;
  31. };
  32. async function getAsyncMenus() {
  33. // 前端角色控制菜单 直接取菜单文件
  34. if (!isBackMode()) {
  35. return staticMenus;
  36. }
  37. return permissionStore.getBackMenuListState;
  38. }
  39. // 获取深层扁平化菜单
  40. export const getFlatMenus = async () => {
  41. const menus = await getAsyncMenus();
  42. return flatMenus(menus);
  43. };
  44. // 获取菜单 树级
  45. export const getMenus = async () => {
  46. const menus = await getAsyncMenus();
  47. const routes = router.getRoutes();
  48. return !isBackMode() ? filter(menus, basicFilter(routes)) : menus;
  49. };
  50. // 获取当前路径的顶级路径
  51. export async function getCurrentParentPath(currentPath: string) {
  52. const menus = await getAsyncMenus();
  53. const allParentPath = await getAllParentPath(menus, currentPath);
  54. return allParentPath[0];
  55. }
  56. // 获取1级菜单,删除children
  57. export async function getShallowMenus() {
  58. const menus = await getAsyncMenus();
  59. const routes = router.getRoutes();
  60. const shallowMenuList = menus.map((item) => ({ ...item, children: undefined }));
  61. return !isBackMode() ? shallowMenuList.filter(basicFilter(routes)) : shallowMenuList;
  62. }
  63. // 获取菜单的children
  64. export async function getChildrenMenus(parentPath: string) {
  65. const menus = await getAsyncMenus();
  66. const parent = menus.find((item) => item.path === parentPath);
  67. if (!parent) return [] as Menu[];
  68. return parent.children;
  69. }
  70. // 扁平化children
  71. export async function getFlatChildrenMenus(children: Menu[]) {
  72. return flatMenus(children);
  73. }
  74. // 通用过滤方法
  75. function basicFilter(routes: RouteRecordNormalized[]) {
  76. return (menu: Menu) => {
  77. const matchRoute = routes.find((route) => {
  78. if (route.meta) {
  79. if (route.meta.carryParam) {
  80. return pathToRegexp(route.path).test(menu.path);
  81. }
  82. if (route.meta.ignoreAuth) return false;
  83. }
  84. return route.path === menu.path;
  85. });
  86. if (!matchRoute) return false;
  87. menu.icon = menu.icon || matchRoute.meta.icon;
  88. menu.meta = matchRoute.meta;
  89. return true;
  90. };
  91. }