From cb21627cf70b1725b2a4d5490b34a4c3c17f2225 Mon Sep 17 00:00:00 2001 From: zhangyi Date: Fri, 6 Dec 2024 09:41:09 +0800 Subject: [PATCH] =?UTF-8?q?=E9=80=9A=E7=94=A8download=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E6=96=B0=E5=A2=9Ejson=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/request.ts | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/src/utils/request.ts b/src/utils/request.ts index 9682b819..00f43659 100644 --- a/src/utils/request.ts +++ b/src/utils/request.ts @@ -174,18 +174,26 @@ service.interceptors.response.use( } ); // 通用下载方法 -export function download(url: string, params: any, fileName: string) { +export function download(url: string, params: any, fileName: string, isJson: boolean = false) { downloadLoadingInstance = ElLoading.service({ text: '正在下载数据,请稍候', background: 'rgba(0, 0, 0, 0.7)' }); - // prettier-ignore - return service.post(url, params, { - transformRequest: [ + + const config = { + headers: { + 'Content-Type': isJson ? 'application/json' : 'application/x-www-form-urlencoded' + }, + responseType: 'blob' as const, + transformRequest: isJson + ? undefined + : [ (params: any) => { return tansParams(params); } - ], - headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, - responseType: 'blob' - }).then(async (resp: any) => { + ] + }; + + // prettier-ignore + return service.post(url, isJson ? params : tansParams(params), config) + .then(async (resp: any) => { const isLogin = blobValidate(resp); if (isLogin) { const blob = new Blob([resp]); -- Gitee