Skip to content

Commit f646e37

Browse files
committed
feat: axios add joinTime field
1 parent c774a6d commit f646e37

File tree

8 files changed

+65
-35
lines changed

8 files changed

+65
-35
lines changed

CHANGELOG.zh_CN.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
### ✨ Features
44

55
- 移除左侧菜单搜索,新增顶部菜单搜索功能
6-
- layout 移动端适配。页面未适配
6+
- layout 移动端适配。业务页面未适配
7+
- axios 加入 joinTime 配置。控制响应是否加入时间戳
78

89
### ⚡ Performance Improvements
910

@@ -19,6 +20,10 @@
1920
- 刷新按钮布局调整
2021
- `route.meta` 移除 `externalLink` 属性
2122

23+
### ✨ Refactor
24+
25+
- `openModal``openDrawer`第三个参数`openOnSet`默认设置为 true
26+
2227
### 🐛 Bug Fixes
2328

2429
- 修复多级路由缓存导致组件渲染多次的问题
@@ -31,6 +36,7 @@
3136
- 修复 `Modal``Drawer`组件在使用 emits 数据传递失效问题
3237
- 修复菜单已知问题
3338
- 修复上传组件 api 失效问题
39+
- 修复菜单权限过滤失效问题
3440

3541
## 2.0.0-rc.13 (2020-12-10)
3642

src/components/Drawer/src/useDrawer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export function useDrawer(): UseDrawerReturnType {
5656
getInstance().setDrawerProps(props);
5757
},
5858

59-
openDrawer: <T = any>(visible = true, data?: T, openOnSet = false): void => {
59+
openDrawer: <T = any>(visible = true, data?: T, openOnSet = true): void => {
6060
getInstance().setDrawerProps({
6161
visible: visible,
6262
});

src/components/Modal/src/useModal.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export function useModal(): UseModalReturnType {
6060
getInstance().setModalProps(props);
6161
},
6262

63-
openModal: <T = any>(visible = true, data?: T, openOnSet = false): void => {
63+
openModal: <T = any>(visible = true, data?: T, openOnSet = true): void => {
6464
getInstance().setModalProps({
6565
visible: visible,
6666
});

src/utils/dateUtil.ts

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { isObject, isString } from '/@/utils/is';
21
import moment from 'moment';
32

43
const DATE_TIME_FORMAT = 'YYYY-MM-DD HH:mm';
@@ -34,28 +33,5 @@ export const formatAgo = (str: string | number) => {
3433
return parseInt(String(time / 31536000000)) + '年前';
3534
}
3635
};
37-
/**
38-
* @description: 格式化请求参数时间
39-
*/
40-
export function formatRequestDate(params: any) {
41-
for (const key in params) {
42-
if (params[key] && params[key]._isAMomentObject) {
43-
params[key] = params[key].format(DATE_TIME_FORMAT);
44-
}
45-
if (isString(key)) {
46-
const value = params[key];
47-
if (value) {
48-
try {
49-
params[key] = isString(value) ? value.trim() : value;
50-
} catch (error) {
51-
throw new Error(error);
52-
}
53-
}
54-
}
55-
if (isObject(params[key])) {
56-
formatRequestDate(params[key]);
57-
}
58-
}
59-
}
6036

6137
export const dateUtil = moment;

src/utils/http/axios/helper.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { isObject, isString } from '/@/utils/is';
2+
3+
export function createNow<T extends boolean>(
4+
join: boolean,
5+
restful: T
6+
): T extends true ? string : object;
7+
8+
export function createNow(join: boolean, restful = false): string | object {
9+
if (!join) {
10+
return restful ? '' : {};
11+
}
12+
const now = new Date().getTime();
13+
if (restful) {
14+
return `?_t=${now}`;
15+
}
16+
17+
return {
18+
_t: now,
19+
};
20+
}
21+
22+
const DATE_TIME_FORMAT = 'YYYY-MM-DD HH:mm';
23+
/**
24+
* @description: 格式化请求参数时间
25+
*/
26+
export function formatRequestDate(params: any) {
27+
for (const key in params) {
28+
if (params[key] && params[key]._isAMomentObject) {
29+
params[key] = params[key].format(DATE_TIME_FORMAT);
30+
}
31+
if (isString(key)) {
32+
const value = params[key];
33+
if (value) {
34+
try {
35+
params[key] = isString(value) ? value.trim() : value;
36+
} catch (error) {
37+
throw new Error(error);
38+
}
39+
}
40+
}
41+
if (isObject(params[key])) {
42+
formatRequestDate(params[key]);
43+
}
44+
}
45+
}

src/utils/http/axios/index.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
import type { AxiosResponse } from 'axios';
55
import type { CreateAxiosOptions, RequestOptions, Result } from './types';
6-
76
import { VAxios } from './Axios';
87
import { getToken } from '/@/utils/auth';
98
import { AxiosTransform } from './axiosTransform';
@@ -16,11 +15,11 @@ import { useMessage } from '/@/hooks/web/useMessage';
1615
import { RequestEnum, ResultEnum, ContentTypeEnum } from '/@/enums/httpEnum';
1716

1817
import { isString } from '/@/utils/is';
19-
import { formatRequestDate } from '/@/utils/dateUtil';
2018
import { setObjToUrlParams, deepMerge } from '/@/utils';
2119
import { errorStore } from '/@/store/modules/error';
2220
import { errorResult } from './const';
2321
import { useI18n } from '/@/hooks/web/useI18n';
22+
import { createNow, formatRequestDate } from './helper';
2423

2524
const globSetting = useGlobSetting();
2625
const 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 || {}

src/utils/http/axios/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ export interface RequestOptions {
1616
apiUrl?: string;
1717
// 错误消息提示类型
1818
errorMessageMode?: ErrorMessageMode;
19+
// 是否加入时间戳
20+
joinTime?: boolean;
1921
}
2022

2123
export interface CreateAxiosOptions extends AxiosRequestConfig {

vite.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ const viteConfig: UserConfig = {
7878
// The package will be recompiled using rollup, and the new package compiled into the esm module specification will be put into node_modules/.vite_opt_cache
7979
optimizeDeps: {
8080
include: [
81+
'qs',
8182
'echarts/map/js/china',
8283
'ant-design-vue/es/locale/zh_CN',
8384
'ant-design-vue/es/locale/en_US',

0 commit comments

Comments
 (0)