33
44import type { AxiosResponse } from 'axios' ;
55import type { CreateAxiosOptions , RequestOptions , Result } from './types' ;
6-
76import { VAxios } from './Axios' ;
87import { getToken } from '/@/utils/auth' ;
98import { AxiosTransform } from './axiosTransform' ;
@@ -16,11 +15,11 @@ import { useMessage } from '/@/hooks/web/useMessage';
1615import { RequestEnum , ResultEnum , ContentTypeEnum } from '/@/enums/httpEnum' ;
1716
1817import { isString } from '/@/utils/is' ;
19- import { formatRequestDate } from '/@/utils/dateUtil' ;
2018import { setObjToUrlParams , deepMerge } from '/@/utils' ;
2119import { errorStore } from '/@/store/modules/error' ;
2220import { errorResult } from './const' ;
2321import { useI18n } from '/@/hooks/web/useI18n' ;
22+ import { createNow , formatRequestDate } from './helper' ;
2423
2524const globSetting = useGlobSetting ( ) ;
2625const prefix = globSetting . urlPrefix ;
@@ -97,7 +96,7 @@ const transform: AxiosTransform = {
9796
9897 // 请求之前处理config
9998 beforeRequestHook : ( config , options ) => {
100- const { apiUrl, joinPrefix, joinParamsToUrl, formatDate } = options ;
99+ const { apiUrl, joinPrefix, joinParamsToUrl, formatDate, joinTime = true } = options ;
101100
102101 if ( joinPrefix ) {
103102 config . url = `${ prefix } ${ config . url } ` ;
@@ -107,17 +106,14 @@ const transform: AxiosTransform = {
107106 config . url = `${ apiUrl } ${ config . url } ` ;
108107 }
109108 if ( config . method ?. toUpperCase ( ) === RequestEnum . GET ) {
110- const now = new Date ( ) . getTime ( ) ;
111109 if ( ! isString ( config . params ) ) {
112110 config . data = {
113111 // 给 get 请求加上时间戳参数,避免从缓存中拿数据。
114- params : Object . assign ( config . params || { } , {
115- _t : now ,
116- } ) ,
112+ params : Object . assign ( config . params || { } , createNow ( joinTime , false ) ) ,
117113 } ;
118114 } else {
119115 // 兼容restful风格
120- config . url = config . url + config . params + `?_t= ${ now } ` ;
116+ config . url = config . url + config . params + `${ createNow ( joinTime , true ) } ` ;
121117 config . params = undefined ;
122118 }
123119 } else {
@@ -187,6 +183,8 @@ function createAxios(opt?: Partial<CreateAxiosOptions>) {
187183 // 接口可能会有通用的地址部分,可以统一抽取出来
188184 prefixUrl : prefix ,
189185 headers : { 'Content-Type' : ContentTypeEnum . JSON } ,
186+ // 如果是form-data格式
187+ // headers: { 'Content-Type': ContentTypeEnum.FORM_URLENCODED },
190188 // 数据处理方式
191189 transform,
192190 // 配置项,下面的选项都可以在独立的接口请求中覆盖
@@ -203,6 +201,8 @@ function createAxios(opt?: Partial<CreateAxiosOptions>) {
203201 errorMessageMode : 'message' ,
204202 // 接口地址
205203 apiUrl : globSetting . apiUrl ,
204+ // 是否加入时间戳
205+ joinTime : true ,
206206 } ,
207207 } ,
208208 opt || { }
0 commit comments