【功能修复】全局:Excel 等导出时,如果发生异常,则不进行下载 Excel,而是错误提示

pull/43/head
YunaiV 2024-07-27 17:56:32 +08:00
parent 6db4cbf5cf
commit 6941e95fc1
2 changed files with 43 additions and 6 deletions

View File

@ -124,6 +124,17 @@ export class VAxios {
// 响应结果拦截器处理
this.axiosInstance.interceptors.response.use(async (res: AxiosResponse<any>) => {
const config = res.config
// 二进制数据则直接返回,例如说 Excel 导出
if (
res.request.responseType === 'blob' ||
res.request.responseType === 'arraybuffer'
) {
// 注意:如果导出的响应为 json说明可能失败了不直接返回进行下载
if (res.data.type === 'application/json') {
res.data = await new Response(res.data).json()
}
}
// 处理 accessToken 过期的情况
if (res.data.code === 401) {
// 如果未认证,并且未进行刷新令牌,说明可能是访问令牌过期了
if (!isRefreshToken) {
@ -265,7 +276,7 @@ export class VAxios {
const opt: RequestOptions = Object.assign({}, requestOptions, options)
const { beforeRequestHook, requestCatchHook } = transform || {}
const { beforeRequestHook, requestCatchHook, transformResponseHook } = transform || {}
if (beforeRequestHook && isFunction(beforeRequestHook))
conf = beforeRequestHook(conf, opt)
@ -278,6 +289,17 @@ 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)
resolve(ret)
}
catch (err) {
reject(err || new Error('request error!'))
}
return
}
resolve(res as unknown as Promise<T>)
// download file
if (typeof res != 'undefined')
@ -296,6 +318,7 @@ export class VAxios {
})
}
// 和 download 类似,只是 method 是 'POST'
export<T = any>(config: AxiosRequestConfig, title: string, options?: RequestOptions): Promise<T> {
let conf: CreateAxiosOptions = cloneDeep({
...config,
@ -308,7 +331,7 @@ export class VAxios {
const opt: RequestOptions = Object.assign({}, requestOptions, options)
const { beforeRequestHook, requestCatchHook } = transform || {}
const { beforeRequestHook, requestCatchHook, transformResponseHook } = transform || {}
if (beforeRequestHook && isFunction(beforeRequestHook))
conf = beforeRequestHook(conf, opt)
@ -321,6 +344,17 @@ 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)
resolve(ret)
}
catch (err) {
reject(err || new Error('request error!'))
}
return
}
resolve(res as unknown as Promise<T>)
// download file
if (typeof res != 'undefined')

View File

@ -39,7 +39,8 @@ const transform: AxiosTransform = {
const { t } = useI18n()
const { isTransformResponse, isReturnNativeResponse } = options
// 二进制数据则直接返回
if (res.request.responseType === 'blob' || res.request.responseType === 'arraybuffer')
if ((res.request.responseType === 'blob' || res.request.responseType === 'arraybuffer')
&& res.data?.code === undefined) // 这个判断的目的是excel 导出等情况下,系统执行异常,此时返回的是 json而不是二进制数据
return res.data
// 是否返回原生响应头 比如:需要获取响应头时使用该属性
@ -256,11 +257,13 @@ const transform: AxiosTransform = {
checkStatus(error?.response?.status, msg, errorMessageMode)
// 添加自动重试机制 保险起见 只针对GET请求
const retryRequest = new AxiosRetry()
const { isOpenRetry } = config.requestOptions.retryRequest
config.method?.toUpperCase() === RequestEnum.GET
if (config?.requestOptions) {
const retryRequest = new AxiosRetry()
const {isOpenRetry} = config.requestOptions.retryRequest
config.method?.toUpperCase() === RequestEnum.GET
&& isOpenRetry
&& retryRequest.retry(axiosInstance, error)
}
return Promise.reject(error)
},
}