fix:修改客服报错问题
This commit is contained in:
@@ -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, '网络异常,请检查网络后重试')
|
||||
}
|
||||
|
||||
47
src/utils/localized-toast.ts
Normal file
47
src/utils/localized-toast.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
import { normalizeNetworkErrorMessage } from '@/utils/api-error'
|
||||
|
||||
const NETWORK_TOAST = '网络异常,请检查网络后重试'
|
||||
|
||||
function localizeToastTitle(title: unknown): string {
|
||||
return normalizeNetworkErrorMessage(String(title ?? ''), NETWORK_TOAST)
|
||||
}
|
||||
|
||||
function patchUniShowToast() {
|
||||
if (typeof uni === 'undefined' || typeof uni.showToast !== 'function') return
|
||||
const marker = '__localizedToastPatched__'
|
||||
if ((uni.showToast as { [marker]?: boolean })[marker]) return
|
||||
|
||||
const raw = uni.showToast.bind(uni)
|
||||
const patched = (options: string | UniApp.ShowToastOptions) => {
|
||||
if (typeof options === 'string') {
|
||||
return raw({ title: localizeToastTitle(options), icon: 'none' })
|
||||
}
|
||||
return raw({
|
||||
...options,
|
||||
title: localizeToastTitle(options?.title),
|
||||
})
|
||||
}
|
||||
;(patched as { [marker]?: boolean })[marker] = true
|
||||
uni.showToast = patched as typeof uni.showToast
|
||||
}
|
||||
|
||||
function patchNativeToast() {
|
||||
// #ifdef APP-PLUS
|
||||
if (typeof plus === 'undefined') return
|
||||
const nativeUI = plus.nativeUI as {
|
||||
toast?: (message: string, styles?: unknown) => void
|
||||
__localizedToastPatched__?: boolean
|
||||
}
|
||||
if (!nativeUI?.toast || nativeUI.__localizedToastPatched__) return
|
||||
|
||||
const raw = nativeUI.toast.bind(nativeUI)
|
||||
nativeUI.toast = (message: string, styles?: unknown) => raw(localizeToastTitle(message), styles)
|
||||
nativeUI.__localizedToastPatched__ = true
|
||||
// #endif
|
||||
}
|
||||
|
||||
/** 启动时安装,拦截框架自动弹出的英文 uploadFile/request 错误 */
|
||||
export function installLocalizedToast() {
|
||||
patchUniShowToast()
|
||||
patchNativeToast()
|
||||
}
|
||||
Reference in New Issue
Block a user