From 77cd005f1560d62f031e53f7217a885f5208afa9 Mon Sep 17 00:00:00 2001 From: dap <15891557205@163.com> Date: Fri, 17 Jan 2025 10:48:52 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=B8=8B=E8=BD=BD=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E6=97=B6(responseType=20=3D=3D=3D=20'blob')=E9=9C=80=E8=A6=81?= =?UTF-8?q?=E5=88=A4=E6=96=AD=E4=B8=8B=E8=BD=BD=E5=A4=B1=E8=B4=A5(?= =?UTF-8?q?=E8=BF=94=E5=9B=9Ejson=E8=80=8C=E9=9D=9E=E4=BA=8C=E8=BF=9B?= =?UTF-8?q?=E5=88=B6)=E7=9A=84=E6=83=85=E5=86=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 1 + apps/web-antd/src/api/request.ts | 21 +++++++++++++++++++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c3b662b1..66c3cd18 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ - 字典项为空时getDict方法无限调用接口(无奈兼容 不给字典item本来就是错误用法) - 表格排序翻页会丢失排序参数 +- 下载文件时(responseType === 'blob')需要判断下载失败(返回json而非二进制)的情况 **OTHERS** diff --git a/apps/web-antd/src/api/request.ts b/apps/web-antd/src/api/request.ts index b3d80804..f0cfc74a 100644 --- a/apps/web-antd/src/api/request.ts +++ b/apps/web-antd/src/api/request.ts @@ -140,7 +140,7 @@ function createRequestClient(baseURL: string) { ); client.addResponseInterceptor({ - fulfilled: (response) => { + fulfilled: async (response) => { const encryptKey = (response.headers || {})['encrypt-key']; if (encryptKey) { /** RSA私钥解密 拿到解密秘钥的base64 */ @@ -164,7 +164,24 @@ function createRequestClient(baseURL: string) { // 不进行任何处理,直接返回 // 用于页面代码可能需要直接获取code,data,message这些信息时开启 if (!isTransformResponse) { - return response.data; + /** + * 需要判断下载二进制的情况 正常是返回二进制 报错会返回json + * 当type为blob且content-type为application/json时 则判断已经下载出错 + * 不能直接去判断blob的类型 因为下载json类型和报错的类型是一致的 需要从响应头判断 + */ + if ( + response.config.responseType === 'blob' && + response.headers['content-type']?.includes?.('application/json') + ) { + // 这时候的data为blob类型 + const blob = response.data as unknown as Blob; + // 拿到字符串转json对象 + response.data = JSON.parse(await blob.text()); + // 然后按正常逻辑执行下面的代码(判断业务状态码) + } else { + // 不为blob 直接返回 + return response.data; + } } const axiosResponseData = response.data;