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.
 
 
 
 
 
 

117 lines
3.6 KiB

// 路由收集器 - 收集前端路由信息
import { directRouteMappings } from './direct-route-mappings.js'
import { RouteUtils } from './RouteConfig.js'
// 路由收集器
export class RouteCollector {
constructor() {
this.routes = []
}
// 收集路由
collectRoutes() {
try {
console.log('🔄 开始收集路由...')
console.log('📊 directRouteMappings:', directRouteMappings)
console.log('📊 directRouteMappings类型:', typeof directRouteMappings)
console.log('📊 directRouteMappings是否为undefined:', directRouteMappings === undefined)
// 从生成的路由映射文件收集
if (!directRouteMappings || !directRouteMappings.routes) {
console.warn('⚠ 生成的路由映射文件格式不正确')
console.log('🔍 调试信息:')
console.log(' - directRouteMappings:', directRouteMappings)
console.log(' - routes:', directRouteMappings?.routes)
console.log(' - 导入是否成功:', directRouteMappings !== undefined)
return []
}
console.log(`📊 找到 ${directRouteMappings.routes.length} 个路由配置`)
// 从路由配置中提取路由信息,并关联API映射
const frontendRoutes = directRouteMappings.routes.map(route => {
console.log(`📋 处理路由: ${route.path} (${route.component})`)
// 查找该路由对应的API调用
const apiCalls = this._findApiCallsForRoute(route)
return {
path: route.path,
name: route.name,
module: route.module,
description: route.description || `${route.name || route.component}页面`,
type: 'list',
component: route.component,
apiCalls: apiCalls
}
})
this.routes = frontendRoutes
console.log(`✅ 成功收集到 ${frontendRoutes.length} 个前端路由`)
return frontendRoutes
} catch (error) {
console.error('❌ 从生成的路由映射文件收集路由失败:', error)
console.error('错误堆栈:', error.stack)
return []
}
}
// 查找路由对应的API调用
_findApiCallsForRoute(route) {
const apiCalls = []
if (!directRouteMappings.apiMappings) {
return apiCalls
}
// 遍历API映射,找到与当前路由相关的调用
directRouteMappings.apiMappings.forEach(moduleMapping => {
if (moduleMapping.module === route.module) {
moduleMapping.apiMappings.forEach(apiMapping => {
// 检查API调用是否与当前路由相关
const isRelated = apiMapping.callingComponents.some(component =>
component.component === route.component ||
component.path === route.path
)
if (isRelated) {
apiCalls.push({
type: 'api',
method: apiMapping.method,
path: apiMapping.path,
methodName: apiMapping.methodName,
serviceName: moduleMapping.serviceName
})
}
})
}
})
return apiCalls
}
// 获取菜单路由
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
}
}