From ccb997ac2a643db597e9496c8c6c9f9c8079fb7b Mon Sep 17 00:00:00 2001 From: YunaiV Date: Sat, 30 Dec 2023 19:11:29 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20=E4=BF=AE=E5=A4=8D=20Excel=20?= =?UTF-8?q?=E5=AF=BC=E5=87=BA=E5=A4=B1=E8=B4=A5=E6=97=B6=EF=BC=8C=E4=BE=9D?= =?UTF-8?q?=E7=84=B6=E4=B8=8B=E8=BD=BD=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/config/axios/service.ts | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/config/axios/service.ts b/src/config/axios/service.ts index 6413e945..c9f26ad1 100644 --- a/src/config/axios/service.ts +++ b/src/config/axios/service.ts @@ -104,7 +104,7 @@ service.interceptors.request.use( // response 拦截器 service.interceptors.response.use( async (response: AxiosResponse) => { - const { data } = response + let { data } = response const config = response.config if (!data) { // 返回“[HTTP]请求没有返回值”; @@ -112,14 +112,18 @@ service.interceptors.response.use( } const { t } = useI18n() // 未设置状态码则默认成功状态 - const code = data.code || result_code - // 二进制数据则直接返回 + // 二进制数据则直接返回,例如说 Excel 导出 if ( response.request.responseType === 'blob' || response.request.responseType === 'arraybuffer' ) { - return response.data + // 注意:如果导出的响应为 json,说明可能失败了,不直接返回进行下载 + if (response.data.type !== 'application/json') { + return response.data + } + data = await new Response(response.data).json() } + const code = data.code || result_code // 获取错误信息 const msg = data.msg || errorCode[code] || errorCode['default'] if (ignoreMsgs.indexOf(msg) !== -1) {