234 lines
7.4 KiB
TypeScript
234 lines
7.4 KiB
TypeScript
/** 下单创建失败:解析草稿标记 + 原因→修改指引 */
|
||
|
||
export type PlaceOrderFailSection = 'basic' | 'sender' | 'recipient' | 'declaration'
|
||
|
||
export interface ParcelCreateDraftMeta {
|
||
draftSaved: boolean
|
||
orderId?: number
|
||
serviceNo?: string
|
||
}
|
||
|
||
export interface ParcelCreateFailGuide {
|
||
/** 原始技术原因(可用于排查) */
|
||
rawReason: string
|
||
/** 对客户展示的人话原因 */
|
||
reason: string
|
||
tips: string[]
|
||
section: PlaceOrderFailSection
|
||
draftSaved: boolean
|
||
orderId?: number
|
||
serviceNo?: string
|
||
/** 是否建议联系客服 */
|
||
suggestSupport?: boolean
|
||
}
|
||
|
||
const OMS_DRAFT_RE = /\[\[omsDraft:(\{[\s\S]*\})\]\]\s*$/
|
||
export const OMS_DRAFT_SAVED_ERROR_CODE = 'OMS_DRAFT_SAVED'
|
||
|
||
export function parseOmsDraftMarker(message: string): {
|
||
cleanMessage: string
|
||
meta?: ParcelCreateDraftMeta
|
||
} {
|
||
const raw = String(message || '')
|
||
const match = raw.match(OMS_DRAFT_RE)
|
||
if (!match || match.index == null) {
|
||
return { cleanMessage: raw.trim() }
|
||
}
|
||
try {
|
||
const parsed = JSON.parse(match[1]) as Record<string, unknown>
|
||
const orderIdRaw = Number(parsed.orderId ?? parsed.OrderId)
|
||
const serviceNo = String(parsed.serviceNo ?? parsed.ServiceNo ?? '').trim()
|
||
return {
|
||
cleanMessage: raw.slice(0, match.index).trim(),
|
||
meta: {
|
||
draftSaved: Boolean(parsed.draftSaved ?? parsed.DraftSaved),
|
||
orderId: Number.isFinite(orderIdRaw) && orderIdRaw > 0 ? orderIdRaw : undefined,
|
||
serviceNo: serviceNo || undefined,
|
||
},
|
||
}
|
||
} catch {
|
||
return { cleanMessage: raw.replace(OMS_DRAFT_RE, '').trim() }
|
||
}
|
||
}
|
||
|
||
/** 从业务错误响应体提取草稿元数据(优先 data/details,兼容 msg 标记) */
|
||
export function extractDraftFailFromBody(body: any): {
|
||
cleanMessage: string
|
||
meta?: ParcelCreateDraftMeta & { errorCode?: string }
|
||
} {
|
||
const rawMsg = String(
|
||
body?.msg ?? body?.Msg ?? body?.message ?? body?.Message ?? '',
|
||
)
|
||
const fromMarker = parseOmsDraftMarker(rawMsg)
|
||
let draftSaved = Boolean(fromMarker.meta?.draftSaved)
|
||
let orderId = fromMarker.meta?.orderId
|
||
let serviceNo = fromMarker.meta?.serviceNo
|
||
let errorCode =
|
||
String(body?.errorCode ?? body?.ErrorCode ?? '').trim() || undefined
|
||
|
||
const candidates = [
|
||
body?.data,
|
||
body?.Data,
|
||
body?.details,
|
||
body?.Details,
|
||
body?.error?.details,
|
||
body?.error?.Details,
|
||
body?.error?.data,
|
||
]
|
||
|
||
for (const candidate of candidates) {
|
||
if (candidate == null) continue
|
||
let obj = candidate
|
||
if (typeof candidate === 'string') {
|
||
const trimmed = candidate.trim()
|
||
if (!trimmed) continue
|
||
try {
|
||
obj = JSON.parse(trimmed)
|
||
} catch {
|
||
continue
|
||
}
|
||
}
|
||
if (!obj || typeof obj !== 'object') continue
|
||
if (obj.draftSaved === true || obj.DraftSaved === true) draftSaved = true
|
||
const id = Number(obj.orderId ?? obj.OrderId)
|
||
if (Number.isFinite(id) && id > 0) orderId = id
|
||
const sn = String(obj.serviceNo ?? obj.ServiceNo ?? '').trim()
|
||
if (sn) serviceNo = sn
|
||
const code = String(obj.errorCode ?? obj.ErrorCode ?? '').trim()
|
||
if (code) errorCode = code
|
||
const reason = String(obj.reason ?? obj.Reason ?? '').trim()
|
||
if (reason && !fromMarker.cleanMessage) {
|
||
fromMarker.cleanMessage = reason
|
||
}
|
||
}
|
||
|
||
if (errorCode === OMS_DRAFT_SAVED_ERROR_CODE) draftSaved = true
|
||
|
||
return {
|
||
cleanMessage: fromMarker.cleanMessage || rawMsg.trim(),
|
||
meta:
|
||
draftSaved || orderId || serviceNo || errorCode
|
||
? { draftSaved, orderId, serviceNo, errorCode }
|
||
: undefined,
|
||
}
|
||
}
|
||
|
||
type GuideRule = {
|
||
test: RegExp
|
||
plainReason: string
|
||
tips: string[]
|
||
section: PlaceOrderFailSection
|
||
suggestSupport?: boolean
|
||
}
|
||
|
||
const GUIDE_RULES: GuideRule[] = [
|
||
{
|
||
test: /黑名单/,
|
||
plainReason: '收件地址在禁运/黑名单范围内',
|
||
tips: ['请更换收件国家、省市、邮编,或换运输方式'],
|
||
section: 'recipient',
|
||
},
|
||
{
|
||
test: /价格分区未匹配/,
|
||
plainReason: '收件邮编/国家未匹配到价格分区',
|
||
tips: ['请核对国家与邮编;仍不行请换运输方式或联系客服'],
|
||
section: 'recipient',
|
||
suggestSupport: true,
|
||
},
|
||
{
|
||
test: /计费重|件重尺|货物重量|长、宽、高|实重/,
|
||
plainReason: '包裹尺寸或重量无法完成计费',
|
||
tips: ['请检查长、宽、高与实重是否填写完整且大于 0'],
|
||
section: 'basic',
|
||
},
|
||
{
|
||
test: /产品卖价未匹配|卖价未匹配|未配置计重|未命中价格|未命中规则|未命中.*快照/,
|
||
plainReason: '当前运输方式暂无可用报价',
|
||
tips: ['请更换运输方式,或联系客服确认线路报价'],
|
||
section: 'basic',
|
||
suggestSupport: true,
|
||
},
|
||
{
|
||
test: /销售产品不存在|产品代码|产品未启用|仓库不存在|仓库ID|货物属性|出口报关/,
|
||
plainReason: '基础信息选择有误或已失效',
|
||
tips: ['请检查运输方式、仓库、货物属性'],
|
||
section: 'basic',
|
||
},
|
||
{
|
||
test: /客户单号已存在/,
|
||
plainReason: '客户单号与已有订单重复',
|
||
tips: ['请修改客户单号后重试'],
|
||
section: 'basic',
|
||
},
|
||
{
|
||
test: /客户单号不能为空|包裹数量/,
|
||
plainReason: '基础必填信息不完整',
|
||
tips: ['请完善基础信息中的必填项'],
|
||
section: 'basic',
|
||
},
|
||
{
|
||
test: /收件人信息不完整/,
|
||
plainReason: '收件人必填信息不完整',
|
||
tips: ['请补全姓名、国家、地址、邮编等'],
|
||
section: 'recipient',
|
||
},
|
||
{
|
||
test: /发件人姓名|sender\s*name/i,
|
||
plainReason: '尚未选择发件人地址',
|
||
tips: ['请选择或维护发件地址'],
|
||
section: 'sender',
|
||
},
|
||
{
|
||
test: /申报明细|申报币种/,
|
||
plainReason: '申报信息不完整',
|
||
tips: ['请检查申报明细与申报币种'],
|
||
section: 'declaration',
|
||
},
|
||
{
|
||
test: /完善手机号和邮箱|联系方式/,
|
||
plainReason: '账户联系方式未完善',
|
||
tips: ['请先完善账户手机号与邮箱'],
|
||
section: 'basic',
|
||
},
|
||
]
|
||
|
||
export function buildParcelCreateFailGuide(error: unknown): ParcelCreateFailGuide {
|
||
const err = error as {
|
||
message?: string
|
||
draftSaved?: boolean
|
||
orderId?: number
|
||
serviceNo?: string
|
||
raw?: any
|
||
response?: { data?: any }
|
||
}
|
||
|
||
const body = err?.raw ?? err?.response?.data
|
||
const fromBody = body ? extractDraftFailFromBody(body) : null
|
||
const rawMessage = String(err?.message || fromBody?.cleanMessage || error || '')
|
||
const { cleanMessage, meta } = fromBody
|
||
? { cleanMessage: fromBody.cleanMessage || parseOmsDraftMarker(rawMessage).cleanMessage, meta: fromBody.meta }
|
||
: parseOmsDraftMarker(rawMessage)
|
||
|
||
const rawReason = cleanMessage || '下单未成功,请检查表单后重试'
|
||
const hit = GUIDE_RULES.find((rule) => rule.test.test(rawReason))
|
||
const draftSaved = Boolean(meta?.draftSaved || err?.draftSaved)
|
||
const orderId = meta?.orderId ?? (Number(err?.orderId) > 0 ? Number(err.orderId) : undefined)
|
||
const serviceNo = meta?.serviceNo || (err?.serviceNo ? String(err.serviceNo) : undefined)
|
||
|
||
return {
|
||
rawReason,
|
||
reason: hit?.plainReason || rawReason,
|
||
tips: hit?.tips?.length ? hit.tips.slice(0, 1) : ['请根据失败原因核对表单后重试'],
|
||
section: hit?.section ?? 'basic',
|
||
draftSaved,
|
||
orderId,
|
||
serviceNo,
|
||
suggestSupport: hit?.suggestSupport ?? !hit,
|
||
}
|
||
}
|
||
|
||
/** 仅根据失败原因文案构建指引(用于我的订单草稿详情) */
|
||
export function buildFailGuideFromReason(reason: string): ParcelCreateFailGuide {
|
||
return buildParcelCreateFailGuide({ message: String(reason || '') })
|
||
}
|