Blame view

src/router/menus/index.ts 3.96 KB
vben authored
1
import type { Menu, MenuModule } from '/@/router/types';
陈文彬 authored
2
import type { RouteRecordNormalized } from 'vue-router';
3
4
import { useAppStoreWithOut } from '/@/store/modules/app';
Vben authored
5
import { usePermissionStore } from '/@/store/modules/permission';
6
import { transformMenuModule, getAllParentPath } from '/@/router/helper/menuHelper';
陈文彬 authored
7
import { filter } from '/@/utils/helper/treeHelper';
8
import { isUrl } from '/@/utils/is';
Vben authored
9
import { router } from '/@/router';
陈文彬 authored
10
import { PermissionModeEnum } from '/@/enums/appEnum';
11
import { pathToRegexp } from 'path-to-regexp';
vben authored
12
13
const modules = import.meta.glob('./modules/**/*.ts', { eager: true });
14
vben authored
15
const menuModules: MenuModule[] = [];
陈文彬 authored
16
vben authored
17
Object.keys(modules).forEach((key) => {
18
  const mod = (modules as Recordable)[key].default || {};
vben authored
19
20
  const modList = Array.isArray(mod) ? [...mod] : [mod];
  menuModules.push(...modList);
vben authored
21
22
});
陈文彬 authored
23
24
25
// ===========================
// ==========Helper===========
// ===========================
26
27
28
29
30

const getPermissionMode = () => {
  const appStore = useAppStoreWithOut();
  return appStore.getProjectConfig.permissionMode;
};
vben authored
31
const isBackMode = () => {
32
33
34
35
36
37
38
39
40
  return getPermissionMode() === PermissionModeEnum.BACK;
};

const isRouteMappingMode = () => {
  return getPermissionMode() === PermissionModeEnum.ROUTE_MAPPING;
};

const isRoleMode = () => {
  return getPermissionMode() === PermissionModeEnum.ROLE;
vben authored
41
};
陈文彬 authored
42
43
44
45
46
47

const staticMenus: Menu[] = [];
(() => {
  menuModules.sort((a, b) => {
    return (a.orderNo || 0) - (b.orderNo || 0);
  });
48
陈文彬 authored
49
50
51
52
53
54
  for (const menu of menuModules) {
    staticMenus.push(transformMenuModule(menu));
  }
})();

async function getAsyncMenus() {
Vben authored
55
  const permissionStore = usePermissionStore();
56
57
58
59
60
61
62
63
64
65
  //递归过滤所有隐藏的菜单
  const menuFilter = (items) => {
    return items.filter((item) => {
      const show = !item.meta?.hideMenu && !item.hideMenu;
      if (show && item.children) {
        item.children = menuFilter(item.children);
      }
      return show;
    });
  };
sanmu authored
66
67
  if (isBackMode()) {
68
    return menuFilter(permissionStore.getBackMenuList);
69
  }
sanmu authored
70
71
  // if (isRouteMappingMode()) {
  if (true) {
72
    return menuFilter(permissionStore.getFrontMenuList);
73
74
  }
  return staticMenus;
陈文彬 authored
75
76
}
vben authored
77
export const getMenus = async (): Promise<Menu[]> => {
陈文彬 authored
78
  const menus = await getAsyncMenus();
79
80
81
82
83
  if (isRoleMode()) {
    const routes = router.getRoutes();
    return filter(menus, basicFilter(routes));
  }
  return menus;
陈文彬 authored
84
85
86
87
88
};

export async function getCurrentParentPath(currentPath: string) {
  const menus = await getAsyncMenus();
  const allParentPath = await getAllParentPath(menus, currentPath);
89
  return allParentPath?.[0];
陈文彬 authored
90
91
}
92
// Get the level 1 menu, delete children
vben authored
93
export async function getShallowMenus(): Promise<Menu[]> {
陈文彬 authored
94
95
  const menus = await getAsyncMenus();
  const shallowMenuList = menus.map((item) => ({ ...item, children: undefined }));
96
97
98
99
100
  if (isRoleMode()) {
    const routes = router.getRoutes();
    return shallowMenuList.filter(basicFilter(routes));
  }
  return shallowMenuList;
陈文彬 authored
101
102
}
103
// Get the children of the menu
陈文彬 authored
104
export async function getChildrenMenus(parentPath: string) {
105
  const menus = await getMenus();
陈文彬 authored
106
  const parent = menus.find((item) => item.path === parentPath);
107
108
109
110
111
112
113
114
  if (!parent || !parent.children || !!parent?.meta?.hideChildrenInMenu) {
    return [] as Menu[];
  }
  if (isRoleMode()) {
    const routes = router.getRoutes();
    return filter(parent.children, basicFilter(routes));
  }
  return parent.children;
陈文彬 authored
115
116
117
118
}

function basicFilter(routes: RouteRecordNormalized[]) {
  return (menu: Menu) => {
119
    const matchRoute = routes.find((route) => {
120
      if (isUrl(menu.path)) return true;
121
122
123
      if (route.meta?.carryParam) {
        return pathToRegexp(route.path).test(menu.path);
124
      }
125
126
127
128
      const isSame = route.path === menu.path;
      if (!isSame) return false;

      if (route.meta?.ignoreAuth) return true;
129
130
      return isSame || pathToRegexp(route.path).test(menu.path);
131
132
133
    });

    if (!matchRoute) return false;
Vben authored
134
    menu.icon = (menu.icon || matchRoute.meta.icon) as string;
陈文彬 authored
135
136
137
138
    menu.meta = matchRoute.meta;
    return true;
  };
}