task-7(壳层/路由): 根路由跳转到首个可见页面,移除硬编码默认页
helpers.firstVisiblePath 深度优先返回首个可路由页面,无页面返回空串; store 增加 firstVisible;守卫在根路由/无权限时跳首个可见页,全部无页面则 停留根路由展示无菜单空态,删除 redirect:'/account/users' 硬编码。
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
import type { AdminMenuNode } from '../types/admin'
|
||||
|
||||
/**
|
||||
* 取后端菜单树中第一个可路由页面路径(深度优先、保序)。
|
||||
* 无任何可路由页面时返回空串,由调用方决定渲染空态或回根路由,不硬编码缺省页。
|
||||
*/
|
||||
export function firstVisiblePath(nodes: AdminMenuNode[] | null | undefined): string {
|
||||
for (const node of nodes || []) {
|
||||
if (node.route) return node.route
|
||||
const nested = firstVisiblePath(node.children || [])
|
||||
if (nested) return nested
|
||||
}
|
||||
return ''
|
||||
}
|
||||
@@ -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
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user