menu.ts 1.68 KB
import { getMicroAppByPath } from "@/qiankun/apps";
/**
 * 菜单相关工具函数
 */
import { useMenuStore } from "@/stores";

/**
 * 获取第一个子应用的第一个菜单的第一个子菜单路径
 * @returns 返回路径,如果没有找到则返回 '/home'
 */
export function getFirstSubAppFirstMenuPath(): string {
	const menuStore = useMenuStore();
	const topMenus = menuStore.topMenus;
	console.log("[菜单工具] 获取默认路径,顶级菜单数量:", topMenus.length);
  
	// 排除首页,找到第一个子应用
	const firstSubApp = topMenus.find(
		(menu) => menu.path !== "/home" && menu.path !== "/",
	);

	if (
		!firstSubApp ||
    !firstSubApp.sidebarMenus ||
    firstSubApp.sidebarMenus.length === 0
	) {
		console.log("[菜单工具] 未找到有效的子应用菜单,返回默认路径 /home");
		return "/home";
	}

	// 获取第一个菜单项
	const firstMenu = firstSubApp.sidebarMenus[0];

	// 如果第一个菜单有子菜单,返回第一个子菜单的路径
	if (firstMenu.children && firstMenu.children.length > 0) {
		const targetPath = firstMenu.children[0].path;
		// 异步检查是否为微应用路由(不阻塞返回)
		getMicroAppByPath(targetPath, true).then(microApp => {
			console.log("[菜单工具] 默认路径:", targetPath, "是否为微应用路由:", !!microApp);
		});
		return targetPath;
	}

	// 如果没有子菜单,返回第一个菜单本身的路径
	const targetPath = firstMenu.path;
	// 异步检查是否为微应用路由(不阻塞返回)
	getMicroAppByPath(targetPath, true).then(microApp => {
		console.log("[菜单工具] 默认路径:", targetPath, "是否为微应用路由:", !!microApp);
	});
	return targetPath;
}