zmdms-utils 0.0.27 → 0.0.29

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -11,7 +11,7 @@ function getBaseUrl(processObj) {
11
11
  // file 服务器
12
12
  var fileBaseUrlObj = {
13
13
  dev: processObj.REACT_APP_FILE_DEV || "http://192.168.0.134:89",
14
- test: processObj.REACT_APP_FILE_TEST || "http://172.55.5.101:33013",
14
+ test: processObj.REACT_APP_FILE_TEST || "http://file.cico.com.cn:30601",
15
15
  stage: processObj.REACT_APP_FILE_STAGE || "https://dzfile-prod.zmd.com.cn",
16
16
  product: processObj.REACT_APP_FILE_PRODUCT || "https://dzfile.zmd.com.cn:8012",
17
17
  };
@@ -1,5 +1,4 @@
1
- import * as axios from 'axios';
2
- import { AxiosRequestConfig } from 'axios';
1
+ import { AxiosResponse, AxiosRequestConfig } from 'axios';
3
2
  import { Crypto } from './crypto.js';
4
3
 
5
4
  declare class AxiosRequest {
@@ -18,9 +17,10 @@ declare class AxiosRequest {
18
17
  private defaultSetting;
19
18
  private interceptorsRequest;
20
19
  private interceptorsResponse;
20
+ private responseSuccess;
21
21
  private responseError;
22
22
  private logoutHandle;
23
- request(options: IOptions): Promise<axios.AxiosResponse<any, any>>;
23
+ request(options: IOptions): Promise<AxiosResponse<any, any>>;
24
24
  }
25
25
  interface IOptions extends AxiosRequestConfig {
26
26
  isFormData?: boolean;
@@ -36,6 +36,10 @@ interface IOptions extends AxiosRequestConfig {
36
36
  * 是否交给实例处理异常
37
37
  */
38
38
  isErrorMessage?: boolean;
39
+ /**
40
+ * 实例返回信息 简化处理
41
+ */
42
+ isEasyMessage?: boolean;
39
43
  }
40
44
  interface IOtherOptions {
41
45
  /** token过期等 需要跳转到登录页 */
@@ -59,11 +59,10 @@ var AxiosRequest = /** @class */ (function () {
59
59
  return;
60
60
  }
61
61
  axios.interceptors.response.use(function (response) {
62
- var _a;
63
62
  // console.log(response);
64
- // 对返回数据进行解密后 返回给客户端
65
- if ((_a = response.config) === null || _a === void 0 ? void 0 : _a.isCrypto) ;
66
- return response;
63
+ // 统一处理响应正常
64
+ var result = _this.responseSuccess(response);
65
+ return result;
67
66
  }, function (error) {
68
67
  console.log(error);
69
68
  // 统一处理响应异常
@@ -71,10 +70,49 @@ var AxiosRequest = /** @class */ (function () {
71
70
  return Promise.reject(error);
72
71
  });
73
72
  };
73
+ // 响应成功处理
74
+ AxiosRequest.prototype.responseSuccess = function (response) {
75
+ var _a;
76
+ var config = response.config, status = response.status, data = response.data;
77
+ // 对返回数据进行解密后 返回给客户端
78
+ if ((_a = response.config) === null || _a === void 0 ? void 0 : _a.isCrypto) ;
79
+ if (!config.isEasyMessage) {
80
+ return response;
81
+ }
82
+ // 简化处理信息
83
+ if (status === 200) {
84
+ // 字符串的情况也基本不会出现
85
+ if (typeof data === "string") {
86
+ return {
87
+ success: true,
88
+ msg: "操作成功",
89
+ data: data,
90
+ };
91
+ }
92
+ // 这里的data 不存在 可能说明这个接口没有返回信息
93
+ // 大多数情况下 不会这样 如果这种情况 也认为接口返回正常
94
+ if (!data) {
95
+ return {
96
+ success: true,
97
+ msg: "操作成功",
98
+ data: data,
99
+ };
100
+ }
101
+ // 大多数情况下 都是这种逻辑
102
+ if ((data === null || data === void 0 ? void 0 : data.code) === 200) {
103
+ return data;
104
+ }
105
+ }
106
+ // 201也可能出现,比如附件下载
107
+ if (status === 201) {
108
+ return response;
109
+ }
110
+ return Promise.reject(response.data);
111
+ };
74
112
  // 响应错误处理
75
113
  AxiosRequest.prototype.responseError = function (error) {
76
114
  var _this = this;
77
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u;
115
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r;
78
116
  if (error === void 0) { error = {}; }
79
117
  var response = error.response;
80
118
  // 处理token失效
@@ -131,7 +169,10 @@ var AxiosRequest = /** @class */ (function () {
131
169
  // 提示业务错误
132
170
  if ((_o = error === null || error === void 0 ? void 0 : error.config) === null || _o === void 0 ? void 0 : _o.isErrorMessage) {
133
171
  // 详细信息 再开发模式开启 之后上到别的环境会去掉
134
- (_p = this.modalConfirm) === null || _p === void 0 ? void 0 : _p.call(this, "".concat(((_r = (_q = error === null || error === void 0 ? void 0 : error.response) === null || _q === void 0 ? void 0 : _q.data) === null || _r === void 0 ? void 0 : _r.msg) || "服务器异常", "\n \u63A5\u53E3\u5730\u5740: ").concat((_s = error === null || error === void 0 ? void 0 : error.config) === null || _s === void 0 ? void 0 : _s.url, "\n \u54CD\u5E94\u72B6\u6001\u7801: ").concat((_t = error === null || error === void 0 ? void 0 : error.response) === null || _t === void 0 ? void 0 : _t.status, "\n \u8BF7\u6C42\u65B9\u5F0F: ").concat((_u = error === null || error === void 0 ? void 0 : error.config) === null || _u === void 0 ? void 0 : _u.method, "\n "));
172
+ (_p = this.modalConfirm) === null || _p === void 0 ? void 0 : _p.call(this, "".concat(((_r = (_q = error === null || error === void 0 ? void 0 : error.response) === null || _q === void 0 ? void 0 : _q.data) === null || _r === void 0 ? void 0 : _r.msg) || (error === null || error === void 0 ? void 0 : error.msg) || "服务器异常"));
173
+ // 接口地址: ${error?.config?.url}
174
+ // 响应状态码: ${error?.response?.status}
175
+ // 请求方式: ${error?.config?.method}
135
176
  return;
136
177
  }
137
178
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zmdms-utils",
3
- "version": "0.0.27",
3
+ "version": "0.0.29",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.js",
package/src/baseUrl.ts CHANGED
@@ -13,7 +13,7 @@ export function getBaseUrl(processObj: any) {
13
13
  // file 服务器
14
14
  const fileBaseUrlObj: any = {
15
15
  dev: processObj.REACT_APP_FILE_DEV || "http://192.168.0.134:89",
16
- test: processObj.REACT_APP_FILE_TEST || "http://172.55.5.101:33013",
16
+ test: processObj.REACT_APP_FILE_TEST || "http://file.cico.com.cn:30601",
17
17
  stage: processObj.REACT_APP_FILE_STAGE || "https://dzfile-prod.zmd.com.cn",
18
18
  product:
19
19
  processObj.REACT_APP_FILE_PRODUCT || "https://dzfile.zmd.com.cn:8012",
package/src/request.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  // 请求方法
2
- import axios, { AxiosRequestConfig } from "axios";
2
+ import axios, { AxiosRequestConfig, AxiosResponse } from "axios";
3
3
  import { getToken, removeToken } from "./auth";
4
4
  import { Crypto } from "./crypto";
5
5
 
@@ -88,10 +88,9 @@ export class AxiosRequest {
88
88
  axios.interceptors.response.use(
89
89
  (response) => {
90
90
  // console.log(response);
91
- // 对返回数据进行解密后 返回给客户端
92
- if ((response.config as any)?.isCrypto) {
93
- }
94
- return response;
91
+ // 统一处理响应正常
92
+ const result = this.responseSuccess(response);
93
+ return result;
95
94
  },
96
95
  (error) => {
97
96
  console.log(error);
@@ -102,6 +101,46 @@ export class AxiosRequest {
102
101
  );
103
102
  }
104
103
 
104
+ // 响应成功处理
105
+ private responseSuccess(response: AxiosResponse) {
106
+ const { config, status, data } = response;
107
+ // 对返回数据进行解密后 返回给客户端
108
+ if ((response.config as any)?.isCrypto) {
109
+ }
110
+ if (!(config as any).isEasyMessage) {
111
+ return response;
112
+ }
113
+ // 简化处理信息
114
+ if (status === 200) {
115
+ // 字符串的情况也基本不会出现
116
+ if (typeof data === "string") {
117
+ return {
118
+ success: true,
119
+ msg: "操作成功",
120
+ data,
121
+ };
122
+ }
123
+ // 这里的data 不存在 可能说明这个接口没有返回信息
124
+ // 大多数情况下 不会这样 如果这种情况 也认为接口返回正常
125
+ if (!data) {
126
+ return {
127
+ success: true,
128
+ msg: "操作成功",
129
+ data,
130
+ };
131
+ }
132
+ // 大多数情况下 都是这种逻辑
133
+ if (data?.code === 200) {
134
+ return data;
135
+ }
136
+ }
137
+ // 201也可能出现,比如附件下载
138
+ if (status === 201) {
139
+ return response;
140
+ }
141
+ return Promise.reject(response.data);
142
+ }
143
+
105
144
  // 响应错误处理
106
145
  private responseError(error: any = {}) {
107
146
  const { response } = error;
@@ -174,11 +213,12 @@ export class AxiosRequest {
174
213
  // 提示业务错误
175
214
  if (error?.config?.isErrorMessage) {
176
215
  // 详细信息 再开发模式开启 之后上到别的环境会去掉
177
- this.modalConfirm?.(`${error?.response?.data?.msg || "服务器异常"}
178
- 接口地址: ${error?.config?.url}
179
- 响应状态码: ${error?.response?.status}
180
- 请求方式: ${error?.config?.method}
181
- `);
216
+ this.modalConfirm?.(
217
+ `${error?.response?.data?.msg || error?.msg || "服务器异常"}`
218
+ );
219
+ // 接口地址: ${error?.config?.url}
220
+ // 响应状态码: ${error?.response?.status}
221
+ // 请求方式: ${error?.config?.method}
182
222
  return;
183
223
  }
184
224
  }
@@ -308,6 +348,10 @@ export interface IOptions extends AxiosRequestConfig {
308
348
  * 是否交给实例处理异常
309
349
  */
310
350
  isErrorMessage?: boolean;
351
+ /**
352
+ * 实例返回信息 简化处理
353
+ */
354
+ isEasyMessage?: boolean;
311
355
  }
312
356
 
313
357
  export interface IOtherOptions {