!47 修复下载Excel文件时的异常处理问题

Merge pull request !47 from kuxiao/master
pull/57/head
xingyu 2024-09-13 09:21:46 +00:00 committed by Gitee
commit b716516b85
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
1 changed files with 14 additions and 10 deletions

View File

@ -126,14 +126,13 @@ export class VAxios {
const config = res.config
// 二进制数据则直接返回,例如说 Excel 导出
if (
res.request.responseType === 'blob' ||
res.request.responseType === 'arraybuffer'
res.request.responseType === 'blob'
|| res.request.responseType === 'arraybuffer'
) {
// 注意:如果导出的响应为 json说明可能失败了不直接返回进行下载
if (res.data.type === 'application/json') {
if (res.data.type === 'application/json')
res.data = await new Response(res.data).json()
}
}
// 处理 accessToken 过期的情况
if (res.data.code === 401) {
// 如果未认证,并且未进行刷新令牌,说明可能是访问令牌过期了
@ -298,12 +297,18 @@ export class VAxios {
reject(err || new Error('request error!'))
}
// 注释 return解决无法导出的问题。参见 https://t.zsxq.com/79cmo 说明。
// return
}
else {
resolve(res as unknown as Promise<T>)
}
// download file
if (typeof res != 'undefined')
if (res && typeof res !== 'undefined' && res.headers) {
const contentType = res.headers['content-type']
// ExcelUtils.java的write函数setContentType顺序问题导致没有contentType
if (!contentType || !contentType.startsWith('application/json'))
downloadByData(res?.data as unknown as BlobPart, title || 'export')
}
})
.catch((e: Error | AxiosError) => {
if (requestCatchHook && isFunction(requestCatchHook)) {
@ -344,7 +349,6 @@ export class VAxios {
this.axiosInstance
.request<any, AxiosResponse<Result>>(conf)
.then((res: AxiosResponse<Result>) => {
debugger
if (transformResponseHook && isFunction(transformResponseHook)) {
try {
const ret = transformResponseHook(res, opt)