diff --git a/src/utils/http/axios/Axios.ts b/src/utils/http/axios/Axios.ts index abdafbcc..693adcc5 100644 --- a/src/utils/http/axios/Axios.ts +++ b/src/utils/http/axios/Axios.ts @@ -124,6 +124,17 @@ export class VAxios { // 响应结果拦截器处理 this.axiosInstance.interceptors.response.use(async (res: AxiosResponse) => { 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>(conf) .then((res: AxiosResponse) => { + 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) // download file if (typeof res != 'undefined') @@ -296,6 +318,7 @@ export class VAxios { }) } + // 和 download 类似,只是 method 是 'POST' export(config: AxiosRequestConfig, title: string, options?: RequestOptions): Promise { 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>(conf) .then((res: AxiosResponse) => { + 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) // download file if (typeof res != 'undefined') diff --git a/src/utils/http/axios/index.ts b/src/utils/http/axios/index.ts index fd2ffeda..24f57d66 100644 --- a/src/utils/http/axios/index.ts +++ b/src/utils/http/axios/index.ts @@ -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) }, }