feat: 优化菜单跳转逻辑,支持携带菜单参数

master^2
24 hours ago
parent b8d87afa88
commit 462a54c0f4

@ -238,7 +238,7 @@ export const useUserStore = defineStore(
const response = await UserAPI.getCurrentUserInfo();
const data = response.data.data;
const menus: MenuTable[] = data?.menus || [];
delete data?.menus;
// delete data?.menus;
info.value = { ...info.value, ...data } as Partial<UserInfo>;
setRoute(menus);
} catch (error) {

@ -67,13 +67,38 @@ export const getFirstMenuPath = (menuList: AppRouteRecordFromTypes[]): string =>
return "";
};
/** 在菜单树中递归查找 route_name 匹配的菜单项 */
const findMenuByRouteName = (menus: any[], routeName: string): any | null => {
for (const menu of menus) {
if (menu.route_name === routeName) return menu;
if (menu.children?.length) {
const found = findMenuByRouteName(menu.children, routeName);
if (found) return found;
}
}
return null;
};
export const openExternalLink = (link: string) => window.open(link, "_blank");
export const handleMenuJump = (item: AppRouteRecord, jumpToFirst: boolean = false) => {
export const handleMenuJump = async (item: AppRouteRecord, jumpToFirst: boolean = false) => {
// 在其他函数内部动态获取,避免模块加载时的循环依赖
const { useUserStoreHook } = await import("@stores");
const userStore = useUserStoreHook();
// 从菜单树中查找当前 item.name对应 route_name的菜单项提取 params 转为 query 参数
const targetMenu = findMenuByRouteName(userStore.info.menus || [], item.name as string);
const query: Record<string, string> = {};
if (targetMenu?.params) {
for (const p of targetMenu.params) {
query[p.key] = p.value;
}
}
const { link, isIframe: menuIsIframe } = item.meta;
if (link && !menuIsIframe) return openExternalLink(link);
if (!jumpToFirst || !item.children?.length) return router.push(item.path);
if (!jumpToFirst || !item.children?.length) return router.push({ path: item.path, query });
const findFirstLeafMenu = (items: AppRouteRecord[]): AppRouteRecord | undefined => {
for (const child of items) {
@ -85,9 +110,9 @@ export const handleMenuJump = (item: AppRouteRecord, jumpToFirst: boolean = fals
};
const firstChild = findFirstLeafMenu(item.children);
if (!firstChild) return router.push(item.path);
if (!firstChild) return router.push({ path: item.path, query });
if (firstChild.meta?.link) return openExternalLink(firstChild.meta.link);
return router.push(firstChild.path);
return router.push({ path: firstChild.path, query });
};
/**

Loading…
Cancel
Save