task-7(壳层/路由): 根路由跳转到首个可见页面,移除硬编码默认页

helpers.firstVisiblePath 深度优先返回首个可路由页面,无页面返回空串;
store 增加 firstVisible;守卫在根路由/无权限时跳首个可见页,全部无页面则
停留根路由展示无菜单空态,删除 redirect:'/account/users' 硬编码。
This commit is contained in:
2026-09-05 12:18:16 +08:00
parent 26edb9e6ed
commit 8e828c1204
4 changed files with 140 additions and 7 deletions
+6 -7
View File
@@ -2,7 +2,6 @@ import { createRouter, createWebHistory } from 'vue-router'
import type { AdminMenuNode } from '@/types/admin'
import AdminLayout from '@/layout/AdminLayout.vue'
import { useAdminSessionStore } from '@/stores/admin-session'
import { findFirstRoute } from './helpers'
import { APP_BASE_PATH } from '@/config/app'
const router = createRouter({
@@ -12,7 +11,6 @@ const router = createRouter({
path: '/',
component: AdminLayout,
children: [
{ path: '', redirect: '/account/users' },
{ path: 'account/users', component: () => import('@/pages/account/UsersPage.vue'), meta: { menuKey: 'admin_users', title: '用户管理' } },
{ path: 'account/menus', component: () => import('@/pages/account/MenusPage.vue'), meta: { menuKey: 'admin_columns', title: '菜单管理' } },
{ path: 'account/groups', component: () => import('@/pages/account/GroupsPage.vue'), meta: { menuKey: 'admin_group_manage', title: '数据权限分组' } },
@@ -30,10 +28,14 @@ router.beforeEach(async (to) => {
return false
}
}
if (to.path === '/') return findFirstRoute(session.menuTree)
// 根路由:有可见页面时跳到首个可见页;没有任何页面时停留根路由展示无菜单空态。
if (to.path === '/') {
return session.firstVisible || true
}
const menuKey = to.meta.menuKey as string | undefined
if (menuKey && !session.isSuperAdmin && !hasMenu(session.menuTree, menuKey)) {
return findFirstRoute(session.menuTree)
// 无权限页面:回退到首个可见页面;一个都没有则回根路由空态。
return session.firstVisible || '/'
}
return true
})
@@ -43,6 +45,3 @@ function hasMenu(nodes: AdminMenuNode[], key: string): boolean {
}
export default router