// 路由收集器 - 收集前端路由信息 import router from '@/router' import { RouteUtils } from './RouteUtils.js' // 加载生成的路由映射文件 const generatedMapping = RouteUtils.loadGeneratedMapping() export class RouteCollector { constructor() { this.routes = [] } // 收集所有路由信息 collectRoutes() { this.routes = [] // 如果存在生成的路由映射,使用它 if (generatedMapping && generatedMapping.mainRoutes) { console.log('🔧 使用生成的路由映射文件') this._collectFromGeneratedMapping() } else { console.log('🔧 生成的路由映射文件不存在,无法收集路由') console.warn('⚠️ 请确保 route-mapping-plugin 已正确生成路由映射文件') } // 检查所有路由的模块信息 console.log('🔍 检查收集到的路由模块信息:') this.routes.forEach((route, index) => { if (!route.module) { console.error(`❌ 路由 ${index} 缺少模块信息:`, route) // 尝试重新提取模块信息 route.module = this._extractModuleFromPath(route.path) console.log(`🔧 重新提取模块信息: ${route.path} -> ${route.module}`) } }) return this.routes } // 从生成的路由映射收集 _collectFromGeneratedMapping() { if (!generatedMapping || !generatedMapping.mainRoutes) { console.warn('⚠️ 生成的路由映射文件无效,无法收集路由') return } console.log('📋 从生成的路由映射收集主入口路由:') generatedMapping.mainRoutes.forEach(route => { this.routes.push({ path: route.path, name: route.name, module: route.module, description: route.description, type: route.type }) console.log(`✅ 添加主入口路由: ${route.path} -> 模块: ${route.module}`) }) } // 从路径中提取模块名 _extractModuleFromPath(path) { return RouteUtils.extractModuleFromPath(path) } // 获取菜单路由 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 } }