sync: 对齐小程序 v1.0.2 生产代码并脱敏
This commit is contained in:
+23
-6
@@ -10,8 +10,8 @@ import type {
|
||||
CardLifecycleStatusParams,
|
||||
CardListParams,
|
||||
CardListRes,
|
||||
CardRecognitionRes,
|
||||
CardQueryRes,
|
||||
CardRecognitionRes,
|
||||
ChangePrepareParams,
|
||||
ChangePrepareRes,
|
||||
ChangeSubmitParams,
|
||||
@@ -57,6 +57,20 @@ export function publicQueryCard(data: PublicQueryCardParams) {
|
||||
return http.post<CardQueryRes>('/card/public-query', data, undefined, undefined, CARD_REQUEST_OPTIONS)
|
||||
}
|
||||
|
||||
interface CardRecognitionPayload {
|
||||
code?: number
|
||||
message?: string
|
||||
data?: CardRecognitionRes
|
||||
}
|
||||
|
||||
function parseCardRecognitionPayload(data: unknown): CardRecognitionPayload {
|
||||
if (data && typeof data === 'object')
|
||||
return data as CardRecognitionPayload
|
||||
|
||||
const text = String(data ?? '').replace(/^\uFEFF/, '').trim()
|
||||
return text ? JSON.parse(text) as CardRecognitionPayload : {}
|
||||
}
|
||||
|
||||
export function recognizeCardImage(filePath: string) {
|
||||
return new Promise<CardRecognitionRes>((resolve, reject) => {
|
||||
uni.uploadFile({
|
||||
@@ -65,17 +79,20 @@ export function recognizeCardImage(filePath: string) {
|
||||
name: 'file',
|
||||
timeout: 30000,
|
||||
success: (response) => {
|
||||
let payload: { code?: number, message?: string, data?: CardRecognitionRes }
|
||||
let payload: CardRecognitionPayload
|
||||
try {
|
||||
payload = JSON.parse(String(response.data || '{}'))
|
||||
payload = parseCardRecognitionPayload(response.data)
|
||||
}
|
||||
catch {
|
||||
reject({ message: '图片识别响应格式错误' })
|
||||
const headers = (response as typeof response & { header?: Record<string, string> }).header
|
||||
const contentType = headers?.['content-type'] || headers?.['Content-Type']
|
||||
const responseType = contentType ? `,${contentType}` : ''
|
||||
reject(new Error(`图片识别响应格式错误(HTTP ${response.statusCode}${responseType})`))
|
||||
return
|
||||
}
|
||||
|
||||
if (response.statusCode < 200 || response.statusCode >= 300 || Number(payload.code) !== 0) {
|
||||
reject({ message: payload.message || '图片识别失败' })
|
||||
reject(new Error(payload.message || '图片识别失败'))
|
||||
return
|
||||
}
|
||||
resolve(payload.data || {
|
||||
@@ -88,7 +105,7 @@ export function recognizeCardImage(filePath: string) {
|
||||
})
|
||||
},
|
||||
fail: (error) => {
|
||||
reject({ message: error.errMsg || '图片上传失败,请检查网络后重试' })
|
||||
reject(new Error(error.errMsg || '图片上传失败,请检查网络后重试'))
|
||||
},
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user