This commit is contained in:
@@ -7,6 +7,7 @@ import { ref } from 'vue'
|
||||
import { ElMessage } from 'element-plus'
|
||||
import { AssetService } from '@/api/modules'
|
||||
import { FrontendRateLimitError } from '@/utils/business/apiRateLimit'
|
||||
import { normalizeApiError } from '@/utils/business/apiError'
|
||||
import type {
|
||||
AssetBoundCard,
|
||||
AssetWalletResponse,
|
||||
@@ -32,6 +33,53 @@ const NULLABLE_REALTIME_CARD_FIELDS = new Set([
|
||||
|
||||
// 详情接口是绑定卡实名时间的唯一来源;实时状态接口不应覆盖或保留旧值。
|
||||
const DETAIL_ONLY_BOUND_CARD_FIELDS = new Set(['real_name_at'])
|
||||
const ASSET_NOT_FOUND_CODE = 1006
|
||||
|
||||
const isLegacyNineteenDigitIdentifier = (identifier: string) => /^89\d{17}$/.test(identifier)
|
||||
|
||||
const isAssetNotFound = (value: any) =>
|
||||
value?.code === ASSET_NOT_FOUND_CODE || value?.response?.data?.code === ASSET_NOT_FOUND_CODE
|
||||
|
||||
const isUnauthorized = (value: any) =>
|
||||
value?.code === 401 || value?.response?.status === 401 || value?.response?.data?.code === 401
|
||||
|
||||
const calculateLuhnCheckDigit = (identifier: string) => {
|
||||
let sum = 0
|
||||
let doubleDigit = true
|
||||
|
||||
for (let index = identifier.length - 1; index >= 0; index -= 1) {
|
||||
let digit = Number(identifier[index])
|
||||
if (doubleDigit) {
|
||||
digit *= 2
|
||||
if (digit > 9) digit -= 9
|
||||
}
|
||||
sum += digit
|
||||
doubleDigit = !doubleDigit
|
||||
}
|
||||
|
||||
return String((10 - (sum % 10)) % 10)
|
||||
}
|
||||
|
||||
const resolveAssetForAssetInfo = async (identifier: string) => {
|
||||
const params = { include_usage_summary: true }
|
||||
if (!isLegacyNineteenDigitIdentifier(identifier)) {
|
||||
return AssetService.resolveAsset(identifier, params)
|
||||
}
|
||||
|
||||
const silentConfig = { requestOptions: { errorMessageMode: 'none' as const } }
|
||||
let firstResponse = null
|
||||
|
||||
try {
|
||||
firstResponse = await AssetService.resolveAsset(identifier, params, silentConfig)
|
||||
} catch (error) {
|
||||
if (!isAssetNotFound(error)) throw error
|
||||
}
|
||||
|
||||
if (firstResponse && !isAssetNotFound(firstResponse)) return firstResponse
|
||||
|
||||
const correctedIdentifier = `${identifier}${calculateLuhnCheckDigit(identifier)}`
|
||||
return AssetService.resolveAsset(correctedIdentifier, params, silentConfig)
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化数据大小(MB -> GB/MB)
|
||||
@@ -96,9 +144,10 @@ export function useAssetInfo() {
|
||||
* @param identifier - 资产标识符(ICCID、IMEI、SN、虚拟号或MSISDN)
|
||||
*/
|
||||
const fetchAssetDetail = async (identifier: string) => {
|
||||
const shouldShowSilentRequestError = isLegacyNineteenDigitIdentifier(identifier)
|
||||
try {
|
||||
loading.value = true
|
||||
const response = await AssetService.resolveAsset(identifier, { include_usage_summary: true })
|
||||
const response = await resolveAssetForAssetInfo(identifier)
|
||||
|
||||
if (response.code === 0 && response.data) {
|
||||
const data = response.data
|
||||
@@ -199,8 +248,12 @@ export function useAssetInfo() {
|
||||
ElMessage.error(response.msg || '查询失败')
|
||||
cardInfo.value = null
|
||||
}
|
||||
} catch {
|
||||
} catch (error: any) {
|
||||
cardInfo.value = null
|
||||
if (shouldShowSilentRequestError && !isUnauthorized(error)) {
|
||||
const backendMessage = error?.response?.data?.msg
|
||||
ElMessage.error(backendMessage || normalizeApiError(error).message)
|
||||
}
|
||||
} finally {
|
||||
loading.value = false
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user