From 6941e95fc188c9f290e2c65cd8a9b59e749b8778 Mon Sep 17 00:00:00 2001 From: YunaiV Date: Sat, 27 Jul 2024 17:56:32 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E5=8A=9F=E8=83=BD=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E3=80=91=E5=85=A8=E5=B1=80=EF=BC=9AExcel=20=E7=AD=89=E5=AF=BC?= =?UTF-8?q?=E5=87=BA=E6=97=B6=EF=BC=8C=E5=A6=82=E6=9E=9C=E5=8F=91=E7=94=9F?= =?UTF-8?q?=E5=BC=82=E5=B8=B8=EF=BC=8C=E5=88=99=E4=B8=8D=E8=BF=9B=E8=A1=8C?= =?UTF-8?q?=E4=B8=8B=E8=BD=BD=20Excel=EF=BC=8C=E8=80=8C=E6=98=AF=E9=94=99?= =?UTF-8?q?=E8=AF=AF=E6=8F=90=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/http/axios/Axios.ts | 38 +++++++++++++++++++++++++++++++++-- src/utils/http/axios/index.ts | 11 ++++++---- 2 files changed, 43 insertions(+), 6 deletions(-) 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) }, }