menus.ts 6.13 KB
import type { MenuItem, TopMenuItem } from "@/types/menu";

/**
 * 侧边栏菜单配置示例
 * 支持一级、二级、三级菜单结构
 * 注意:这些是默认菜单配置,实际菜单将从 API 动态获取并存储在 menu store 中
 */

// 首页侧边栏菜单
export const homeMenus: MenuItem[] = [];

// Vue 微应用侧边栏菜单(多级嵌套示例)
// 支持一级、二级、三级菜单嵌套结构
// 注意:此配置仅作为后备方案,实际菜单将从 API 动态获取
export const vueAppMenus: MenuItem[] = [];

/**
 * 默认顶部导航菜单配置
 * 每个顶部菜单可以关联不同的侧边栏菜单
 * 注意:实际菜单将从 API 动态获取,此配置仅作为后备方案
 */
export const defaultTopMenus: TopMenuItem[] = [];

/**
 * 根据路径获取对应的侧边栏菜单
 * 注意:此函数已废弃,请使用 menu store 中的 getSidebarMenusByPath 方法
 * @deprecated 请使用 useMenuStore().getSidebarMenusByPath()
 */
export function getSidebarMenusByPath(path: string): MenuItem[] {
	const topMenu = defaultTopMenus.find((menu) => path.startsWith(menu.path));
	return topMenu?.sidebarMenus || [];
}

/**
 * 递归查找菜单项
 */
export function findMenuItem(menus: MenuItem[], id: string): MenuItem | null {
	for (const menu of menus) {
		if (menu.id === id) {
			return menu;
		}
		if (menu.children) {
			const found = findMenuItem(menu.children, id);
			if (found) return found;
		}
	}
	return null;
}

/**
 * 检查路径是否匹配(支持动态路由参数)
 * @param menuPath 菜单配置的路径(可能包含 :id, :param 等参数)
 * @param targetPath 实际路由路径
 * @returns 是否匹配
 */
export function isPathMatch(menuPath: string, targetPath: string): boolean {
	if (!menuPath || !targetPath) {
		return false;
	}

	// 去掉末尾多余的 /,方便匹配像 /edit/ 与 /edit/2 这类场景
	const normalizedMenuPath = menuPath.replace(/\/+$/, "");
	const normalizedTargetPath = targetPath.replace(/\/+$/, "");

	// 精确匹配
	if (normalizedMenuPath === normalizedTargetPath) {
		return true;
	}

	// 如果菜单路径包含参数占位符(如 :id),转换为正则表达式匹配
	if (normalizedMenuPath.includes(":")) {
		// 将路径中的 :id, :param 等替换为正则表达式的非斜杠字符匹配
		// 例如: /assisant/assistant/edit/:id -> /assisant/assistant/edit/[^/]+
		const pattern = normalizedMenuPath
			.replaceAll(/:[^/]+/g, "[^/]+")
			.replaceAll("/", String.raw`\/`);
		// 匹配完整路径,例如: /assisant/assistant/edit/123 应该匹配 /assisant/assistant/edit/:id
		const regex = new RegExp(`^${pattern}$`);
		return regex.test(normalizedTargetPath);
	}

	// 如果目标路径以菜单路径开头,也认为是匹配(用于处理动态路由参数)
	// 例如:菜单路径是 /assisant/assistant/edit,实际路径是 /assisant/assistant/edit/123
	// 检查菜单路径是否是目标路径的前缀,且目标路径的下一个字符是 / 或结束
	if (normalizedTargetPath.startsWith(normalizedMenuPath)) {
		const nextChar = normalizedTargetPath[normalizedMenuPath.length];
		// 如果下一个字符是 / 或者是 undefined(路径结束),则匹配
		if (nextChar === "/" || nextChar === undefined) {
			return true;
		}
	}

	return false;
}

/**
 * 获取菜单的所有父级路径(包括自身)
 * 支持动态路由参数匹配(如 /edit/:id 可以匹配 /edit/123)
 * 返回的路径数组按照菜单层级顺序排列(从一级到当前级)
 * 
 * @param menus 菜单列表
 * @param targetPath 目标路径
 * @returns 路径数组,按层级顺序排列
 */
export function getMenuParentPaths(
	menus: MenuItem[],
	targetPath: string,
): string[] {
	const paths: string[] = [];
	const normalizedTargetPath = targetPath.replace(/\/+$/, "");

	/**
   * 递归查找路径链
   * @param items 当前层级的菜单项
   * @param target 目标路径
   * @param currentPathChain 当前已构建的路径链
   * @returns 是否找到匹配项
   */
	function findPathChain(
		items: MenuItem[],
		target: string,
		currentPathChain: string[],
	): boolean {
		for (const item of items) {
			// 构建新的路径链(包含当前项)
			const newPathChain = [ ...currentPathChain, item.path ];

			// 使用路径匹配检查当前项是否匹配目标路径
			if (isPathMatch(item.path, target)) {
				// 找到匹配项,将完整的路径链添加到结果中
				paths.push(...newPathChain);
				return true;
			}

			// 递归查找子菜单
			if (item.children && item.children.length > 0 && findPathChain(item.children, target, newPathChain)) {
				// 在子菜单中找到匹配项,路径链已经添加到 paths 中
				return true;
			}
		}
		return false;
	}

	// 开始查找路径链
	findPathChain(menus, normalizedTargetPath, []);
  
	// 返回去重后的路径数组(虽然理论上不应该有重复,但为了安全起见)
	return Array.from(new Set(paths));
}

/**
 * 查找菜单项及其所有父级菜单项
 * 返回的数组按照菜单层级顺序排列(从一级到当前级)
 * 
 * @param menus 菜单列表
 * @param targetPath 目标路径
 * @returns 菜单项数组,按层级顺序排列
 */
export function getMenuChain(
	menus: MenuItem[],
	targetPath: string,
): MenuItem[] {
	const menuChain: MenuItem[] = [];
	const normalizedTargetPath = targetPath.replace(/\/+$/, "");

	/**
   * 递归查找菜单链
   */
	function findMenuChain(
		items: MenuItem[],
		target: string,
		currentChain: MenuItem[],
	): boolean {
		for (const item of items) {
			// 构建新的菜单链(包含当前项)
			const newChain = [ ...currentChain, item ];

			// 使用路径匹配检查当前项是否匹配目标路径
			if (isPathMatch(item.path, target)) {
				// 找到匹配项,将完整的菜单链添加到结果中
				menuChain.push(...newChain);
				return true;
			}

			// 递归查找子菜单
			if (item.children && item.children.length > 0 && findMenuChain(item.children, target, newChain)) {
				// 在子菜单中找到匹配项,菜单链已经添加到 menuChain 中
				return true;
			}
		}
		return false;
	}

	// 开始查找菜单链
	findMenuChain(menus, normalizedTargetPath, []);
  
	return menuChain;
}