index.ts 3.1 KB
Newer Older
V
vben 已提交
1
import type { Menu, MenuModule } from '/@/router/types';
陈文彬 已提交
2
import type { RouteRecordNormalized } from 'vue-router';
V
vben 已提交
3

陈文彬 已提交
4 5
import { appStore } from '/@/store/modules/app';
import { permissionStore } from '/@/store/modules/permission';
V
vben 已提交
6
import { transformMenuModule, getAllParentPath } from '/@/router/helper/menuHelper';
陈文彬 已提交
7
import { filter } from '/@/utils/helper/treeHelper';
8
import { isUrl } from '/@/utils/is';
陈文彬 已提交
9 10
import router from '/@/router';
import { PermissionModeEnum } from '/@/enums/appEnum';
V
vben 已提交
11
import { pathToRegexp } from 'path-to-regexp';
V
vben 已提交
12

V
vben 已提交
13
const modules = import.meta.globEager('./modules/**/*.ts');
V
vben 已提交
14

V
vben 已提交
15
const menuModules: MenuModule[] = [];
陈文彬 已提交
16

V
vben 已提交
17
Object.keys(modules).forEach((key) => {
V
vben 已提交
18 19 20
  const mod = modules[key].default || {};
  const modList = Array.isArray(mod) ? [...mod] : [mod];
  menuModules.push(...modList);
V
vben 已提交
21 22
});

陈文彬 已提交
23 24 25
// ===========================
// ==========Helper===========
// ===========================
V
vben 已提交
26 27 28
const isBackMode = () => {
  return appStore.getProjectConfig.permissionMode === PermissionModeEnum.BACK;
};
陈文彬 已提交
29 30 31 32 33 34

const staticMenus: Menu[] = [];
(() => {
  menuModules.sort((a, b) => {
    return (a.orderNo || 0) - (b.orderNo || 0);
  });
V
vben 已提交
35

陈文彬 已提交
36 37 38 39 40 41
  for (const menu of menuModules) {
    staticMenus.push(transformMenuModule(menu));
  }
})();

async function getAsyncMenus() {
V
vben 已提交
42
  return !isBackMode() ? staticMenus : permissionStore.getBackMenuListState;
陈文彬 已提交
43 44
}

V
vben 已提交
45
export const getMenus = async (): Promise<Menu[]> => {
陈文彬 已提交
46 47 48 49 50 51 52
  const menus = await getAsyncMenus();
  const routes = router.getRoutes();
  return !isBackMode() ? filter(menus, basicFilter(routes)) : menus;
};

export async function getCurrentParentPath(currentPath: string) {
  const menus = await getAsyncMenus();
53

陈文彬 已提交
54
  const allParentPath = await getAllParentPath(menus, currentPath);
55

V
vben 已提交
56
  return allParentPath?.[0];
陈文彬 已提交
57 58
}

59
// Get the level 1 menu, delete children
V
vben 已提交
60
export async function getShallowMenus(): Promise<Menu[]> {
陈文彬 已提交
61 62 63 64 65 66
  const menus = await getAsyncMenus();
  const routes = router.getRoutes();
  const shallowMenuList = menus.map((item) => ({ ...item, children: undefined }));
  return !isBackMode() ? shallowMenuList.filter(basicFilter(routes)) : shallowMenuList;
}

67
// Get the children of the menu
陈文彬 已提交
68 69 70
export async function getChildrenMenus(parentPath: string) {
  const menus = await getAsyncMenus();
  const parent = menus.find((item) => item.path === parentPath);
V
vben 已提交
71 72 73 74
  if (!parent || !parent.children) return [] as Menu[];
  const routes = router.getRoutes();

  return !isBackMode() ? filter(parent.children, basicFilter(routes)) : parent.children;
陈文彬 已提交
75 76 77 78
}

function basicFilter(routes: RouteRecordNormalized[]) {
  return (menu: Menu) => {
V
vben 已提交
79
    const matchRoute = routes.find((route) => {
80
      if (isUrl(menu.path)) return true;
V
vben 已提交
81

V
vben 已提交
82 83
      if (route.meta?.carryParam) {
        return pathToRegexp(route.path).test(menu.path);
V
vben 已提交
84
      }
V
vben 已提交
85 86 87 88
      const isSame = route.path === menu.path;
      if (!isSame) return false;

      if (route.meta?.ignoreAuth) return true;
V
vben 已提交
89

V
vben 已提交
90
      return isSame || pathToRegexp(route.path).test(menu.path);
V
vben 已提交
91 92 93
    });

    if (!matchRoute) return false;
V
Vben 已提交
94
    menu.icon = (menu.icon || matchRoute.meta.icon) as string;
陈文彬 已提交
95 96 97 98
    menu.meta = matchRoute.meta;
    return true;
  };
}