From 6cd64291029114acd9e8fc27ec8bd7243edf82ab Mon Sep 17 00:00:00 2001 From: preschooler Date: Tue, 19 Nov 2024 19:24:49 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9E=20fix:=E6=81=A2=E5=A4=8D=20axios?= =?UTF-8?q?=20=E8=87=AA=E5=AE=9A=E4=B9=89=E5=8F=82=E6=95=B0=E5=BA=8F?= =?UTF-8?q?=E5=88=97=E5=8C=96=E5=87=BD=E6=95=B0=EF=BC=8C=E5=B9=B6=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E6=89=80=E6=9C=89=E8=AF=B7=E6=B1=82=E7=9A=84=20params?= =?UTF-8?q?=20=E8=87=AA=E5=AE=9A=E4=B9=89=E5=BA=8F=E5=88=97=E5=8C=96?= =?UTF-8?q?=EF=BC=8C=E4=BF=AE=E5=A4=8D=E5=88=B7=E6=96=B0=20token=20?= =?UTF-8?q?=E5=90=8E=E4=BA=8C=E6=AC=A1=E8=AF=B7=E6=B1=82=E5=8F=82=E6=95=B0?= =?UTF-8?q?=E4=B8=A2=E5=A4=B1=E9=97=AE=E9=A2=98=EF=BC=8C=E4=BC=98=E5=8C=96?= =?UTF-8?q?=20GET=20=E8=AF=B7=E6=B1=82=E7=BC=93=E5=AD=98=E9=97=AE=E9=A2=98?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/config/axios/service.ts | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/config/axios/service.ts b/src/config/axios/service.ts index f0eccc56..ae3845c0 100644 --- a/src/config/axios/service.ts +++ b/src/config/axios/service.ts @@ -1,6 +1,7 @@ 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' @@ -30,7 +31,11 @@ const whiteList: string[] = ['/login', '/refresh-token'] const service: AxiosInstance = axios.create({ baseURL: base_url, // api 的 base_url timeout: request_timeout, // 请求超时时间 - withCredentials: false // 禁用 Cookie 等信息 + withCredentials: false, // 禁用 Cookie 等信息 + // 自定义参数序列化函数 + paramsSerializer: (params) => { + return qs.stringify(params, { allowDots: true }) + } }) // request拦截器 @@ -52,6 +57,21 @@ service.interceptors.request.use( const tenantId = getTenantId() if (tenantId) config.headers['tenant-id'] = tenantId } + const method = config.method?.toUpperCase() + // 防止 GET 请求缓存 + if (method === 'GET') { + config.headers['Cache-Control'] = 'no-cache' + config.headers['Pragma'] = 'no-cache' + } + // 自定义参数序列化函数 + else if (method === 'POST') { + const contentType = config.headers['Content-Type'] || config.headers['content-type'] + if (contentType === 'application/x-www-form-urlencoded') { + if (config.data && typeof config.data !== 'string') { + config.data = qs.stringify(config.data) + } + } + } return config }, (error: AxiosError) => {