波形解析相关

This commit is contained in:
2026-04-16 08:11:38 +08:00
parent 8e2c044381
commit 5596d57409
23 changed files with 162 additions and 78 deletions

View File

@@ -7,7 +7,7 @@ import { useAuthStore } from '@/stores/modules/auth'
// 引入 views 文件夹下所有 vue 文件
const modules = import.meta.glob('@/views/**/*.vue')
const STATIC_ROUTE_NAMES = new Set(['layout', 'login', 'home', '403', '404', '500'])
const STATIC_ROUTE_NAMES = new Set(['layout', 'login', 'home', 'tools', 'toolWaveform', 'toolMmsMapping', '403', '404', '500'])
let isInitializing = false
@@ -24,11 +24,12 @@ const clearDynamicRoutes = () => {
}
/**
* 根据 component 路径查找对应的模块
* @param path 组件路径
* 根据菜单 component 路径查找对应的页面模块
* 兼容两种仓库写法:
* 1. /foo/bar.vue
* 2. /foo/bar/index.vue
*/
const resolveComponentModule = async (path: string) => {
// 规范化路径,去除首尾斜杠
const resolveComponentModule = (path: string) => {
let normalizedPath = path.trim()
if (!normalizedPath.startsWith('/')) {
normalizedPath = '/' + normalizedPath
@@ -36,9 +37,26 @@ const resolveComponentModule = async (path: string) => {
if (normalizedPath.endsWith('.vue')) {
normalizedPath = normalizedPath.slice(0, -4)
}
if (normalizedPath.length > 1 && normalizedPath.endsWith('/')) {
normalizedPath = normalizedPath.slice(0, -1)
}
const fullPath = `/src/views${normalizedPath}.vue`
return modules[fullPath]
const candidatePaths = [`/src/views${normalizedPath}.vue`, `/src/views${normalizedPath}/index.vue`]
for (const candidatePath of candidatePaths) {
const moduleLoader = modules[candidatePath]
if (moduleLoader) {
return {
moduleLoader,
resolvedPath: candidatePath
}
}
}
return {
moduleLoader: undefined,
resolvedPath: candidatePaths
}
}
/**
@@ -50,6 +68,7 @@ export const initDynamicRouter = async () => {
isInitializing = true
const userStore = useUserStore()
const authStore = useAuthStore()
const unresolvedRoutes: Array<{ name?: string; path?: string; component?: string; candidates: string[] }> = []
try {
// 1. 获取菜单列表 && 按钮权限列表
@@ -81,11 +100,17 @@ export const initDynamicRouter = async () => {
// 处理组件映射
if (item.component && typeof item.component === 'string') {
const moduleLoader = await resolveComponentModule(item.component)
const { moduleLoader, resolvedPath } = resolveComponentModule(item.component)
if (moduleLoader) {
item.component = moduleLoader
} else {
console.warn(`未能找到组件: ${item.component}`)
// 动态路由组件一旦解析失败,对应菜单会落入 404这里必须打印清楚候选路径。
unresolvedRoutes.push({
name: item.name,
path: item.path,
component: item.component,
candidates: resolvedPath
})
continue
}
}
@@ -96,7 +121,7 @@ export const initDynamicRouter = async () => {
(typeof item.component === 'function' || typeof item.redirect === 'string')
) {
const routeItem = item as unknown as RouteRecordRaw
if (item.meta.isFull) {
if (item.meta?.isFull) {
router.addRoute(routeItem)
} else {
router.addRoute('layout', routeItem)
@@ -105,6 +130,10 @@ export const initDynamicRouter = async () => {
console.warn('Invalid route item:', item)
}
}
if (unresolvedRoutes.length) {
console.error('[dynamic-router] unresolved route components', unresolvedRoutes)
}
} catch (error) {
// 当按钮 || 菜单请求出错时,重定向到登陆页
userStore.setAccessToken('')