front/src/utils/route-name-mapper.ts
2026-06-04 19:15:28 +08:00

186 lines
5.8 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 菜单路径/代码到路由名称的映射
* 用于将菜单的 code 或 path 转换为正确的路由 name
*/
const routeNameMap: Record<string, string> = {
// 订单相关
'order_place_order': 'PlaceOrder',
'/order/placeOrder': 'PlaceOrder',
'order_price_check': 'PriceCheck',
'/order/priceCheck': 'PriceCheck',
'order_my_orders': 'MyOrders',
'/order/myOrders': 'MyOrders',
'order': 'Order',
'/order': 'Order',
// 首页相关
'home_workplace': 'Workplace',
'/home/workplace': 'Workplace',
'home_monitor': 'Monitor',
'/home/monitor': 'Monitor',
'home': 'Home',
'/home': 'Home',
// 轨迹中心相关
'track': 'TrackCenter',
'/track': 'TrackCenter',
'track_guide': 'TrackGuide',
'/track/guide': 'TrackGuide',
'track_order': 'TrackOrder',
'/track/order': 'TrackOrder',
'track_warning_order': 'TrackWarningOrder',
'/track/warningOrder': 'TrackWarningOrder',
'track_state': 'TrackState',
'/track/state': 'TrackState',
'track_flag': 'TrackFlag',
'/track/flag': 'TrackFlag',
'track_content': 'TrackContent',
'/track/content': 'TrackContent',
'track_pull': 'TrackPull',
'/track/pull': 'TrackPull',
'track_push': 'TrackPush',
'/track/push': 'TrackPush',
'track_kpi_region': 'KpiRegion',
'/track/kpiRegion': 'KpiRegion',
'track_kpi_config': 'KpiEarlyWarningConfig',
'/track/kpiEarlyWarningConfig': 'KpiEarlyWarningConfig',
// 系统管理相关
'system': 'SystemManagement',
'/system': 'SystemManagement',
'system_users': 'SystemUsers',
'/system/users': 'SystemUsers',
'system_roles': 'SystemRoles',
'/system/roles': 'SystemRoles',
'system_menus': 'SystemMenus',
'/system/menus': 'SystemMenus',
'system_organizations': 'SystemOrganizations',
'/system/organizations': 'SystemOrganizations',
'system_announcement': 'SystemAnnouncement',
'/system/announcement': 'SystemAnnouncement',
'system_dict': 'SystemDict',
'/system/dict': 'SystemDict',
'system_warehouseAddress': 'WarehouseAddress',
'/system/warehouseAddress': 'WarehouseAddress',
'system_batchEmail': 'SystemBatchEmail',
'/system/batchEmail': 'SystemBatchEmail',
// API相关
'api': 'Api',
'/api': 'Api',
'system_upstream_interface': 'UpstreamInterface',
'/api/upstreamInterface': 'UpstreamInterface',
'/api/upstreamInterfaceConfig': 'UpstreamInterfaceConfig',
'/api/upstreamInterfaceConfigEdit': 'UpstreamInterfaceConfigEdit',
'system_downstream_interface': 'DownstreamInterface',
'/api/downstreamInterface': 'DownstreamInterface',
'/api/downstreamInterface/newPort': 'NewPort',
'system_app_list': 'AppList',
'/api/appList': 'AppList',
'/api/appList/createApplication': 'CreateApplication',
'system_request_log': 'RequestLog',
'/api/requestLog': 'RequestLog',
'system_call_statistics': 'CallStatistics',
'/api/callStatistics': 'CallStatistics',
'system_api_debugger': 'ApiDebugger',
'/api/apiDebugger': 'ApiDebugger',
// 用户相关
'user': 'User',
'/user': 'User',
'user_recharge': 'VipRechargeDeposit',
'/user/recharge': 'VipRechargeDeposit',
'user_recharge_record': 'VipRechargeRechargeRecord',
'/user/rechargeRecord': 'VipRechargeRechargeRecord',
// 渠道相关
'channel': 'Channel',
'/channel': 'Channel',
'channel_sales_products': 'SalesProducts',
'/channel/salesProducts': 'SalesProducts',
'channel_service_channels': 'ServiceChannels',
'/channel/serviceChannels': 'ServiceChannels',
'channel_price_area': 'PriceArea',
'/channel/priceArea': 'PriceArea',
'channel_blacklist': 'Blacklist',
'/channel/blacklist': 'Blacklist',
'channel_remote_zones': 'RemoteZones',
'/channel/remoteZones': 'RemoteZones',
'channel_weight_rules': 'WeightRules',
'/channel/weightRules': 'WeightRules',
'channel_weight_rounding': 'WeightRounding',
'/channel/weightRounding': 'WeightRounding',
'channel_auto_trigger': 'AutoTrigger',
'/channel/autoTrigger': 'AutoTrigger',
// 可以继续添加其他映射...
};
/**
* 根据路径生成路由名称(PascalCase)
* 例如: /order/placeOrder -> PlaceOrder
*/
function pathToRouteName(path: string): string {
if (!path) return '';
// 移除开头的斜杠
const cleanPath = path.startsWith('/') ? path.slice(1) : path;
// 分割路径并转换为 PascalCase
return cleanPath
.split('/')
.map(segment => {
// 将 kebab-case 或 snake_case 转换为 PascalCase
return segment
.split(/[-_]/)
.map(word => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase())
.join('');
})
.join('');
}
/**
* 根据菜单的 code、path 或 name 获取路由名称
*/
export function getRouteName(menu: { code?: string; path?: string; name?: string }): string {
// 优先使用映射表(code 和 path 的映射优先级最高)
if (menu.code && routeNameMap[menu.code]) {
return routeNameMap[menu.code];
}
if (menu.path && routeNameMap[menu.path]) {
return routeNameMap[menu.path];
}
// 如果 name 看起来像 code(包含下划线),优先使用 code/path 生成
const nameLooksLikeCode = menu.name && menu.name.includes('_');
if (!nameLooksLikeCode && menu.name) {
// name 看起来像正常的路由名称,直接使用
return menu.name;
}
// 根据路径生成路由名称
if (menu.path) {
return pathToRouteName(menu.path);
}
// 如果只有 code,尝试转换
if (menu.code) {
// 将 code 转换为路径格式再生成名称
// 例如: order_place_order -> /order/placeOrder -> PlaceOrder
const pathFromCode = '/' + menu.code.replace(/_/g, '/');
return pathToRouteName(pathFromCode);
}
// 如果 name 存在但看起来像 code,尝试从 name 生成
if (nameLooksLikeCode && menu.name) {
const pathFromName = '/' + menu.name.replace(/_/g, '/');
return pathToRouteName(pathFromName);
}
// 兜底:返回空字符串
return '';
}