index.ts 3.1 KB

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