From f796d2f69cb00433ad5f8203aad54409d35a9ac6 Mon Sep 17 00:00:00 2001 From: bai <1643359946@qq.com> Date: Tue, 10 Oct 2023 10:56:18 +0800 Subject: [PATCH] =?UTF-8?q?2023=E5=B9=B410=E6=9C=8810=E6=97=A5=20=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D=E9=83=A8=E5=88=86=E4=B8=8D=E5=BA=94=E8=AF=A5=E8=B7=B3?= =?UTF-8?q?=E8=BD=AC=E7=9A=84=E5=86=85=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/http/axios/index.ts | 30 ++++++++++++++++++++++++++---- types/axios.d.ts | 1 + 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/utils/http/axios/index.ts b/src/utils/http/axios/index.ts index c993c78..e50a955 100644 --- a/src/utils/http/axios/index.ts +++ b/src/utils/http/axios/index.ts @@ -141,7 +141,7 @@ export const transform: AxiosTransform = { requestInterceptors: (config: Recordable, options) => { // 请求之前处理config const token = getToken(); - let tenantid = getTenantId(); + let tenantid:any = getTenantId(); if (token && (config as Recordable)?.requestOptions?.withToken !== false) { // jwt token config.headers.Authorization = options.authenticationScheme ? `${options.authenticationScheme} ${token}` : token; @@ -193,7 +193,12 @@ export const transform: AxiosTransform = { * @description: 响应错误处理 */ responseInterceptorsCatch: (error: any) => { - //console.log('报错了!',error); + //console.log('报错了!',error,error.config,error.request,error.response,error.isAxiosError,error.toJSON(),); + + if (!error?.response?.status) {//找不到返回值,也找不到状态码,则认为是重定向后跨域了, + window.location.href = error?.config?.url || window.location.href; + } + const { t } = useI18n(); const errorLogStore = useErrorLogStoreWithOut(); errorLogStore.addAjaxErrorInfo(error); @@ -216,7 +221,7 @@ export const transform: AxiosTransform = { errMessage = t('sys.api.networkExceptionMsg'); //errMessage = '请刷新'; //刷新页面 - window.location.href = config?.url || window.location.href; + //window.location.href = config?.url || window.location.href; } if (errMessage) { @@ -227,7 +232,7 @@ export const transform: AxiosTransform = { } return Promise.reject(error); } - } catch (error) { + } catch (error:any) { throw new Error(error); } @@ -236,10 +241,27 @@ export const transform: AxiosTransform = { }, }; +// .onerror = function handleError() { +// // Real errors are hidden from us by the browser +// // onerror should only fire if it's a network error +// reject(createError('Network Error', config, null, request)); + +// // Clean up request +// request = null; +// }; + function createAxios(opt?: Partial) { return new VAxios( deepMerge( { + // transformResponse: (res,conf) => { + + // console.log(`🚀 ---------------------------------------------------🚀`); + // console.log(`🚀 ~ file: index.ts:259 ~ createAxios ~ ...d:`, res,conf); + // console.log(`🚀 ---------------------------------------------------🚀`); + // return res + + // }, // See https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication#authentication_schemes // authentication schemes,e.g: Bearer // authenticationScheme: 'Bearer', diff --git a/types/axios.d.ts b/types/axios.d.ts index b1206d8..ba633a6 100644 --- a/types/axios.d.ts +++ b/types/axios.d.ts @@ -32,6 +32,7 @@ export interface Result { type: 'success' | 'error' | 'warning'; message: string; result: T; + success: T; } //文件上传参数