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
+14
View File
@@ -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 ''
}
+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
@@ -0,0 +1,43 @@
import { defineStore } from 'pinia'
import { fetchAdminMenuTree, fetchCurrentUser, logout } from '@/api/session'
import type { AdminMenuNode, AdminUser } from '@/types/admin'
import { firstVisiblePath } from '@/router/helpers'
export const useAdminSessionStore = defineStore('admin-session', {
state: () => ({
user: null as AdminUser | null,
menuTree: [] as AdminMenuNode[],
initialized: false,
loading: false,
error: '',
}),
getters: {
isSuperAdmin: (state) => state.user?.role === 'super_admin',
firstVisible: (state): string => {
return firstVisiblePath(state.menuTree)
},
},
actions: {
async initialize() {
if (this.initialized || this.loading) return
this.loading = true
this.error = ''
try {
this.user = await fetchCurrentUser()
this.menuTree = await fetchAdminMenuTree()
this.initialized = true
} catch (error) {
this.error = error instanceof Error ? error.message : '后台初始化失败'
throw error
} finally {
this.loading = false
}
},
async signOut() {
await logout()
this.$reset()
window.location.assign('/login')
},
},
})