修复小程序令牌过期登录循环
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
const { API_DOMAIN, REQUEST_TIMEOUT } = require('./config')
|
||||
const request = require('./request')
|
||||
|
||||
function normalizeUploadResponse(response, fallbackMessage) {
|
||||
function normalizeUploadResponse(response, fallbackMessage, url) {
|
||||
if (response.statusCode === 401) {
|
||||
throw new Error('登录已过期,请重新登录')
|
||||
throw request.handleUnauthorized({ data: response.data }, url)
|
||||
}
|
||||
if (response.statusCode >= 400) {
|
||||
const message = response.data && response.data.message ? response.data.message : fallbackMessage
|
||||
@@ -59,8 +59,16 @@ function uploadFile(options) {
|
||||
header: signatureHeaders,
|
||||
success(response) {
|
||||
try {
|
||||
const data = response.data ? preserveResponseId(response.data, JSON.parse(response.data)) : {}
|
||||
resolve(normalizeUploadResponse({ ...response, data }, options.errorMessage || '上传失败'))
|
||||
let parsedData = {}
|
||||
if (response.data) {
|
||||
try {
|
||||
parsedData = JSON.parse(response.data)
|
||||
} catch (error) {
|
||||
if (response.statusCode !== 401) throw error
|
||||
}
|
||||
}
|
||||
const data = preserveResponseId(response.data, parsedData)
|
||||
resolve(normalizeUploadResponse({ ...response, data }, options.errorMessage || '上传失败', options.url))
|
||||
} catch (error) {
|
||||
reject(error)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user