| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- import type { Menu, MenuModule } from '@jeesite/core/router/types';
- import type { RouteRecordNormalized } from 'vue-router';
- import { useAppStoreWithOut } from '@jeesite/core/store/modules/app';
- import { usePermissionStore } from '@jeesite/core/store/modules/permission';
- import { transformMenuModule, getAllParentPath } from '@jeesite/core/router/helper/menuHelper';
- import { isUrl } from '@jeesite/core/utils/is';
- import { pathToRegexp } from 'path-to-regexp';
- const modules = import.meta.glob('./modules/**/*.ts', { eager: true });
- const menuModules: MenuModule[] = [];
- Object.keys(modules).forEach((key) => {
- const mod = (modules as Recordable)[key].default || {};
- const modList = Array.isArray(mod) ? [...mod] : [mod];
- menuModules.push(...modList);
- });
- // ===========================
- // ==========Helper===========
- // ===========================
- const getPermissionMode = () => {
- const appStore = useAppStoreWithOut();
- return appStore.getProjectConfig.permissionMode;
- };
- const staticMenus: Menu[] = [];
- (() => {
- menuModules.sort((a, b) => {
- return (a.orderNo || 0) - (b.orderNo || 0);
- });
- for (const menu of menuModules) {
- staticMenus.push(transformMenuModule(menu));
- }
- })();
- async function getAsyncMenus() {
- const permissionStore = usePermissionStore();
- return permissionStore.getBackMenuList;
- }
- export const getMenus = async (): Promise<Menu[]> => {
- return await getAsyncMenus();
- };
- export async function getCurrentParentPath(currentPath: string) {
- const menus = await getAsyncMenus();
- const allParentPath = await getAllParentPath(menus, currentPath);
- return allParentPath?.[0];
- }
- // Get the level 1 menu, delete children
- export async function getShallowMenus(): Promise<Menu[]> {
- const menus = await getAsyncMenus();
- const shallowMenuList = menus.map((item) => ({ ...item, children: undefined }));
- return shallowMenuList;
- }
- // Get the children of the menu
- export async function getChildrenMenus(parentPath: string) {
- const menus = await getMenus();
- const parent = menus.find((item) => item.path === parentPath);
- if (!parent || !parent.children || !!parent?.meta?.hideChildrenInMenu) {
- return [] as Menu[];
- }
- return parent.children;
- }
- function basicFilter(routes: RouteRecordNormalized[]) {
- return (menu: Menu) => {
- const matchRoute = routes.find((route) => {
- if (isUrl(menu.path)) return true;
- if (route.meta?.carryParam) {
- return pathToRegexp(route.path).regexp.test(menu.path);
- }
- const isSame = route.path === menu.path;
- if (!isSame) return false;
- if (route.meta?.ignoreAuth) return true;
- return isSame || pathToRegexp(route.path).regexp.test(menu.path);
- });
- if (!matchRoute) return false;
- menu.icon = (menu.icon || matchRoute.meta.icon) as string;
- menu.meta = matchRoute.meta;
- return true;
- };
- }
|