Commit 49f39de7b40e3ec8343bdeaf3eb00fd79d395746

Authored by 无木
1 parent 8819af08

feat(axios): use `defHttp` like `axios`

当非GET请求并且同时存在data和params,不再忽略data。使defHttp的用法习惯接近axios原生配置

fixed: #850
Showing 1 changed file with 15 additions and 3 deletions
src/utils/http/axios/index.ts
@@ -61,6 +61,7 @@ const transform: AxiosTransform = { @@ -61,6 +61,7 @@ const transform: AxiosTransform = {
61 switch (code) { 61 switch (code) {
62 case ResultEnum.TIMEOUT: 62 case ResultEnum.TIMEOUT:
63 timeoutMsg = t('sys.api.timeoutMessage'); 63 timeoutMsg = t('sys.api.timeoutMessage');
  64 + break;
64 default: 65 default:
65 if (message) { 66 if (message) {
66 timeoutMsg = message; 67 timeoutMsg = message;
@@ -90,6 +91,8 @@ const transform: AxiosTransform = { @@ -90,6 +91,8 @@ const transform: AxiosTransform = {
90 config.url = `${apiUrl}${config.url}`; 91 config.url = `${apiUrl}${config.url}`;
91 } 92 }
92 const params = config.params || {}; 93 const params = config.params || {};
  94 + const data = config.data || false;
  95 + formatDate && data && !isString(data) && formatRequestDate(data);
93 if (config.method?.toUpperCase() === RequestEnum.GET) { 96 if (config.method?.toUpperCase() === RequestEnum.GET) {
94 if (!isString(params)) { 97 if (!isString(params)) {
95 // 给 get 请求加上时间戳参数,避免从缓存中拿数据。 98 // 给 get 请求加上时间戳参数,避免从缓存中拿数据。
@@ -102,10 +105,19 @@ const transform: AxiosTransform = { @@ -102,10 +105,19 @@ const transform: AxiosTransform = {
102 } else { 105 } else {
103 if (!isString(params)) { 106 if (!isString(params)) {
104 formatDate && formatRequestDate(params); 107 formatDate && formatRequestDate(params);
105 - config.data = params;  
106 - config.params = undefined; 108 + if (Reflect.has(config, 'data') && config.data && Object.keys(config.data).length > 0) {
  109 + config.data = data;
  110 + config.params = params;
  111 + } else {
  112 + // 非GET请求如果没有提供data,则将params视为data
  113 + config.data = params;
  114 + config.params = undefined;
  115 + }
107 if (joinParamsToUrl) { 116 if (joinParamsToUrl) {
108 - config.url = setObjToUrlParams(config.url as string, config.data); 117 + config.url = setObjToUrlParams(
  118 + config.url as string,
  119 + Object.assign({}, config.params, config.data)
  120 + );
109 } 121 }
110 } else { 122 } else {
111 // 兼容restful风格 123 // 兼容restful风格