上传小程序修改合入
This commit is contained in:
@@ -13,6 +13,7 @@ class ApiError extends Error {
|
||||
|
||||
function getErrorDetail(data: any): string {
|
||||
if (!data) return 'Request failed'
|
||||
if (typeof data.errMsg === 'string') return data.errMsg
|
||||
if (typeof data.detail === 'string') return data.detail
|
||||
if (Array.isArray(data.detail)) {
|
||||
return data.detail.map((item: any) => String(item?.msg || item)).join('; ')
|
||||
@@ -30,6 +31,8 @@ async function request<T>(
|
||||
} = {}
|
||||
): Promise<T> {
|
||||
const token = getToken()
|
||||
const requestUrl = `${BASE_URL}${url}`
|
||||
const requestMethod = options.method || 'GET'
|
||||
const headers: Record<string, string> = {
|
||||
'Content-Type': 'application/json',
|
||||
...options.headers,
|
||||
@@ -41,22 +44,19 @@ async function request<T>(
|
||||
let response
|
||||
try {
|
||||
response = await Taro.request({
|
||||
url: `${BASE_URL}${url}`,
|
||||
method: options.method || 'GET',
|
||||
url: requestUrl,
|
||||
method: requestMethod,
|
||||
data: options.data,
|
||||
header: headers,
|
||||
timeout: REQUEST_TIMEOUT_MS,
|
||||
})
|
||||
} catch (error: any) {
|
||||
const message = String(error?.errMsg || error?.message || '')
|
||||
const detail = message.includes('timeout')
|
||||
? '请求超时,请检查后端服务是否可访问'
|
||||
: '网络请求失败,请检查后端地址和网络连接'
|
||||
throw new ApiError(0, detail)
|
||||
const message = String(error?.errMsg || error?.message || 'request:fail')
|
||||
throw new ApiError(0, message)
|
||||
}
|
||||
|
||||
if (response.statusCode === 401) {
|
||||
handleUnauthorized({ redirect: !url.startsWith('/auth/') })
|
||||
handleUnauthorized({ redirect: !url.startsWith('/banban/auth/') })
|
||||
}
|
||||
|
||||
if (response.statusCode >= 400) {
|
||||
|
||||
Reference in New Issue
Block a user