我叫洪圣文

This commit is contained in:
2026-05-15 16:36:50 +08:00
parent b6006e0dfe
commit 6687cf0339
36 changed files with 2201 additions and 271 deletions

View File

@@ -8,10 +8,20 @@ import { useAuthStore } from '@/stores/modules/auth'
const modules = import.meta.glob('@/views/**/*.vue')
const VIEWS_ALIAS_PREFIX = '@/views'
const VIEWS_SRC_PREFIX = '/src/views'
const ROUTER_PERF_DEBUG = import.meta.env.DEV
const perfNow = () => (typeof performance !== 'undefined' ? performance.now() : Date.now())
const logRouterPerf = (label: string, start: number, extra?: Record<string, unknown>) => {
if (!ROUTER_PERF_DEBUG) return
const duration = Math.round((perfNow() - start) * 100) / 100
console.info(`[router-perf] ${label}`, { duration, ...extra })
}
const COMPONENT_PATH_ALIASES: Record<string, string> = {
// 后端菜单沿用 event-list 模块名时,前端实际页面目录为 eventList。
'/event/event-list': '/event/eventList',
'/event/event-list/index': '/event/eventList/index'
'/event/event-list/index': '/event/eventList/index',
// 后端菜单可能使用短横线模块名,前端页面目录统一为 steadyDataView。
'/steady/steady-data-view': '/steady/steadyDataView',
'/steady/steady-data-view/index': '/steady/steadyDataView/index'
}
const STATIC_ROUTE_NAMES = new Set([
'layout',
@@ -23,6 +33,7 @@ const STATIC_ROUTE_NAMES = new Set([
'toolAddData',
'toolAddLedger',
'eventList',
'steadyDataView',
'systemMonitor',
'diskMonitor',
'403',
@@ -111,13 +122,25 @@ export const initDynamicRouter = async () => {
if (isInitializing) return Promise.reject(new Error('Dynamic router initialization in progress'))
isInitializing = true
const initStart = perfNow()
const userStore = useUserStore()
const authStore = useAuthStore()
const unresolvedRoutes: Array<{ name?: string; path?: string; component?: string; candidates: string[] }> = []
try {
const menuStart = perfNow()
await authStore.getAuthMenuList()
logRouterPerf('dynamic-router-load-menus-api', menuStart)
const flattenMenuStart = perfNow()
const flatMenuList = authStore.flatMenuListGet
logRouterPerf('dynamic-router-flatten-menus', flattenMenuStart, {
menuCount: flatMenuList.length
})
const buttonStart = perfNow()
await authStore.getAuthButtonList()
logRouterPerf('dynamic-router-load-buttons', buttonStart)
if (!authStore.authMenuListGet.length) {
ElNotification({
@@ -133,9 +156,13 @@ export const initDynamicRouter = async () => {
return Promise.reject('No permission')
}
const clearStart = perfNow()
clearDynamicRoutes()
logRouterPerf('dynamic-router-clear-routes', clearStart)
for (const item of authStore.flatMenuListGet) {
const addRoutesStart = perfNow()
let addedRouteCount = 0
for (const item of flatMenuList) {
if (item.children) delete item.children
if (item.name && STATIC_ROUTE_NAMES.has(String(item.name))) {
continue
@@ -167,10 +194,16 @@ export const initDynamicRouter = async () => {
} else {
router.addRoute('layout', routeItem)
}
addedRouteCount += 1
} else {
console.warn('Invalid route item:', item)
}
}
logRouterPerf('dynamic-router-add-routes', addRoutesStart, {
menuCount: flatMenuList.length,
addedRouteCount,
unresolvedRouteCount: unresolvedRoutes.length
})
if (unresolvedRoutes.length) {
console.error('[dynamic-router] unresolved route components', unresolvedRoutes)
@@ -182,6 +215,9 @@ export const initDynamicRouter = async () => {
await router.replace(LOGIN_URL)
return Promise.reject(error)
} finally {
logRouterPerf('dynamic-router-total', initStart, {
unresolvedRouteCount: unresolvedRoutes.length
})
isInitializing = false
}
}