index.ts 3.2 KB
Newer Older
V
vben 已提交
1
import type { Menu, MenuModule } from '/@/router/types';
陈文彬 已提交
2 3 4
import type { RouteRecordNormalized } from 'vue-router';
import { appStore } from '/@/store/modules/app';
import { permissionStore } from '/@/store/modules/permission';
V
vben 已提交
5
import { transformMenuModule, flatMenus, getAllParentPath } from '/@/router/helper/menuHelper';
陈文彬 已提交
6 7 8
import { filter } from '/@/utils/helper/treeHelper';
import router from '/@/router';
import { PermissionModeEnum } from '/@/enums/appEnum';
V
vben 已提交
9
import { pathToRegexp } from 'path-to-regexp';
V
vben 已提交
10

V
vben 已提交
11
import modules from 'globby!/@/router/menus/modules/**/*.@(ts)';
陈文彬 已提交
12

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

V
vben 已提交
15
Object.keys(modules).forEach((key) => {
V
vben 已提交
16 17 18
  const moduleItem = modules[key];
  const menuModule = Array.isArray(moduleItem) ? [...moduleItem] : [moduleItem];
  menuModules.push(...menuModule);
V
vben 已提交
19 20
});

陈文彬 已提交
21 22 23 24 25 26 27 28 29
// ===========================
// ==========Helper===========
// ===========================

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

陈文彬 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
  for (const menu of menuModules) {
    staticMenus.push(transformMenuModule(menu));
  }
})();

const isBackMode = () => {
  return appStore.getProjectConfig.permissionMode === PermissionModeEnum.BACK;
};

async function getAsyncMenus() {
  // 前端角色控制菜单 直接取菜单文件
  if (!isBackMode()) {
    return staticMenus;
  }
  return permissionStore.getBackMenuListState;
}

// 获取深层扁平化菜单
export const getFlatMenus = async () => {
  const menus = await getAsyncMenus();
  return flatMenus(menus);
};

// 获取菜单 树级
export const getMenus = async () => {
  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();
  const allParentPath = await getAllParentPath(menus, currentPath);
  return allParentPath[0];
}

// 获取1级菜单,删除children
export async function getShallowMenus() {
  const menus = await getAsyncMenus();
  const routes = router.getRoutes();
  const shallowMenuList = menus.map((item) => ({ ...item, children: undefined }));
  return !isBackMode() ? shallowMenuList.filter(basicFilter(routes)) : shallowMenuList;
}

// 获取菜单的children
export async function getChildrenMenus(parentPath: string) {
  const menus = await getAsyncMenus();
  const parent = menus.find((item) => item.path === parentPath);
  if (!parent) return [] as Menu[];
  return parent.children;
}

// 扁平化children
export async function getFlatChildrenMenus(children: Menu[]) {
  return flatMenus(children);
}

// 通用过滤方法
function basicFilter(routes: RouteRecordNormalized[]) {
  return (menu: Menu) => {
V
vben 已提交
92
    const matchRoute = routes.find((route) => {
93 94 95 96
      if (route.meta) {
        if (route.meta.carryParam) {
          return pathToRegexp(route.path).test(menu.path);
        }
V
vben 已提交
97
        if (route.meta.ignoreAuth) return false;
V
vben 已提交
98 99 100 101 102
      }
      return route.path === menu.path;
    });

    if (!matchRoute) return false;
陈文彬 已提交
103 104 105 106 107
    menu.icon = menu.icon || matchRoute.meta.icon;
    menu.meta = matchRoute.meta;
    return true;
  };
}