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.

366 lines
10 KiB

// 路由收集器 - 收集前端路由信息
import router from '@/router'
// 尝试导入生成的路由映射文件
let generatedMapping = null
try {
generatedMapping = require('./generated-route-mapping.js')
} catch (error) {
console.warn('⚠ 未找到生成的路由映射文件,使用默认收集逻辑')
}
export class RouteCollector {
constructor() {
this.routes = []
}
// 收集所有路由信息
collectRoutes() {
this.routes = []
// 如果存在生成的路由映射,使用它
if (generatedMapping && generatedMapping.mainRoutes) {
console.log('🔧 使用生成的路由映射文件')
this._collectFromGeneratedMapping()
} else {
console.log('🔧 使用默认路由收集逻辑')
this._collectFromRouter(router.getRoutes())
this._addPageOperationRoutes()
}
// 检查所有路由的模块信息
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('⚠ 生成的路由映射文件无效,回退到默认逻辑')
this._collectFromRouter(router.getRoutes())
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}`)
})
}
// 从Vue Router收集路由
_collectFromRouter(routes, parentPath = '') {
routes.forEach(route => {
console.log(`🔍 处理路由: path="${route.path}", parentPath="${parentPath}"`)
if (route.path && !route.path.startsWith('*')) {
const routeInfo = this._extractRouteInfo(route, parentPath)
if (routeInfo) {
console.log(`✅ 添加路由: ${routeInfo.path} -> 模块: ${routeInfo.module}`)
this.routes.push(routeInfo)
} else {
console.log(` 跳过路由: ${route.path}`)
}
}
// 递归处理子路由
if (route.children && route.children.length > 0) {
// 构建正确的父路径,避免双斜杠
let currentPath
if (parentPath && route.path) {
// 确保路径之间只有一个斜杠
currentPath = parentPath.endsWith('/') && route.path.startsWith('/')
? `${parentPath}${route.path.substring(1)}`
: `${parentPath}${route.path}`
} else {
currentPath = parentPath || route.path
}
console.log(`🔄 递归处理子路由,当前路径: ${currentPath}`)
this._collectFromRouter(route.children, currentPath)
}
})
}
// 添加页面内操作路由
_addPageOperationRoutes() {
console.log('🔍 开始添加页面内操作路由...')
// 用户管理页面的操作路由
this._addUserManagementOperations()
// 角色管理页面的操作路由
this._addRoleManagementOperations()
console.log('✅ 页面内操作路由添加完成')
}
// 添加用户管理页面的操作路由
_addUserManagementOperations() {
const userManagementRoute = this.routes.find(r => r.path === '/user-management')
if (!userManagementRoute) {
console.log('⚠ 未找到用户管理路由,跳过操作路由添加')
return
}
const operations = [
{
path: '/user-management/create',
name: 'CreateUser',
description: '新增用户',
operation: 'create'
},
{
path: '/user-management/edit',
name: 'EditUser',
description: '编辑用户',
operation: 'edit'
},
{
path: '/user-management/delete',
name: 'DeleteUser',
description: '删除用户',
operation: 'delete'
},
{
path: '/user-management/assign-roles',
name: 'AssignUserRoles',
description: '分配用户角色',
operation: 'assign-roles'
},
{
path: '/user-management/enable',
name: 'EnableUser',
description: '启用用户',
operation: 'enable'
},
{
path: '/user-management/disable',
name: 'DisableUser',
description: '禁用用户',
operation: 'disable'
}
]
operations.forEach(op => {
const operationRoute = {
path: op.path,
name: op.name,
component: 'UserManagementModal',
meta: { operation: op.operation },
module: 'user-management',
description: op.description,
isMenu: false,
sort: 0,
icon: '',
status: 1
}
console.log(`✅ 添加用户管理操作路由: ${op.path} -> ${op.description}`)
this.routes.push(operationRoute)
})
}
// 添加角色管理页面的操作路由
_addRoleManagementOperations() {
const roleManagementRoute = this.routes.find(r => r.path === '/role-management')
if (!roleManagementRoute) {
console.log('⚠ 未找到角色管理路由,跳过操作路由添加')
return
}
const operations = [
{
path: '/role-management/create',
name: 'CreateRole',
description: '新增角色',
operation: 'create'
},
{
path: '/role-management/edit',
name: 'EditRole',
description: '编辑角色',
operation: 'edit'
},
{
path: '/role-management/delete',
name: 'DeleteRole',
description: '删除角色',
operation: 'delete'
},
{
path: '/role-management/assign-permissions',
name: 'AssignRolePermissions',
description: '分配角色权限',
operation: 'assign-permissions'
}
]
operations.forEach(op => {
const operationRoute = {
path: op.path,
name: op.name,
component: 'RoleManagementModal',
meta: { operation: op.operation },
module: 'role-management',
description: op.description,
isMenu: false,
sort: 0,
icon: '',
status: 1
}
console.log(`✅ 添加角色管理操作路由: ${op.path} -> ${op.description}`)
this.routes.push(operationRoute)
})
}
// 提取路由信息
_extractRouteInfo(route, parentPath = '') {
// 构建完整路径,避免双斜杠
let fullPath
if (parentPath && route.path) {
// 确保路径之间只有一个斜杠
fullPath = parentPath.endsWith('/') && route.path.startsWith('/')
? `${parentPath}${route.path.substring(1)}`
: `${parentPath}${route.path}`
} else {
fullPath = parentPath || route.path
}
// 跳过通配符路径,但保留根路径的子路由
if (fullPath.includes('*')) {
return null
}
// 如果是根路径且没有子路由,跳过
if (fullPath === '/' && (!route.children || route.children.length === 0)) {
return null
}
return {
path: fullPath,
name: route.name || '',
component: route.component ? route.component.name || 'Unknown' : 'Unknown',
meta: route.meta || {},
module: this._extractModuleFromPath(fullPath),
description: this._generateDescription(route),
isMenu: this._isMenuRoute(route),
sort: route.meta?.sort || 0,
icon: route.meta?.icon || '',
status: 1
}
}
// 从路径中提取模块名
_extractModuleFromPath(path) {
// 移除开头的斜杠,并处理双斜杠
let cleanPath = path.startsWith('/') ? path.substring(1) : path
// 处理双斜杠情况
if (cleanPath.startsWith('/')) {
cleanPath = cleanPath.substring(1)
}
if (!cleanPath) {
console.log(`🔍 路径 "${path}" -> 模块 "core" (空路径)`)
return 'core'
}
// 分割路径
const parts = cleanPath.split('/')
const firstPart = parts[0]
// 映射常见的模块名
const moduleMap = {
'user-management': 'user-management',
'user-profile': 'user-management',
'role-management': 'role-management',
'settings': 'system-settings',
'system-settings': 'system-settings',
'route-sync-test': 'route-sync'
}
const module = moduleMap[firstPart] || firstPart
console.log(`🔍 路径 "${path}" -> 模块 "${module}" (firstPart: "${firstPart}")`)
return module
}
// 生成路由描述
_generateDescription(route) {
const name = route.name || ''
const path = route.path || ''
if (name) {
return `${name}页面`
}
// 根据路径生成描述
const pathParts = path.split('/').filter(p => p)
if (pathParts.length > 0) {
const lastPart = pathParts[pathParts.length - 1]
return `${lastPart}管理`
}
return '页面'
}
// 判断是否为菜单路由
_isMenuRoute(route) {
// 有name的路由通常是菜单路由
if (route.name) return true
// 有meta.menu的路由是菜单路由
if (route.meta && route.meta.menu) return true
// 没有子路由的路由通常是页面路由
if (!route.children || route.children.length === 0) return true
return false
}
// 获取菜单路由
getMenuRoutes() {
return this.routes.filter(route => route.isMenu)
}
// 获取页面路由
getPageRoutes() {
return this.routes.filter(route => !route.isMenu)
}
// 获取操作路由
getOperationRoutes() {
return this.routes.filter(route => route.meta && route.meta.operation)
}
// 按模块分组路由
getRoutesByModule() {
const grouped = {}
this.routes.forEach(route => {
const module = route.module
if (!grouped[module]) {
grouped[module] = []
}
grouped[module].push(route)
})
return grouped
}
}