From c119e910d038083dd7892e3ecfb339f6a04846dc Mon Sep 17 00:00:00 2001 From: preschooler Date: Mon, 18 Nov 2024 11:38:42 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9E=20fix:=E4=BF=AE=E5=A4=8D=E5=AF=B9?= =?UTF-8?q?=20axios=20=E7=9A=84=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/login/oauth2/index.ts | 2 +- src/config/axios/index.ts | 12 ++++-------- src/config/axios/service.ts | 30 +++--------------------------- 3 files changed, 8 insertions(+), 36 deletions(-) diff --git a/src/api/login/oauth2/index.ts b/src/api/login/oauth2/index.ts index aef1820d..f4a67fbe 100644 --- a/src/api/login/oauth2/index.ts +++ b/src/api/login/oauth2/index.ts @@ -27,7 +27,7 @@ export const authorize = ( return request.post({ url: '/system/oauth2/authorize', headers: { - 'Content-type': 'application/x-www-form-urlencoded' + 'Content-Type': 'application/x-www-form-urlencoded' }, params: { response_type: responseType, diff --git a/src/config/axios/index.ts b/src/config/axios/index.ts index 79e558da..07719a20 100644 --- a/src/config/axios/index.ts +++ b/src/config/axios/index.ts @@ -5,16 +5,12 @@ import { config } from './config' const { default_headers } = config const request = (option: any) => { - const { url, method, params, data, headersType, responseType, ...config } = option + const { headersType, headers, ...otherOption } = option return service({ - url: url, - method, - params, - data, - ...config, - responseType: responseType, + ...otherOption, headers: { - 'Content-Type': headersType || default_headers + 'Content-Type': headersType || default_headers, + ...headers } }) } diff --git a/src/config/axios/service.ts b/src/config/axios/service.ts index 3df813f2..f0eccc56 100644 --- a/src/config/axios/service.ts +++ b/src/config/axios/service.ts @@ -1,13 +1,6 @@ -import axios, { - AxiosError, - AxiosInstance, - AxiosRequestHeaders, - AxiosResponse, - InternalAxiosRequestConfig -} from 'axios' +import axios, { AxiosError, AxiosInstance, AxiosResponse, InternalAxiosRequestConfig } from 'axios' import { ElMessage, ElMessageBox, ElNotification } from 'element-plus' -import qs from 'qs' import { config } from '@/config/axios/config' import { getAccessToken, getRefreshToken, getTenantId, removeToken, setToken } from '@/utils/auth' import errorCode from './errorCode' @@ -52,29 +45,12 @@ service.interceptors.request.use( } }) if (getAccessToken() && !isToken) { - ;(config as Recordable).headers.Authorization = 'Bearer ' + getAccessToken() // 让每个请求携带自定义token + config.headers.Authorization = 'Bearer ' + getAccessToken() // 让每个请求携带自定义token } // 设置租户 if (tenantEnable && tenantEnable === 'true') { const tenantId = getTenantId() - if (tenantId) (config as Recordable).headers['tenant-id'] = tenantId - } - const params = config.params || {} - const data = config.data || false - if ( - config.method?.toUpperCase() === 'POST' && - (config.headers as AxiosRequestHeaders)['Content-Type'] === - 'application/x-www-form-urlencoded' - ) { - config.data = qs.stringify(data) - } - // get参数编码 - if (config.method?.toUpperCase() === 'GET' && params) { - config.params = {} - const paramsStr = qs.stringify(params, { allowDots: true }) - if (paramsStr) { - config.url = config.url + '?' + paramsStr - } + if (tenantId) config.headers['tenant-id'] = tenantId } return config },