You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 lines
2.2 KiB

// 路由收集器 - 收集前端路由信息
import { mainRoutes } from './generated-route-mapping.js'
// 路由收集器
export class RouteCollector {
constructor() {
this.routes = []
}
// 收集路由
collectRoutes() {
try {
// 从生成的路由映射文件收集
if (!mainRoutes || !Array.isArray(mainRoutes)) {
console.warn('⚠ 生成的路由映射文件格式不正确')
return []
}
// 转换为主入口路由格式
const frontendRoutes = mainRoutes.map(route => {
// 确保模块信息正确
if (!route.module) {
route.module = this._extractModuleFromPath(route.path)
}
return {
path: route.path,
name: route.name,
module: route.module,
description: route.description || route.name,
type: route.type || 'list'
}
})
this.routes = frontendRoutes
return frontendRoutes
} catch (error) {
console.error('❌ 从生成的路由映射文件收集路由失败:', error)
return []
}
}
// 从路径提取模块名
_extractModuleFromPath(path) {
if (path === '/' || path === '') return 'home'
const segments = path.split('/').filter(Boolean)
if (segments.length === 0) return 'home'
// 获取第一个段作为模块名
const module = segments[0]
// 映射模块名
const moduleMap = {
'user-management': 'user-management',
'role-management': 'role-management',
'settings': 'system-settings',
'user-profile': 'user-management',
'route-sync': 'route-sync'
}
return moduleMap[module] || module
}
// 获取菜单路由
getMenuRoutes() {
return this.routes.filter(route => route.name && route.name !== '')
}
// 获取操作路由
getOperationRoutes() {
return this.routes.filter(route => route.type && route.type !== 'list')
}
// 按模块分组路由
getRoutesByModule() {
const grouped = {}
this.routes.forEach(route => {
const module = route.module
if (!grouped[module]) {
grouped[module] = []
}
grouped[module].push(route)
})
return grouped
}
}