front/src/utils/menu-i18n.ts
zzh 02fb9c4f28
All checks were successful
Build and Deploy Front / deploy (push) Successful in 6m37s
feat: 切换 bcdship 域名并收敛 deployTargets,完善微信支付充值与 AI 助手顶栏
将站点/API 默认值收敛到配置,避免再次全局改域名;同步 H5/PC 支付充值与助手浮层交互调整。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-12-01 18:31:30 +08:00

242 lines
8.9 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.

import type { ComposerTranslation } from 'vue-i18n'
import { appRoutes } from '@/router/routes'
import type { AppRouteRecordRaw } from '@/router/routes/types'
import { normalizeMenuPath } from '@/utils/menu-display'
import i18n from '@/locale'
/** RBAC 菜单中文名 → locale key(路由匹配失败时的兜底;含重名时以更常用模块为准) */
export const MENU_NAME_TO_LOCALE_KEY: Record<string, string> = {
首页: 'menu.home',
客户: 'menu.user',
客户管理: 'menu.user.customer',
子账号: 'menu.user.subAccount',
地址管理: 'menu.user.senderAddress',
出口资质备案: 'menu.user.exportQualification',
个人中心: 'menu.user.profile',
资金充值: 'menu.user.recharge',
财务: 'menu.finance',
充值记录: 'menu.finance.rechargeRecord',
资金流水: 'menu.finance.accountFlow',
应收应付: 'menu.finance.receivablePayable',
发票管理: 'menu.finance.invoiceManage',
抬头管理: 'menu.finance.invoiceProfile',
我的发票: 'menu.user.invoice',
资金流水: 'menu.user.accountFlow',
营销: 'menu.marketing',
营销指引: 'menu.marketing.marketingGuide',
订单: 'menu.order',
订单指引: 'menu.order.orderGuide',
新手指引: 'menu.order.orderGuide',
我要查价: 'menu.order.priceCheck',
我要下单: 'menu.order.placeOrder',
模板字段映射: 'menu.order.templateMapping',
我的订单: 'menu.order.myOrders',
运单管理: 'menu.order.waybillManage',
揽收订单: 'menu.order.pickupOrder',
'9610订单': 'menu.order.customs9610Order',
批量任务: 'menu.order.batchJobs',
店铺授权: 'menu.order.platformStore',
店铺订单: 'menu.order.platformStoreOrder',
仓储: 'menu.warehouse',
仓储指引: 'menu.warehouse.warehouseGuide',
我的包裹: 'menu.warehouse.parcel',
我的库存: 'menu.warehouse.inventory',
'我的 SKU': 'menu.warehouse.sku',
入库预报: 'menu.warehouse.inboundNotice',
库位管理: 'menu.warehouse.binLocation',
仓租设置: 'menu.warehouse.storageRent',
渠道: 'menu.channel',
渠道指引: 'menu.channel.channelGuide',
销售产品: 'menu.channel.salesProducts',
服务渠道: 'menu.channel.serviceChannels',
价格分区: 'menu.channel.priceArea',
黑名单区: 'menu.channel.blacklist',
偏远分区: 'menu.channel.remoteZones',
计重规则: 'menu.channel.weightRules',
计重进位: 'menu.channel.weightRounding',
自动触发: 'menu.channel.autoTrigger',
问题模板: 'menu.channel.issueTemplate',
标签设计: 'menu.channel.labelDesign',
轨迹: 'menu.track',
轨迹指引: 'menu.track.trackGuide',
轨迹订单: 'menu.track.trackOrder',
预警订单: 'menu.track.warningOrder',
轨迹状态: 'menu.track.trackState',
轨迹标记: 'menu.track.trackFlag',
轨迹内容: 'menu.track.trackContent',
轨迹拉取: 'menu.track.trackPull',
轨迹推送: 'menu.track.trackPush',
预警配置: 'menu.track.kpiEarlyWarningConfig',
操作: 'menu.operation',
操作指引: 'menu.operation.operationGuide',
入库扫描: 'menu.operation.inboundScan',
出库扫描: 'menu.operation.outboundScan',
装袋作业: 'menu.operation.bagPack',
大包管理: 'menu.operation.bagManage',
打板作业: 'menu.operation.masterBoard',
主单管理: 'menu.operation.masterBill',
打印标签: 'menu.operation.printLabel',
快递到仓: 'menu.operation.parcelArrival',
API: 'menu.api',
上游接口: 'menu.api.upstreamInterface',
下游接口: 'menu.api.downstreamInterface',
创建接口: 'menu.api.createInterface',
修改接口: 'menu.api.editInterface',
App列表: 'menu.api.appList',
App管理: 'menu.api.appList',
应用管理: 'menu.api.appList',
请求日志: 'menu.api.requestLog',
调用统计: 'menu.api.callStatistics',
创建应用: 'menu.api.createApplication',
API调试: 'menu.api.apiDebugger',
'API LIKE': 'menu.api.apiLike',
API测试: 'menu.api.apiTest',
OpenAPI: 'menu.api.openApi',
系统: 'menu.system',
组织管理: 'menu.system.organization',
用户管理: 'menu.system.user',
职位管理: 'menu.system.position',
角色管理: 'menu.system.role',
模块管理: 'menu.system.module',
菜单管理: 'menu.system.menu',
数据字典: 'menu.system.dict',
其他配置: 'menu.system.otherConfig',
系统配置: 'menu.system.config',
公告通知: 'menu.system.announcement',
访问日志: 'menu.system.vislog',
操作日志: 'menu.system.oplog',
系统接口: 'menu.system.interface',
接口配置: 'menu.system.interface.config',
仓库地址: 'menu.system.warehouseAddress',
国家地区: 'menu.system.country',
批量发送邮件: 'menu.system.batchEmail',
'AI 助手': 'menu.system.aiAssistant',
'AI Skill': 'menu.system.aiSkills',
'MCP 工具': 'menu.system.mcpTools',
数据权限: 'menu.system.dataPerm',
用户数据权限: 'menu.system.dataPermUsers',
权限模板: 'menu.system.dataPermTemplates',
权限审计: 'menu.system.dataPermAudit',
小红书: 'menu.xhs',
启动服务: 'menu.xhs.startService',
内容分析: 'menu.xhs.analysis',
爆款创作: 'menu.xhs.creation',
发布记录: 'menu.xhs.publishHistory',
技能卡片: 'menu.xhs.skills',
拟人设置: 'menu.xhs.humanSettings',
知识库: 'menu.kb',
培训手册: 'menu.kb.training',
分类管理: 'menu.kb.category',
文章管理: 'menu.kb.article',
}
export type MenuTitleSource = {
name?: string | null
route?: string | null
path?: string | null
meta?: { locale?: string; title?: string } | null
}
function joinRoutePath(parentPath: string, childPath: string): string {
if (childPath.startsWith('/')) return normalizeMenuPath(childPath)
const base = normalizeMenuPath(parentPath)
return normalizeMenuPath(`${base}/${childPath}`)
}
function collectPathLocales(
routes: AppRouteRecordRaw[],
parentPath = '',
map = new Map<string, string>(),
): Map<string, string> {
for (const route of routes) {
const fullPath = route.path.startsWith('/')
? normalizeMenuPath(route.path)
: joinRoutePath(parentPath, route.path)
const locale = route.meta?.locale
if (typeof locale === 'string' && locale.trim()) {
map.set(fullPath, locale.trim())
}
if (route.children?.length) {
collectPathLocales(route.children as AppRouteRecordRaw[], fullPath, map)
}
}
return map
}
let cachedPathToLocale: Map<string, string> | null = null
export function getMenuPathToLocaleKeyMap(): Map<string, string> {
if (!cachedPathToLocale) {
cachedPathToLocale = collectPathLocales(appRoutes as unknown as AppRouteRecordRaw[])
}
return cachedPathToLocale
}
function findBestPathLocaleKey(path: string, map: Map<string, string>): string | undefined {
if (map.has(path)) return map.get(path)
let best = ''
let bestKey: string | undefined
for (const [menuPath, key] of map) {
if (path === menuPath || path.startsWith(`${menuPath}/`)) {
if (menuPath.length > best.length) {
best = menuPath
bestKey = key
}
}
}
return bestKey
}
/** 解析菜单对应的 i18n key(优先 route.meta.locale / 路由 path,其次中文名映射) */
export function resolveMenuI18nKey(menu: MenuTitleSource): string | undefined {
const metaLocale = menu.meta?.locale
if (typeof metaLocale === 'string' && metaLocale.trim()) {
return metaLocale.trim()
}
const path = normalizeMenuPath(menu.route || menu.path || '')
if (path) {
const key = findBestPathLocaleKey(path, getMenuPathToLocaleKeyMap())
if (key) return key
}
const name = (menu.name || menu.meta?.title || '').trim()
if (!name) return undefined
if (name.startsWith('menu.')) return name
return MENU_NAME_TO_LOCALE_KEY[name]
}
export function translateMenuTitle(
t: ComposerTranslation | ((key: string) => string),
menu: MenuTitleSource,
): string {
const rbacName = (menu.name || menu.meta?.title || '').trim()
const key = resolveMenuI18nKey(menu)
const locale = String(i18n.global.locale.value || 'zh-CN')
const isZh = locale.toLowerCase().startsWith('zh')
// 中文:菜单管理配置名优先(与 RBAC 一致,避免被语言包简称覆盖,如「揽收订单」→「揽收单」)
if (isZh && rbacName && !rbacName.startsWith('menu.')) {
return rbacName
}
// 非中文:跟语言包走,保证切英文能切换;无文案再回退 RBAC 名
if (key) {
const translated = String(t(key))
if (translated && translated !== key) return translated
}
if (rbacName && !rbacName.startsWith('menu.')) return rbacName
return rbacName || key || ''
}
/** 在非 setup 场景(如 pinia)用全局 i18n 翻译路径标题;fallback 为菜单管理名称时直接使用 */
export function translateMenuTitleByPath(path: string, fallback = ''): string {
const trimmedFallback = fallback.trim()
if (trimmedFallback) return trimmedFallback
const key = findBestPathLocaleKey(normalizeMenuPath(path), getMenuPathToLocaleKeyMap())
if (!key) return trimmedFallback
const translated = String(i18n.global.t(key))
return translated && translated !== key ? translated : trimmedFallback
}