forked from natuka/web.puabadge.com
新增:京东下单页面
This commit is contained in:
1 parent
8e8cde9ef5
commit
007c675603
25 files changed
+3264
-25
No files matched your search
+53
-18
@@ -69,30 +69,65 @@ const requestHandler = (config: InternalAxiosRequestConfig): InternalAxiosReques
|
||||
// Add a request interceptor
|
||||
request.interceptors.request.use(requestHandler, errorHandler)
|
||||
|
||||
function coerceJsonPayload(payload: any) {
|
||||
// 后端若用 text/plain 返回 JSON,axios 不会自动 parse,这里补一层
|
||||
if (typeof payload !== 'string') return payload
|
||||
const text = payload.replace(/^\uFEFF/, '').trim()
|
||||
if (!text) return payload
|
||||
try {
|
||||
return JSON.parse(text)
|
||||
} catch {
|
||||
const start = text.indexOf('{')
|
||||
const end = text.lastIndexOf('}')
|
||||
if (start >= 0 && end > start) {
|
||||
try {
|
||||
return JSON.parse(text.slice(start, end + 1))
|
||||
} catch {
|
||||
return payload
|
||||
}
|
||||
}
|
||||
return payload
|
||||
}
|
||||
}
|
||||
|
||||
// 响应拦截器
|
||||
const responseHandler = (response: { data: any }) => {
|
||||
if (!response?.data)
|
||||
if (!response?.data && response?.data !== 0 && response?.data !== false)
|
||||
return response
|
||||
|
||||
const { code, data, message } = response.data
|
||||
|
||||
if (!code)
|
||||
return response.data
|
||||
|
||||
// 拦截时间
|
||||
switch (code) {
|
||||
case 1000:
|
||||
if (Array.isArray(data)) {
|
||||
// transfter(data, timezone.info);
|
||||
} else if (data && data.list !== undefined && data.list?.length > 0) {
|
||||
// transfter(data.list, timezone.info);
|
||||
}
|
||||
return data
|
||||
default:
|
||||
return Promise.reject(new Error(message))
|
||||
// 兼容偶发双层:{ data: { code, data, message } }
|
||||
let payload = coerceJsonPayload(response.data)
|
||||
if (
|
||||
payload &&
|
||||
typeof payload === 'object' &&
|
||||
payload.code == null &&
|
||||
payload.data &&
|
||||
typeof payload.data === 'object' &&
|
||||
payload.data.code != null
|
||||
) {
|
||||
payload = payload.data
|
||||
}
|
||||
|
||||
return response.data
|
||||
if (!payload || typeof payload !== 'object')
|
||||
return payload
|
||||
|
||||
const { code, data, message } = payload
|
||||
|
||||
if (code == null || code === '')
|
||||
return payload
|
||||
|
||||
// 兼容 code 为字符串 "1000"
|
||||
const numericCode = Number(code)
|
||||
if (numericCode === 1000) {
|
||||
if (Array.isArray(data)) {
|
||||
// transfter(data, timezone.info);
|
||||
} else if (data && data.list !== undefined && data.list?.length > 0) {
|
||||
// transfter(data.list, timezone.info);
|
||||
}
|
||||
return data
|
||||
}
|
||||
|
||||
return Promise.reject(new Error(message || '请求失败'))
|
||||
}
|
||||
|
||||
// Add a response interceptor
|
||||
|
||||
Reference in new issue
Block a user