index.ts 2.8 KB

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