fix: ref写法优化 路由跳转逻辑优化 部分样式优化

This commit is contained in:
ZhuRui
2026-07-29 09:32:14 +08:00
parent a4dab891cf
commit 8439f64c40
11 changed files with 145 additions and 69 deletions
+38 -6
View File
@@ -54,7 +54,7 @@ const state = reactive<MenuState>({
menuTree: [],
menuItems: [],
loaded: false,
homePath: '/dashboard',
homePath: '',
});
// ============================================================
@@ -69,6 +69,13 @@ function getEnabledChildren(node: MenuNode): MenuNode[] {
return node.children.filter((child) => !child.disabled && !child.externalLink);
}
function normalizeRoutePath(path?: string): string {
if (!path) return '';
const purePath = path.split(/[?#]/)[0] || '';
if (!purePath || purePath === '/') return purePath;
return purePath.startsWith('/') ? purePath : `/${purePath}`;
}
/**
* 将后端 MenuNode 递归转为 antd Menu 的 items 格式
*
@@ -186,15 +193,35 @@ function getFirstMenuPath(nodes: MenuNode[]): string {
if (node.disabled || node.externalLink) continue;
const enabledChildren = getEnabledChildren(node);
if (!node.hideInMenu && enabledChildren.length === 0) {
return '/' + node.path;
if (!node.hideInMenu && node.component) {
return normalizeRoutePath(node.path);
}
if (enabledChildren.length > 0) {
const childPath = getFirstMenuPath(enabledChildren);
if (childPath) return childPath;
}
}
return '/dashboard';
return '';
}
function hasRoutePath(nodes: MenuNode[], targetPath: string): boolean {
const normalizedTarget = normalizeRoutePath(targetPath);
if (!normalizedTarget) return false;
for (const node of nodes) {
if (node.disabled || node.externalLink) continue;
const currentPath = normalizeRoutePath(node.path);
if (currentPath === normalizedTarget && node.component) {
return true;
}
if (node.children?.length && hasRoutePath(node.children, normalizedTarget)) {
return true;
}
}
return false;
}
/**
@@ -230,7 +257,7 @@ function applyMenuTree(menuTree: MenuNode[]) {
});
// 默认重定向到首页
router.addRoute('BasicLayout', { path: '', redirect: state.homePath });
router.addRoute('BasicLayout', { path: '', redirect: state.homePath || '/404' });
}
// ============================================================
@@ -258,7 +285,11 @@ export function useMenuStore() {
state.menuTree = [];
state.menuItems = [];
state.loaded = false;
state.homePath = '/dashboard';
state.homePath = '';
};
const isRoutePathAvailable = (path?: string) => {
return hasRoutePath(state.menuTree, path || '');
};
return {
@@ -269,5 +300,6 @@ export function useMenuStore() {
homePath: computed(() => state.homePath),
loadMenu,
clearMenu,
isRoutePathAvailable,
};
}
+4
View File
@@ -1,5 +1,6 @@
import { reactive, computed, toRefs } from 'vue';
import router from '@/router';
import { useMenuStore } from '@/stores/menuStore';
import type { TabView } from '@/types';
export type { TabView };
@@ -31,6 +32,9 @@ export function useTabsStore() {
const addTab = (view: TabView): void => {
if (!view || !view.path || !view.name) return;
const { isRoutePathAvailable } = useMenuStore();
if (!isRoutePathAvailable(view.path)) return;
if (!state.tabs.some((t) => t.path === view.path)) {
state.tabs.push(view);
}