import { ElMessage, ElMessageBox } from 'element-plus' import { getT } from '@/utils/i18nHelper' // ============================================================ // M01-01 产品主数据错误码映射(向后兼容,保留原有导出) // ============================================================ export interface ErrorMapping { msg: string type: 'field' | 'toast' // field=字段级红字, toast=全局Toast提示 field?: string // type='field' 时关联的表单字段名 } export const PRODUCT_ERROR_MAP: Record = { '3100_crm_product_001': { msg: '产品ID不能为空', type: 'toast' }, '3100_crm_product_002': { msg: '产品编码不能为空', type: 'field', field: 'productCode' }, '3100_crm_product_003': { msg: '产品名称不能为空', type: 'field', field: 'productName' }, '3100_crm_product_004': { msg: '产品大类不能为空', type: 'field', field: 'category' }, '3100_crm_product_005': { msg: '产品数据不存在', type: 'toast' }, '3100_crm_product_006': { msg: '标准化属性不能为空', type: 'field', field: 'standardized' }, '3100_crm_product_007': { msg: '产品编码已存在', type: 'field', field: 'productCode' }, '3100_crm_product_008': { msg: '产品阶段不能为空', type: 'field', field: 'stage' }, '3100_crm_product_009': { msg: '该产品已下架,不可使用', type: 'toast' }, 'NOT_NULL_PARAM': { msg: '请求参数不能为空', type: 'toast' }, } export function getErrorMapping(code: string): ErrorMapping | undefined { return PRODUCT_ERROR_MAP[code] } // ============================================================ // M06-01 销售预测错误码映射与处理 // ============================================================ // 需要弹窗确认的错误码(而非 Toast 消息) const DIALOG_ERROR_CODES = new Set(['version_conflict']) /** * 处理 API 错误响应 * 错误文案统一从 i18n forecast-order.error.{code} 获取,已覆盖中英文 */ export function handleApiError(code: string | number, msg?: string): void { const t = getT() const codeStr = String(code) const i18nKey = `forecast-order.error.${codeStr}` const translated = t(i18nKey) // 如果 i18n 有对应翻译,使用翻译;否则使用服务器返回消息或未知错误兜底 const message = translated !== i18nKey ? translated : (msg || t('forecast-order.error.unknownError')) if (DIALOG_ERROR_CODES.has(codeStr)) { ElMessageBox.alert(message, t('forecast-order.common.confirm'), { confirmButtonText: t('forecast-order.common.confirm'), type: 'warning', }) } else { ElMessage.error(message) } } // errorCodeMap 已移除,错误文案统一从 i18n forecast-order.error.{code} 获取