fix:修改客服报错问题

This commit is contained in:
李伊帆
2026-10-23 11:09:37 +08:00
parent 28531c8120
commit c288d6914e
6 changed files with 205 additions and 36 deletions

View File

@@ -22,9 +22,42 @@ export function unwrapApiData<T>(body: ApiResponse<T> & Record<string, unknown>)
return body as T
}
const NETWORK_ERROR_PATTERN = /request:fail|network\s*error|unable to resolve host|no address associated|failed to connect|connection\s*(refused|reset|timed out|timeout)|timed?\s*out|offline|statuscode:\s*-1|err_connection|net::/i
const NETWORK_ERROR_PATTERN = /request:fail|uploadFile:fail|downloadFile:fail|chooseImage:fail|connectSocket:fail|network\s*error|network\s*request\s*failed|failed\s+to\s+fetch|fetch\s+failed|load\s+failed|unable to resolve host|no address associated|failed to connect|connection\s*(refused|reset|timed out|timeout)|timed?\s*out|offline|statuscode:\s*(null|-1)|err_connection|net::|the internet connection appears to be/i
/** 将 uni.request / 系统层英文网络错误转为中文提示 */
const UNI_API_FAIL_PATTERN = /(request|uploadFile|downloadFile|chooseImage|connectSocket):fail\b/i
function hasChinese(text: string): boolean {
return /[\u4e00-\u9fff]/.test(text)
}
function isEnglishSystemError(text: string): boolean {
const value = text.trim()
if (!value) return true
if (hasChinese(value)) return false
return /[a-z]/i.test(value)
}
function collectErrorTexts(err: unknown): string[] {
const texts: string[] = []
if (typeof err === 'string') {
texts.push(err)
return texts
}
if (!err || typeof err !== 'object') return texts
const record = err as Record<string, unknown>
if (typeof record.message === 'string') texts.push(record.message)
if (typeof record.errMsg === 'string') texts.push(record.errMsg)
if (typeof record.msg === 'string') texts.push(record.msg)
if (err instanceof Error && err.message) {
texts.push(err.message)
}
return [...new Set(texts.filter(Boolean))]
}
/** 将 uni.request / uploadFile / fetch 等系统层英文网络错误转为中文提示 */
export function normalizeNetworkErrorMessage(
message?: string,
fallback = '网络异常,请检查网络后重试',
@@ -32,24 +65,32 @@ export function normalizeNetworkErrorMessage(
const text = message?.trim() ?? ''
if (!text) return fallback
if (NETWORK_ERROR_PATTERN.test(text)) return fallback
if (UNI_API_FAIL_PATTERN.test(text)) return fallback
if (isEnglishSystemError(text) && /fail|error|timeout|network|statuscode|upload|request|socket|fetch|offline|null/i.test(text)) {
return fallback
}
return text
}
export function getErrorMessage(err: unknown, fallback = '请求失败'): string {
if (err instanceof ApiError) {
return normalizeNetworkErrorMessage(err.message, fallback)
}
if (err instanceof Error && err.message) {
return normalizeNetworkErrorMessage(err.message, fallback)
}
if (err && typeof err === 'object' && 'errMsg' in err) {
const errMsg = String((err as { errMsg?: string }).errMsg || '')
if (errMsg) return normalizeNetworkErrorMessage(errMsg, fallback)
const texts = collectErrorTexts(err)
for (const text of texts) {
const normalized = normalizeNetworkErrorMessage(text, fallback)
if (normalized !== text) return normalized
if (!isEnglishSystemError(text)) return text
}
if (err && typeof err === 'object' && 'data' in err) {
const data = (err as { data?: ApiResponse }).data
if (data?.msg) return data.msg
if (data?.message) return data.message
if (data?.msg && !isEnglishSystemError(data.msg)) return data.msg
if (data?.message && !isEnglishSystemError(data.message)) return data.message
}
return fallback
}
/** 文件上传失败提示(无网 / 系统层错误统一中文) */
export function getUploadErrorMessage(err: unknown): string {
return getErrorMessage(err, '网络异常,请检查网络后重试')
}