Files
one-pipe-system/src/utils/business/exchangeError.ts
luo c31183205d
All checks were successful
构建并部署前端到测试环境 / deploy (push) Successful in 56s
fix: some
2026-10-13 17:21:46 +08:00

48 lines
1.8 KiB
TypeScript

type BusinessFailure = {
code?: number | string
msg?: string
message?: string
data?: { phone?: unknown }
}
const getBusinessFailure = (value: unknown): BusinessFailure | undefined => {
if (!value || typeof value !== 'object') return undefined
const candidate = value as { response?: { data?: BusinessFailure } } & BusinessFailure
return candidate.response?.data || candidate
}
export const getExchangePhoneConflictMessage = (value: unknown): string | undefined => {
const failure = getBusinessFailure(value)
if (Number(failure?.code) !== 1202) return undefined
const phone = typeof failure?.data?.phone === 'string' ? failure.data.phone.trim() : ''
return `新资产已绑定手机号${phone ? ` ${phone}` : ''},请先到『手机号资产关联』解除后再提交`
}
export const getExchangeBusinessErrorMessage = (value: unknown): string | undefined => {
const failure = getBusinessFailure(value)
return failure?.msg || failure?.message
}
export interface ExchangeNewAssetContext {
asset_identifier?: string
associated_phones?: string[] | null
}
// 新资产上下文由提交时选中的资产提供,绝不回退到旧资产。
export const getExchangePhoneAssociationQuery = (
failure: unknown,
newAsset: ExchangeNewAssetContext
): { asset_identifier: string; phone: string } | undefined => {
const response = getBusinessFailure(failure)
if (Number(response?.code) !== 1202) return undefined
const asset_identifier = newAsset.asset_identifier?.trim() || ''
const responsePhone = typeof response?.data?.phone === 'string' ? response.data.phone.trim() : ''
const phone =
responsePhone ||
newAsset.associated_phones
?.find((value) => typeof value === 'string' && value.trim())
?.trim() ||
''
return asset_identifier && phone ? { asset_identifier, phone } : undefined
}