From 45d31a0f5625784423bea463b2373b0cd35b37f5 Mon Sep 17 00:00:00 2001 From: Soybean <2570172956@qq.com> Date: Wed, 13 Oct 2021 11:13:05 +0800 Subject: [PATCH] =?UTF-8?q?fix(types):=20=E4=BF=AE=E5=A4=8DTS=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/service/request/instance.ts | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/src/service/request/instance.ts b/src/service/request/instance.ts index 8f1a2a6c..95273f41 100644 --- a/src/service/request/instance.ts +++ b/src/service/request/instance.ts @@ -40,18 +40,20 @@ export default class CustomAxiosInstance { this.instance.interceptors.request.use( async config => { const handleConfig = { ...config }; - // form类型转换 - if (handleConfig.headers['Content-Type'] === ContentType.formUrlencoded) { - handleConfig.data = qs.stringify(handleConfig.data); + if (handleConfig.headers) { + // form类型转换 + if (handleConfig.headers['Content-Type'] === ContentType.formUrlencoded) { + handleConfig.data = qs.stringify(handleConfig.data); + } + // 文件类型转换 + if (handleConfig?.headers['Content-Type'] === ContentType.formData) { + const key = Object.keys(handleConfig.data)[0]; + const file = handleConfig.data[key]; + handleConfig.data = await transformFile(file, key); + } + // 设置token + handleConfig.headers.Authorization = getToken(); } - // 文件类型转换 - if (handleConfig.headers['Content-Type'] === ContentType.formData) { - const key = Object.keys(handleConfig.data)[0]; - const file = handleConfig.data[key]; - handleConfig.data = await transformFile(file, key); - } - // 设置token - handleConfig.headers.Authorization = getToken(); return handleConfig; }, error => { @@ -64,11 +66,12 @@ export default class CustomAxiosInstance { const { status, data } = response; const { statusKey, msgKey, successCode } = statusConfig; if (status === 200 || status < 300 || status === 304) { - if (data[statusKey] === successCode) { - return Promise.resolve(data.data); + const responseData = data as any; + if (responseData[statusKey] === successCode) { + return Promise.resolve(responseData.data); } - window.$message?.error(data[msgKey]); - return Promise.reject(data[msgKey]); + window.$message?.error(responseData[msgKey]); + return Promise.reject(responseData[msgKey]); } const error = { response }; errorHandler(error);