Commit f646e37754d21ba7c89437176bd9e375924dee03

Authored by vben
1 parent c774a6d3

feat: axios add joinTime field

CHANGELOG.zh_CN.md
@@ -3,7 +3,8 @@ @@ -3,7 +3,8 @@
3 ### ✨ Features 3 ### ✨ Features
4 4
5 - 移除左侧菜单搜索,新增顶部菜单搜索功能 5 - 移除左侧菜单搜索,新增顶部菜单搜索功能
6 -- layout 移动端适配。页面未适配 6 +- layout 移动端适配。业务页面未适配
  7 +- axios 加入 joinTime 配置。控制响应是否加入时间戳
7 8
8 ### ⚡ Performance Improvements 9 ### ⚡ Performance Improvements
9 10
@@ -19,6 +20,10 @@ @@ -19,6 +20,10 @@
19 - 刷新按钮布局调整 20 - 刷新按钮布局调整
20 - `route.meta` 移除 `externalLink` 属性 21 - `route.meta` 移除 `externalLink` 属性
21 22
  23 +### ✨ Refactor
  24 +
  25 +- `openModal`与`openDrawer`第三个参数`openOnSet`默认设置为 true
  26 +
22 ### 🐛 Bug Fixes 27 ### 🐛 Bug Fixes
23 28
24 - 修复多级路由缓存导致组件渲染多次的问题 29 - 修复多级路由缓存导致组件渲染多次的问题
@@ -31,6 +36,7 @@ @@ -31,6 +36,7 @@
31 - 修复 `Modal`与`Drawer`组件在使用 emits 数据传递失效问题 36 - 修复 `Modal`与`Drawer`组件在使用 emits 数据传递失效问题
32 - 修复菜单已知问题 37 - 修复菜单已知问题
33 - 修复上传组件 api 失效问题 38 - 修复上传组件 api 失效问题
  39 +- 修复菜单权限过滤失效问题
