From fffdb69815acfe612d87523ae584ec198089d2b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=88=90?= Date: Thu, 9 Oct 2025 18:41:16 +0800 Subject: [PATCH] 1 --- src/store/user.js | 3 +-- src/utils/uiTool.js | 45 +++++++++++++++++++++++++++++++++++++++------ 2 files changed, 40 insertions(+), 8 deletions(-) diff --git a/src/store/user.js b/src/store/user.js index cec5c6d..d387b32 100644 --- a/src/store/user.js +++ b/src/store/user.js @@ -103,7 +103,6 @@ export default { // 动态添加路由(重要!解决登录后点击菜单空白的问题) if (window.rootVue && window.rootVue.$router) { - // 先移除旧的主路由(如果存在) const router = window.rootVue.$router const routes = router.options.routes @@ -115,7 +114,7 @@ export default { // 添加新的主路由 router.addRoute(mainMenu) - console.log('动态路由已添加') + console.log('动态路由已添加,redirect 设置为:', mainMenu.redirect) } } }, diff --git a/src/utils/uiTool.js b/src/utils/uiTool.js index 319ae79..4ec9ef0 100644 --- a/src/utils/uiTool.js +++ b/src/utils/uiTool.js @@ -221,15 +221,13 @@ export default class uiTool { component: Main, meta: { title: '首页', notCache: true }, children: [ - // 默认 home 路由,确保登录后能跳转 { path: '/home', - name: 'home', - meta: { title: '首页', notCache: true }, - component: HomePage || { - render: h => h('div', { style: { padding: '20px' } }, '欢迎使用管理系统') - } + name: '首页', + component: HomePage, + meta: { title: '首页', notCache: true } } + ] } @@ -269,9 +267,44 @@ export default class uiTool { if (hasHome) { // 如果权限路由中有 home,使用权限路由的 home(不添加默认首页) mainRoute.children = curRoutes + mainRoute.redirect = '/home' } else { // 如果权限路由中没有 home,保留默认 home 并添加其他路由 mainRoute.children = [homeRoute, ...curRoutes] + mainRoute.redirect = '/home' + } + + // 动态设置 redirect 到第一个有效的路由 + // 优先查找首页路由,如果没有则使用第一个菜单项 + const findFirstRoute = (routes) => { + if (!routes || routes.length === 0) return null + + for (let route of routes) { + // 优先查找 /home 路由 + if (route.path === '/home' || route.path === 'home') { + return route.path.startsWith('/') ? route.path : '/' + route.path + } + } + + // 如果没有 home,使用第一个页面路由 + for (let route of routes) { + if (route.type !== '菜单' && route.path) { + return route.path.startsWith('/') ? route.path : '/' + route.path + } + // 如果是菜单,递归查找子路由 + if (route.children && route.children.length > 0) { + const childRoute = findFirstRoute(route.children) + if (childRoute) return childRoute + } + } + + return null + } + + const firstRoutePath = findFirstRoute(mainRoute.children) + if (firstRoutePath) { + mainRoute.redirect = firstRoutePath + console.log('主路由 redirect 设置为:', firstRoutePath) } } }