Skip to content

Commit 056684e

Browse files
YunaiVgitee-org
authored andcommitted
!588 【修复】恢复 axios 自定义参数序列化函数,并支持所有请求的 params 自定义序列化,修复刷新 token 后二次请求参数丢失问题。
Merge pull request !588 from 半栈幼儿员/hotfix/axios
2 parents ba2cdcc + 6cd6429 commit 056684e

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

src/config/axios/service.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import axios, { AxiosError, AxiosInstance, AxiosResponse, InternalAxiosRequestConfig } from 'axios'
22

33
import { ElMessage, ElMessageBox, ElNotification } from 'element-plus'
4+
import qs from 'qs'
45
import { config } from '@/config/axios/config'
56
import { getAccessToken, getRefreshToken, getTenantId, removeToken, setToken } from '@/utils/auth'
67
import errorCode from './errorCode'
@@ -30,7 +31,11 @@ const whiteList: string[] = ['/login', '/refresh-token']
3031
const service: AxiosInstance = axios.create({
3132
baseURL: base_url, // api 的 base_url
3233
timeout: request_timeout, // 请求超时时间
33-
withCredentials: false // 禁用 Cookie 等信息
34+
withCredentials: false, // 禁用 Cookie 等信息
35+
// 自定义参数序列化函数
36+
paramsSerializer: (params) => {
37+
return qs.stringify(params, { allowDots: true })
38+
}
3439
})
3540

3641
// request拦截器
@@ -52,6 +57,21 @@ service.interceptors.request.use(
5257
const tenantId = getTenantId()
5358
if (tenantId) config.headers['tenant-id'] = tenantId
5459
}
60+
const method = config.method?.toUpperCase()
61+
// 防止 GET 请求缓存
62+
if (method === 'GET') {
63+
config.headers['Cache-Control'] = 'no-cache'
64+
config.headers['Pragma'] = 'no-cache'
65+
}
66+
// 自定义参数序列化函数
67+
else if (method === 'POST') {
68+
const contentType = config.headers['Content-Type'] || config.headers['content-type']
69+
if (contentType === 'application/x-www-form-urlencoded') {
70+
if (config.data && typeof config.data !== 'string') {
71+
config.data = qs.stringify(config.data)
72+
}
73+
}
74+
}
5575
return config
5676
},
5777
(error: AxiosError) => {

0 commit comments

Comments
 (0)