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

// 硬编码的菜单配置
export const homeMenus: MenuItem[] = [
	{
		id: "home-dashboard",
		title: "工作台",
		path: "/home/dashboard",
		level: 1,
	},
];

export const systemMenus: MenuItem[] = [
	{
		id: "system-user",
		title: "UI组件库",
		path: "/system/user",
		level: 1,
	},
	{
		id: "page-hooks",
		title: "组合函数",
		path: "/pageHooks/index",
		level: 1,
	},
	{
		id: "system-role",
		title: "角色管理",
		path: "/system/role",
		level: 1,
	},
	{
		id: "system-menu",
		title: "菜单管理",
		path: "/system/menu",
		level: 1,
	},
];

export const businessMenus: MenuItem[] = [
	{
		id: "business-order",
		title: "订单管理",
		path: "/business/order",
		level: 1,
		children: [
			{
				id: "business-order-list",
				title: "订单列表",
				path: "/business/order/list",
				level: 2,
				parentId: "business-order",
			},
			{
				id: "business-order-detail",
				title: "订单详情",
				path: "/business/order/detail",
				level: 2,
				parentId: "business-order",
			},
		],
	},
	{
		id: "business-product",
		title: "产品管理",
		path: "/business/product",
		level: 1,
		children: [
			{
				id: "business-product-list",
				title: "产品列表",
				path: "/business/product/list",
				level: 2,
				parentId: "business-product",
			},
			{
				id: "business-product-add",
				title: "添加产品",
				path: "/business/product/add",
				level: 2,
				parentId: "business-product",
			},
		],
	},
];

export const defaultTopMenus: TopMenuItem[] = [
	{
		id: "home",
		title: "首页",
		path: "/home",
		sidebarMenus: homeMenus,
	},
	{
		id: "system",
		title: "系统管理",
		path: "/system",
		sidebarMenus: systemMenus,
	},
	{
		id: "business",
		title: "业务管理",
		path: "/business",
		sidebarMenus: businessMenus,
	},
];

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;
}

export function isPathMatch(menuPath: string, targetPath: string): boolean {
	if (!menuPath || !targetPath) {
		return false;
	}

	const normalizedMenuPath = menuPath.replace(/\/+$/, "");
	const normalizedTargetPath = targetPath.replace(/\/+$/, "");

	if (normalizedMenuPath === normalizedTargetPath) {
		return true;
	}

	if (normalizedMenuPath.includes(":")) {
		const pattern = normalizedMenuPath.replaceAll(/:[^/]+/g, "[^/]+").replaceAll("/", String.raw`\/`);
		const regex = new RegExp(`^${pattern}$`);
		return regex.test(normalizedTargetPath);
	}

	if (normalizedTargetPath.startsWith(normalizedMenuPath)) {
		const nextChar = normalizedTargetPath[normalizedMenuPath.length];
		if (nextChar === "/" || nextChar === undefined) {
			return true;
		}
	}

	return false;
}

export function getMenuParentPaths(
	menus: MenuItem[],
	targetPath: string,
): string[] {
	const paths: string[] = [];
	const normalizedTargetPath = targetPath.replace(/\/+$/, "");

	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)) {
				return true;
			}
		}
		return false;
	}

	findPathChain(menus, normalizedTargetPath, []);
  
	return Array.from(new Set(paths));
}

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)) {
				return true;
			}
		}
		return false;
	}

	findMenuChain(menus, normalizedTargetPath, []);
  
	return menuChain;
}