!588 【修复】恢复 axios 自定义参数序列化函数,并支持所有请求的 params 自定义序列化,修复刷新 token 后二次请求参数丢失问题。

Merge pull request !588 from 半栈幼儿员/hotfix/axios
pull/590/head
芋道源码 2024-11-20 01:38:29 +00:00 committed by Gitee
commit 056684e291
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
1 changed files with 21 additions and 1 deletions

View File

@ -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) => {