我叫洪圣文

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

@@ -10,6 +10,15 @@ import { createHomeMenuMap, getHomeValidPaths, HOME_EXCLUDED_PATHS } from '@/uti
const WHITE_LIST_SET = new Set(ROUTER_WHITE_LIST)
const mode = import.meta.env.VITE_ROUTER_MODE
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 })
}
let routePerfStart = 0
const routerMode = {
hash: () => createWebHashHistory(),
@@ -26,10 +35,17 @@ const router = createRouter({
const syncHomeStateWithMenus = () => {
const authStore = useAuthStore()
const homeStore = useHomeStore()
homeStore.syncWithMenus(getHomeValidPaths(authStore.flatMenuListGet))
const start = perfNow()
const flatMenuList = authStore.flatMenuListGet
homeStore.syncWithMenus(getHomeValidPaths(flatMenuList))
logRouterPerf('sync-home-state', start, {
menuCount: flatMenuList.length
})
}
router.beforeEach(async (to, from, next) => {
const guardStart = perfNow()
routePerfStart = guardStart
const userStore = useUserStore()
const authStore = useAuthStore()
@@ -53,8 +69,18 @@ router.beforeEach(async (to, from, next) => {
if (!authStore.authMenuListGet.length) {
try {
const initStart = perfNow()
await initDynamicRouter()
logRouterPerf('init-dynamic-router', initStart)
const syncStart = perfNow()
syncHomeStateWithMenus()
logRouterPerf('first-sync-home-state', syncStart)
logRouterPerf('before-each-first-init', guardStart, {
path: to.path,
from: from.path
})
return next({ ...to, replace: true })
} catch (error) {
console.error('Dynamic router initialization failed', error)
@@ -64,12 +90,23 @@ router.beforeEach(async (to, from, next) => {
}
syncHomeStateWithMenus()
logRouterPerf('before-each-menu-sync', guardStart, {
path: to.path,
from: from.path,
hasActivateInfo: authStore.activateInfoLoadedGet
})
await authStore.setRouteName(to.name as string)
if (!authStore.activateInfoLoadedGet) {
const activateStart = perfNow()
await authStore.setActivateInfo()
logRouterPerf('load-activate-info', activateStart)
}
logRouterPerf('before-each-total', guardStart, {
path: to.path,
from: from.path
})
next()
})
@@ -89,8 +126,15 @@ router.onError(error => {
})
})
router.afterEach(to => {
router.afterEach((to, from) => {
NProgress.done()
const afterEachStart = perfNow()
if (routePerfStart) {
logRouterPerf('navigation-total', routePerfStart, {
path: to.path,
from: from.path
})
}
if (HOME_EXCLUDED_PATHS.has(to.path)) return
@@ -101,6 +145,10 @@ router.afterEach(to => {
if (!menuMap[to.path]) return
homeStore.addRecentVisited(to.path)
logRouterPerf('after-each-home-recent', afterEachStart, {
path: to.path,
menuCount: Object.keys(menuMap).length
})
})
export default router

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
}
}

View File

@@ -99,6 +99,24 @@ export const staticRouter: RouteRecordRaw[] = [
title: '事件列表'
}
},
{
path: '/steadyDataView/index',
name: 'steadyDataView',
alias: [
'/steadyDataView',
'/steadydataView',
'/steadydataView/index',
'/steady/steadyDataView',
'/steady/steadyDataView/index',
'/steady/steady-data-view',
'/steady/steady-data-view/index'
],
component: () => import('@/views/steady/steadyDataView/index.vue'),
meta: {
cacheName: 'SteadyDataView',
title: '稳态数据'
}
},
{
path: '/403',
name: '403',