task-8(壳层/路由): 实现业务域路由注册表

新增 src/router/routes.ts:adminPages 登记业务域页面(account 首批),routeOf 生成
路由记录,validateAdminPages 校验路径唯一/相对/含 title/load;router/index.ts 改
消费 adminRouteRecords,移除内联懒加载与路径字面量。
This commit is contained in:
2026-09-05 12:19:24 +08:00
parent f610d90523
commit d10bba1ea1
4 changed files with 133 additions and 8 deletions
+49
View File
@@ -0,0 +1,49 @@
import type { RouteRecordRaw } from 'vue-router'
export interface AdminPageMeta {
title: string
menuKey?: string
}
export interface AdminPageDef extends AdminPageMeta {
/** 相对 /admin-vue/ 基准的子路径,不带前导斜杠、不带 base 前缀。 */
path: string
/** 异步组件加载器(页面按路由分包)。 */
load: () => Promise<{ default: unknown }>
}
/** 业务域路由注册表(任务 8):首批 account 域;后续按业务域在此登记页面。 */
export const adminPages: AdminPageDef[] = [
{ path: 'account/users', menuKey: 'admin_users', title: '用户管理', load: () => import('@/pages/account/UsersPage.vue') },
{ path: 'account/menus', menuKey: 'admin_columns', title: '菜单管理', load: () => import('@/pages/account/MenusPage.vue') },
{ path: 'account/groups', menuKey: 'admin_group_manage', title: '数据权限分组', load: () => import('@/pages/account/GroupsPage.vue') },
]
export function routeOf(page: AdminPageDef): RouteRecordRaw {
if (!page.path || !page.load || !page.title) {
throw new Error(`路由注册表页面定义非法,需 path/load/title 齐全: ${JSON.stringify(page)}`)
}
const meta: Record<string, string> = { title: page.title }
if (page.menuKey) meta.menuKey = page.menuKey
return { path: page.path, component: page.load, meta }
}
export const adminRouteRecords: RouteRecordRaw[] = adminPages.map(routeOf)
/** 校验注册表完整性:路径唯一、title/load 齐全、路径为相对子路径。返回问题清单。 */
export function validateAdminPages(pages: AdminPageDef[]): string[] {
const issues: string[] = []
const seen = new Set<string>()
for (const page of pages) {
if (!page.path || page.path.startsWith('/')) {
issues.push(`页面路径必须是相对子路径(不带 / 与 base 前缀): ${page.path || '(空)'}`)
} else if (seen.has(page.path)) {
issues.push(`页面路径重复: ${page.path}`)
}
seen.add(page.path)
if (!page.title) issues.push(`页面缺少标题: ${page.path}`)
if (!page.load) issues.push(`页面缺少异步加载器: ${page.path}`)
if (page.path.includes('/admin-vue')) issues.push(`页面路径不得包含 base 前缀: ${page.path}`)
}
return issues
}