06c1a3accd
- 新建 src/config/company-scope.ts:SUPER_ADMIN_ONLY_MENU_KEYS=['admin_companies'] 与 COMPANY_SCOPED_PAGES(批次 1 的 8 页口径清单,后续任务的唯一分母) - routes.ts:AdminPageDef 加 superAdminOnly/companyScoped;公司管理标 superAdminOnly: true, 8 个 A 类页标 companyScoped: true,C 类两页显式写 false(声明而非留空) - helpers.ts:pruneSuperAdminOnly 纯函数(超管原样返回,非超管递归剪掉专属节点) - router/index.ts:菜单授权之外补一条与菜单树无关的角色判定 - admin-session.ts:菜单树唯一写点内接剪枝(顺序仍在 fetchCurrentUser 之后) - 新增 tests/company-super-admin-only.test.ts 5 支护栏 - task-29 的主路径断言随唯一写点改写(原断言只认 canonicalize 裸赋值, 剪枝并入同一写点后必然失配;改为同时要求 prune + canonicalize,意图不降反强)
64 lines
2.3 KiB
TypeScript
64 lines
2.3 KiB
TypeScript
import { createRouter, createWebHistory, type RouteRecordRaw } from 'vue-router'
|
|
import AdminLayout from '@/layout/AdminLayout.vue'
|
|
import LoginPage from '@/pages/login/LoginPage.vue'
|
|
import NotFoundPage from '@/pages/error/NotFoundPage.vue'
|
|
import { useAdminSessionStore } from '@/stores/admin-session'
|
|
import { APP_BASE_PATH } from '@/config/app'
|
|
import { adminRouteRecords } from './routes'
|
|
import { defineLazyPage } from './lazy-page'
|
|
import { isRouteAllowed } from './helpers'
|
|
|
|
const router = createRouter({
|
|
history: createWebHistory(APP_BASE_PATH),
|
|
routes: [
|
|
{
|
|
path: '/login',
|
|
component: LoginPage,
|
|
meta: { title: '登录' },
|
|
},
|
|
{
|
|
path: '/',
|
|
component: AdminLayout,
|
|
children: [
|
|
...adminRouteRecords.map((record) => ({
|
|
...record,
|
|
component: defineLazyPage(record.component as () => Promise<{ default: unknown }>),
|
|
})),
|
|
{ path: ':pathMatch(.*)*', component: NotFoundPage, meta: { title: '页面不存在' } },
|
|
] as RouteRecordRaw[],
|
|
},
|
|
],
|
|
})
|
|
|
|
router.beforeEach(async (to) => {
|
|
const session = useAdminSessionStore()
|
|
// 登录页是公开页;已登录访问则回根路由。
|
|
if (to.path === '/login') {
|
|
return session.initialized && session.user ? '/' : true
|
|
}
|
|
if (!session.initialized) {
|
|
try {
|
|
await session.initialize()
|
|
} catch {
|
|
// 未登录:401 由 http 拦截器整页跳转 SPA 登录页;此处放行当前导航由守卫收口。
|
|
return false
|
|
}
|
|
}
|
|
// 根路由:有可见页面时跳到首个可见页;没有任何页面时停留根路由展示无菜单空态。
|
|
if (to.path === '/') {
|
|
return session.firstVisible || true
|
|
}
|
|
// 角色内在闸门:与菜单树无关。超管恒 true 会让"只剪菜单树"这条对超管无效,
|
|
// 而非超管即使被历史授权过 admin_companies 也进不来(spec §8.1:两处缺一处就留旁路)。
|
|
if (to.meta.superAdminOnly && !session.isSuperAdmin) {
|
|
return session.firstVisible || '/'
|
|
}
|
|
// 页面级权限:无 menuKey 开放页放行;越权页回退首个可见页,全无则回根空态。
|
|
if (!isRouteAllowed(session.isSuperAdmin, to.meta.menuKey, session.menuTree)) {
|
|
return session.firstVisible || '/'
|
|
}
|
|
return true
|
|
})
|
|
|
|
export default router
|