优化
This commit is contained in:
@@ -78,6 +78,25 @@ function rawUpload(path, filePath, name, token) {
|
||||
})
|
||||
}
|
||||
|
||||
function overseasUrl(path) {
|
||||
const value = String(path || '')
|
||||
const normalized = /\/(?:api|overseas-api)$/.test(session.apiBase)
|
||||
? value.replace(/^\/api(?=\/)/, '')
|
||||
: value
|
||||
return `${session.apiBase}${normalized}`
|
||||
}
|
||||
|
||||
function rawDownload(path, token) {
|
||||
return new Promise((resolve, reject) => {
|
||||
uni.downloadFile({
|
||||
url: overseasUrl(path),
|
||||
header: { Authorization: `Bearer ${token}` },
|
||||
success: resolve,
|
||||
fail: (cause) => reject(Object.assign(new Error(cause.errMsg || '签收照片下载失败'), { network: true }))
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
export async function overseasUpload(path, filePath, name = 'file', retry = true) {
|
||||
const attemptedToken = session.token
|
||||
let response = await rawUpload(path, filePath, name, attemptedToken)
|
||||
@@ -92,4 +111,17 @@ export async function overseasUpload(path, filePath, name = 'file', retry = true
|
||||
return body.data
|
||||
}
|
||||
|
||||
export async function overseasDownload(path, retry = true) {
|
||||
const attemptedToken = session.token
|
||||
let response = await rawDownload(path, attemptedToken)
|
||||
if (retry && unauthorized(response)) {
|
||||
const replacementToken = await tokenAfterUnauthorized(attemptedToken)
|
||||
response = await rawDownload(path, replacementToken)
|
||||
}
|
||||
if (response?.statusCode !== 200 || !response?.tempFilePath) {
|
||||
throw errorFrom({ ...response, data: {} }, '签收照片下载失败')
|
||||
}
|
||||
return response.tempFilePath
|
||||
}
|
||||
|
||||
export function resetOverseasRequestStateForTest() { refreshPromise = null }
|
||||
|
||||
Reference in New Issue
Block a user