1
1
import axios , { AxiosError , AxiosInstance , AxiosResponse , InternalAxiosRequestConfig } from 'axios'
2
2
3
3
import { ElMessage , ElMessageBox , ElNotification } from 'element-plus'
4
+ import qs from 'qs'
4
5
import { config } from '@/config/axios/config'
5
6
import { getAccessToken , getRefreshToken , getTenantId , removeToken , setToken } from '@/utils/auth'
6
7
import errorCode from './errorCode'
@@ -30,7 +31,11 @@ const whiteList: string[] = ['/login', '/refresh-token']
30
31
const service : AxiosInstance = axios . create ( {
31
32
baseURL : base_url , // api 的 base_url
32
33
timeout : request_timeout , // 请求超时时间
33
- withCredentials : false // 禁用 Cookie 等信息
34
+ withCredentials : false , // 禁用 Cookie 等信息
35
+ // 自定义参数序列化函数
36
+ paramsSerializer : ( params ) => {
37
+ return qs . stringify ( params , { allowDots : true } )
38
+ }
34
39
} )
35
40
36
41
// request拦截器
@@ -52,6 +57,21 @@ service.interceptors.request.use(
52
57
const tenantId = getTenantId ( )
53
58
if ( tenantId ) config . headers [ 'tenant-id' ] = tenantId
54
59
}
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
+ }
55
75
return config
56
76
} ,
57
77
( error : AxiosError ) => {
0 commit comments