34 40
35 ## 2.0.0-rc.13 (2020-12-10) 41 ## 2.0.0-rc.13 (2020-12-10)
36 42
src/components/Drawer/src/useDrawer.ts
@@ -56,7 +56,7 @@ export function useDrawer(): UseDrawerReturnType { @@ -56,7 +56,7 @@ export function useDrawer(): UseDrawerReturnType {
56 getInstance().setDrawerProps(props); 56 getInstance().setDrawerProps(props);
57 }, 57 },
58 58
59 - openDrawer: <T = any>(visible = true, data?: T, openOnSet = false): void => { 59 + openDrawer: <T = any>(visible = true, data?: T, openOnSet = true): void => {
60 getInstance().setDrawerProps({ 60 getInstance().setDrawerProps({
61 visible: visible, 61 visible: visible,
62 }); 62 });
src/components/Modal/src/useModal.ts
@@ -60,7 +60,7 @@ export function useModal(): UseModalReturnType { @@ -60,7 +60,7 @@ export function useModal(): UseModalReturnType {
60 getInstance().setModalProps(props); 60 getInstance().setModalProps(props);
61 }, 61 },
62 62
63 - openModal: <T = any>(visible = true, data?: T, openOnSet = false): void => { 63 + openModal: <T = any>(visible = true, data?: T, openOnSet = true): void => {
64 getInstance().setModalProps({ 64 getInstance().setModalProps({
65 visible: visible, 65 visible: visible,
66 }); 66 });
src/utils/dateUtil.ts
1 -import { isObject, isString } from '/@/utils/is';  
2 import moment from 'moment'; 1 import moment from 'moment';
3 2
4 const DATE_TIME_FORMAT = 'YYYY-MM-DD HH:mm'; 3 const DATE_TIME_FORMAT = 'YYYY-MM-DD HH:mm';
@@ -34,28 +33,5 @@ export const formatAgo = (str: string | number) =&gt; { @@ -34,28 +33,5 @@ export const formatAgo = (str: string | number) =&gt; {
34 return parseInt(String(time / 31536000000)) + '年前'; 33 return parseInt(String(time / 31536000000)) + '年前';
35 } 34 }
36 }; 35 };
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 -}  
60 36
61 export const dateUtil = moment; 37 export const dateUtil = moment;
src/utils/http/axios/helper.ts 0 → 100644
  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
@@ -3,7 +3,6 @@ @@ -3,7 +3,6 @@
3 3
4 import type { AxiosResponse } from 'axios'; 4 import type { AxiosResponse } from 'axios';
5 import type { CreateAxiosOptions, RequestOptions, Result } from './types'; 5 import type { CreateAxiosOptions, RequestOptions, Result } from './types';
6 -  
7 import { VAxios } from './Axios'; 6 import { VAxios } from './Axios';
8 import { getToken } from '/@/utils/auth'; 7 import { getToken } from '/@/utils/auth';
9 import { AxiosTransform } from './axiosTransform'; 8 import { AxiosTransform } from './axiosTransform';
@@ -16,11 +15,11 @@ import { useMessage } from &#39;/@/hooks/web/useMessage&#39;; @@ -16,11 +15,11 @@ import { useMessage } from &#39;/@/hooks/web/useMessage&#39;;
16 import { RequestEnum, ResultEnum, ContentTypeEnum } from '/@/enums/httpEnum'; 15 import { RequestEnum, ResultEnum, ContentTypeEnum } from '/@/enums/httpEnum';
17 16
18 import { isString } from '/@/utils/is'; 17 import { isString } from '/@/utils/is';
19 -import { formatRequestDate } from '/@/utils/dateUtil';  
20 import { setObjToUrlParams, deepMerge } from '/@/utils'; 18 import { setObjToUrlParams, deepMerge } from '/@/utils';
21 import { errorStore } from '/@/store/modules/error'; 19 import { errorStore } from '/@/store/modules/error';
22 import { errorResult } from './const'; 20 import { errorResult } from './const';
23 import { useI18n } from '/@/hooks/web/useI18n'; 21 import { useI18n } from '/@/hooks/web/useI18n';
  22 +import { createNow, formatRequestDate } from './helper';
24 23
25 const globSetting = useGlobSetting(); 24 const globSetting = useGlobSetting();
26 const prefix = globSetting.urlPrefix; 25 const prefix = globSetting.urlPrefix;
@@ -97,7 +96,7 @@ const transform: AxiosTransform = { @@ -97,7 +96,7 @@ const transform: AxiosTransform = {
97 96
98 // 请求之前处理config 97 // 请求之前处理config
99 beforeRequestHook: (config, options) => { 98 beforeRequestHook: (config, options) => {
100 - const { apiUrl, joinPrefix, joinParamsToUrl, formatDate } = options; 99 + const { apiUrl, joinPrefix, joinParamsToUrl, formatDate, joinTime = true } = options;
101 100
102 if (joinPrefix) { 101 if (joinPrefix) {
103 config.url = `${prefix}${config.url}`; 102 config.url = `${prefix}${config.url}`;
@@ -107,17 +106,14 @@ const transform: AxiosTransform = { @@ -107,17 +106,14 @@ const transform: AxiosTransform = {
107 config.url = `${apiUrl}${config.url}`; 106 config.url = `${apiUrl}${config.url}`;
108 } 107 }
109 if (config.method?.toUpperCase() === RequestEnum.GET) { 108 if (config.method?.toUpperCase() === RequestEnum.GET) {
110 - const now = new Date().getTime();  
111 if (!isString(config.params)) { 109 if (!isString(config.params)) {
112 config.data = { 110 config.data = {
113 // 给 get 请求加上时间戳参数,避免从缓存中拿数据。 111 // 给 get 请求加上时间戳参数,避免从缓存中拿数据。
114 - params: Object.assign(config.params || {}, {  
115 - _t: now,  
116 - }), 112 + params: Object.assign(config.params || {}, createNow(joinTime, false)),
117 }; 113 };
118 } else { 114 } else {
119 // 兼容restful风格 115 // 兼容restful风格
120 - config.url = config.url + config.params + `?_t=${now}`; 116 + config.url = config.url + config.params + `${createNow(joinTime, true)}`;
121 config.params = undefined; 117 config.params = undefined;
122 } 118 }
123 } else { 119 } else {
@@ -187,6 +183,8 @@ function createAxios(opt?: Partial&lt;CreateAxiosOptions&gt;) { @@ -187,6 +183,8 @@ function createAxios(opt?: Partial&lt;CreateAxiosOptions&gt;) {
187 // 接口可能会有通用的地址部分,可以统一抽取出来 183 // 接口可能会有通用的地址部分,可以统一抽取出来
188 prefixUrl: prefix, 184 prefixUrl: prefix,
189 headers: { 'Content-Type': ContentTypeEnum.JSON }, 185 headers: { 'Content-Type': ContentTypeEnum.JSON },
  186 + // 如果是form-data格式
  187 + // headers: { 'Content-Type': ContentTypeEnum.FORM_URLENCODED },
190 // 数据处理方式 188 // 数据处理方式
191 transform, 189 transform,
192 // 配置项,下面的选项都可以在独立的接口请求中覆盖 190 // 配置项,下面的选项都可以在独立的接口请求中覆盖
@@ -203,6 +201,8 @@ function createAxios(opt?: Partial&lt;CreateAxiosOptions&gt;) { @@ -203,6 +201,8 @@ function createAxios(opt?: Partial&lt;CreateAxiosOptions&gt;) {
203 errorMessageMode: 'message', 201 errorMessageMode: 'message',
204 // 接口地址 202 // 接口地址
205 apiUrl: globSetting.apiUrl, 203 apiUrl: globSetting.apiUrl,
  204 + // 是否加入时间戳
  205 + joinTime: true,
206 }, 206 },
207 }, 207 },
208 opt || {} 208 opt || {}
src/utils/http/axios/types.ts
@@ -16,6 +16,8 @@ export interface RequestOptions { @@ -16,6 +16,8 @@ export interface RequestOptions {
16 apiUrl?: string; 16 apiUrl?: string;
17 // 错误消息提示类型 17 // 错误消息提示类型
18 errorMessageMode?: ErrorMessageMode; 18 errorMessageMode?: ErrorMessageMode;
  19 + // 是否加入时间戳
  20 + joinTime?: boolean;
19 } 21 }
20 22
21 export interface CreateAxiosOptions extends AxiosRequestConfig { 23 export interface CreateAxiosOptions extends AxiosRequestConfig {
vite.config.ts
@@ -78,6 +78,7 @@ const viteConfig: UserConfig = { @@ -78,6 +78,7 @@ const viteConfig: UserConfig = {
78 // 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 78 // 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
79 optimizeDeps: { 79 optimizeDeps: {
80 include: [ 80 include: [
  81 + 'qs',
81 'echarts/map/js/china', 82 'echarts/map/js/china',
82 'ant-design-vue/es/locale/zh_CN', 83 'ant-design-vue/es/locale/zh_CN',
83 'ant-design-vue/es/locale/en_US', 84 'ant-design-vue/es/locale/en_US',