zmdms-utils 0.0.57 → 0.0.58

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.
@@ -13,6 +13,7 @@ declare class AxiosRequest {
13
13
  private isTimeoutConfirmStr?;
14
14
  private messageWarning?;
15
15
  private modalConfirm?;
16
+ private instanceAxios;
16
17
  constructor(otherOptions: IOtherOptions);
17
18
  private defaultSetting;
18
19
  private interceptorsRequest;
@@ -44,6 +45,10 @@ interface IOptions extends AxiosRequestConfig {
44
45
  * 登录过期是否交给自己处理
45
46
  */
46
47
  isHandleLoginExpiration?: boolean;
48
+ /**
49
+ * 登录过期跳转逻辑交给实例自己处理
50
+ */
51
+ expirationHandle?: (options: any) => void;
47
52
  }
48
53
  interface IOtherOptions {
49
54
  /** token过期等 需要跳转到登录页 */
@@ -18,6 +18,8 @@ var AxiosRequest = /** @class */ (function () {
18
18
  this.isNotFound = false;
19
19
  // timeout
20
20
  this.isTimeout = false;
21
+ // axios单例
22
+ this.instanceAxios = axios.create();
21
23
  var crypto = otherOptions.crypto, jumpCallback = otherOptions.jumpCallback, isTimeoutConfirm = otherOptions.isTimeoutConfirm, isTimeoutConfirmStr = otherOptions.isTimeoutConfirmStr, messageWarning = otherOptions.messageWarning, modalConfirm = otherOptions.modalConfirm, resetOtherOptions = __rest(otherOptions, ["crypto", "jumpCallback", "isTimeoutConfirm", "isTimeoutConfirmStr", "messageWarning", "modalConfirm"]);
22
24
  this.otherOptions = __assign(__assign({}, defaultOptions), resetOtherOptions);
23
25
  this.crypto = crypto;
@@ -33,12 +35,13 @@ var AxiosRequest = /** @class */ (function () {
33
35
  // 设置axios方法默认值
34
36
  AxiosRequest.prototype.defaultSetting = function () {
35
37
  // 设置默认请求头
36
- axios.defaults.headers.common["Tenant-Id"] = this.otherOptions.TenantId;
37
- axios.defaults.headers.common["Authorization"] =
38
+ this.instanceAxios.defaults.headers.common["Tenant-Id"] =
39
+ this.otherOptions.TenantId;
40
+ this.instanceAxios.defaults.headers.common["Authorization"] =
38
41
  this.otherOptions.Authorization;
39
42
  // 其他默认参数
40
- axios.defaults.timeout = this.otherOptions.timeout;
41
- axios.defaults.baseURL = this.otherOptions.baseURL;
43
+ this.instanceAxios.defaults.timeout = this.otherOptions.timeout;
44
+ this.instanceAxios.defaults.baseURL = this.otherOptions.baseURL;
42
45
  };
43
46
  // 拦截请求
44
47
  AxiosRequest.prototype.interceptorsRequest = function () {
@@ -47,7 +50,7 @@ var AxiosRequest = /** @class */ (function () {
47
50
  this.otherOptions.interceptorsRequestHandle();
48
51
  return;
49
52
  }
50
- axios.interceptors.request.use(function (config) {
53
+ this.instanceAxios.interceptors.request.use(function (config) {
51
54
  if (_this.otherOptions.interceptorsRequestConfig) {
52
55
  return _this.otherOptions.interceptorsRequestConfig(config);
53
56
  }
@@ -63,7 +66,7 @@ var AxiosRequest = /** @class */ (function () {
63
66
  this.otherOptions.interceptorsResponseHandle();
64
67
  return;
65
68
  }
66
- axios.interceptors.response.use(function (response) {
69
+ this.instanceAxios.interceptors.response.use(function (response) {
67
70
  // console.log(response);
68
71
  // 统一处理响应正常
69
72
  var result = _this.responseSuccess(response);
@@ -117,21 +120,27 @@ var AxiosRequest = /** @class */ (function () {
117
120
  // 响应错误处理
118
121
  AxiosRequest.prototype.responseError = function (error) {
119
122
  var _this = this;
120
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s;
123
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t;
121
124
  if (error === void 0) { error = {}; }
122
125
  var response = error.response;
123
126
  // 登录过期是否自己处理
124
127
  var isHandleLoginExpiration = (_a = error === null || error === void 0 ? void 0 : error.config) === null || _a === void 0 ? void 0 : _a.isHandleLoginExpiration;
128
+ var expirationHandle = (_b = error === null || error === void 0 ? void 0 : error.config) === null || _b === void 0 ? void 0 : _b.expirationHandle;
125
129
  // 处理token失效
126
130
  // TODO: 刷新token逻辑
127
131
  if ((response === null || response === void 0 ? void 0 : response.status) === 401 && !isHandleLoginExpiration) {
128
132
  removeToken();
129
133
  if (!this.isExpired) {
130
134
  this.isExpired = true;
131
- (_b = this.messageWarning) === null || _b === void 0 ? void 0 : _b.call(this, "登录过期", 3.5, function () {
135
+ (_c = this.messageWarning) === null || _c === void 0 ? void 0 : _c.call(this, "登录过期", 3.5, function () {
132
136
  _this.isExpired = false;
133
137
  });
134
- this.logoutHandle(response.status);
138
+ if (expirationHandle) {
139
+ expirationHandle(response);
140
+ }
141
+ else {
142
+ this.logoutHandle(response.status);
143
+ }
135
144
  }
136
145
  return;
137
146
  }
@@ -140,10 +149,15 @@ var AxiosRequest = /** @class */ (function () {
140
149
  removeToken();
141
150
  if (!this.isOffline) {
142
151
  this.isOffline = true;
143
- (_c = this.messageWarning) === null || _c === void 0 ? void 0 : _c.call(this, "你的账号已在其他地方登录!", 3.5, function () {
152
+ (_d = this.messageWarning) === null || _d === void 0 ? void 0 : _d.call(this, "你的账号已在其他地方登录!", 3.5, function () {
144
153
  _this.isOffline = false;
145
154
  });
146
- this.logoutHandle(response.status);
155
+ if (expirationHandle) {
156
+ expirationHandle(response);
157
+ }
158
+ else {
159
+ this.logoutHandle(response.status);
160
+ }
147
161
  }
148
162
  return;
149
163
  }
@@ -151,7 +165,7 @@ var AxiosRequest = /** @class */ (function () {
151
165
  if ((response === null || response === void 0 ? void 0 : response.status) === 404) {
152
166
  if (!this.isNotFound) {
153
167
  this.isNotFound = true;
154
- (_d = this.messageWarning) === null || _d === void 0 ? void 0 : _d.call(this, "\u63A5\u53E3\uFF1A".concat(response.config.url, " \u672A\u627E\u5230\uFF01"), 3.5, function () {
168
+ (_e = this.messageWarning) === null || _e === void 0 ? void 0 : _e.call(this, "\u63A5\u53E3\uFF1A".concat(response.config.url, " \u672A\u627E\u5230\uFF01"), 3.5, function () {
155
169
  _this.isNotFound = false;
156
170
  });
157
171
  }
@@ -159,24 +173,24 @@ var AxiosRequest = /** @class */ (function () {
159
173
  }
160
174
  // 处理接口超时逻辑
161
175
  if (this.isTimeoutConfirm &&
162
- ((_g = (_f = (_e = error === null || error === void 0 ? void 0 : error.config) === null || _e === void 0 ? void 0 : _e.method) === null || _f === void 0 ? void 0 : _f.toUpperCase) === null || _g === void 0 ? void 0 : _g.call(_f)) === "POST" &&
176
+ ((_h = (_g = (_f = error === null || error === void 0 ? void 0 : error.config) === null || _f === void 0 ? void 0 : _f.method) === null || _g === void 0 ? void 0 : _g.toUpperCase) === null || _h === void 0 ? void 0 : _h.call(_g)) === "POST" &&
163
177
  !response &&
164
178
  error.code === "ECONNABORTED" &&
165
- ((_j = (_h = error.message) === null || _h === void 0 ? void 0 : _h.indexOf) === null || _j === void 0 ? void 0 : _j.call(_h, "".concat(error.config.timeout, "ms"))) !== -1) {
179
+ ((_k = (_j = error.message) === null || _j === void 0 ? void 0 : _j.indexOf) === null || _k === void 0 ? void 0 : _k.call(_j, "".concat(error.config.timeout, "ms"))) !== -1) {
166
180
  if (!this.isTimeout) {
167
181
  this.isTimeout = true;
168
- (_k = this.modalConfirm) === null || _k === void 0 ? void 0 : _k.call(this, "".concat(this.isTimeoutConfirmStr
182
+ (_l = this.modalConfirm) === null || _l === void 0 ? void 0 : _l.call(this, "".concat(this.isTimeoutConfirmStr
169
183
  ? this.isTimeoutConfirmStr
170
- : "请求超时,请稍后再试,或联系相关人员确认后处理!", "\n \u63A5\u53E3\u5730\u5740: ").concat((_l = error === null || error === void 0 ? void 0 : error.config) === null || _l === void 0 ? void 0 : _l.url, "\n \u8BF7\u6C42\u65B9\u5F0F: ").concat((_m = error === null || error === void 0 ? void 0 : error.config) === null || _m === void 0 ? void 0 : _m.method, "\n \u8BF7\u6C42\u8D85\u65F6\u65F6\u95F4: ").concat(((_o = error === null || error === void 0 ? void 0 : error.config) === null || _o === void 0 ? void 0 : _o.timeout) / 1000, "\u79D2\n "), function () {
184
+ : "请求超时,请稍后再试,或联系相关人员确认后处理!", "\n \u63A5\u53E3\u5730\u5740: ").concat((_m = error === null || error === void 0 ? void 0 : error.config) === null || _m === void 0 ? void 0 : _m.url, "\n \u8BF7\u6C42\u65B9\u5F0F: ").concat((_o = error === null || error === void 0 ? void 0 : error.config) === null || _o === void 0 ? void 0 : _o.method, "\n \u8BF7\u6C42\u8D85\u65F6\u65F6\u95F4: ").concat(((_p = error === null || error === void 0 ? void 0 : error.config) === null || _p === void 0 ? void 0 : _p.timeout) / 1000, "\u79D2\n "), function () {
171
185
  _this.isTimeout = false;
172
186
  });
173
187
  }
174
188
  return;
175
189
  }
176
190
  // 提示业务错误
177
- if ((_p = error === null || error === void 0 ? void 0 : error.config) === null || _p === void 0 ? void 0 : _p.isErrorMessage) {
191
+ if ((_q = error === null || error === void 0 ? void 0 : error.config) === null || _q === void 0 ? void 0 : _q.isErrorMessage) {
178
192
  // 详细信息 再开发模式开启 之后上到别的环境会去掉
179
- (_q = this.modalConfirm) === null || _q === void 0 ? void 0 : _q.call(this, "".concat(((_s = (_r = error === null || error === void 0 ? void 0 : error.response) === null || _r === void 0 ? void 0 : _r.data) === null || _s === void 0 ? void 0 : _s.msg) || (error === null || error === void 0 ? void 0 : error.msg) || "服务器异常"));
193
+ (_r = this.modalConfirm) === null || _r === void 0 ? void 0 : _r.call(this, "".concat(((_t = (_s = error === null || error === void 0 ? void 0 : error.response) === null || _s === void 0 ? void 0 : _s.data) === null || _t === void 0 ? void 0 : _t.msg) || (error === null || error === void 0 ? void 0 : error.msg) || "服务器异常"));
180
194
  // 接口地址: ${error?.config?.url}
181
195
  // 响应状态码: ${error?.response?.status}
182
196
  // 请求方式: ${error?.config?.method}
@@ -253,7 +267,7 @@ var AxiosRequest = /** @class */ (function () {
253
267
  newOptions.params = (_j = (_h = this.crypto) === null || _h === void 0 ? void 0 : _h.encrypt_des) === null || _j === void 0 ? void 0 : _j.call(_h, JSON.stringify(newOptions.params));
254
268
  }
255
269
  }
256
- return axios(newOptions);
270
+ return this.instanceAxios(newOptions);
257
271
  };
258
272
  return AxiosRequest;
259
273
  }());
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zmdms-utils",
3
- "version": "0.0.57",
3
+ "version": "0.0.58",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.js",
package/src/request.ts CHANGED
@@ -3,6 +3,7 @@ import axios, {
3
3
  AxiosRequestConfig,
4
4
  AxiosResponse,
5
5
  InternalAxiosRequestConfig,
6
+ AxiosInstance,
6
7
  } from "axios";
7
8
  import { getToken, removeToken } from "./auth";
8
9
  import { Crypto } from "./crypto";
@@ -33,6 +34,8 @@ export class AxiosRequest {
33
34
  private messageWarning?: IOtherOptions["messageWarning"];
34
35
  // modalConfirm 方法
35
36
  private modalConfirm?: IOtherOptions["modalConfirm"];
37
+ // axios单例
38
+ private instanceAxios: AxiosInstance = axios.create();
36
39
 
37
40
  constructor(otherOptions: IOtherOptions) {
38
41
  const {
@@ -59,13 +62,14 @@ export class AxiosRequest {
59
62
  // 设置axios方法默认值
60
63
  private defaultSetting() {
61
64
  // 设置默认请求头
62
- axios.defaults.headers.common["Tenant-Id"] = this.otherOptions.TenantId;
63
- axios.defaults.headers.common["Authorization"] =
65
+ this.instanceAxios.defaults.headers.common["Tenant-Id"] =
66
+ this.otherOptions.TenantId;
67
+ this.instanceAxios.defaults.headers.common["Authorization"] =
64
68
  this.otherOptions.Authorization;
65
69
 
66
70
  // 其他默认参数
67
- axios.defaults.timeout = this.otherOptions.timeout;
68
- axios.defaults.baseURL = this.otherOptions.baseURL;
71
+ this.instanceAxios.defaults.timeout = this.otherOptions.timeout;
72
+ this.instanceAxios.defaults.baseURL = this.otherOptions.baseURL;
69
73
  }
70
74
 
71
75
  // 拦截请求
@@ -74,7 +78,7 @@ export class AxiosRequest {
74
78
  this.otherOptions.interceptorsRequestHandle();
75
79
  return;
76
80
  }
77
- axios.interceptors.request.use(
81
+ this.instanceAxios.interceptors.request.use(
78
82
  (config) => {
79
83
  if (this.otherOptions.interceptorsRequestConfig) {
80
84
  return this.otherOptions.interceptorsRequestConfig(config);
@@ -93,7 +97,7 @@ export class AxiosRequest {
93
97
  this.otherOptions.interceptorsResponseHandle();
94
98
  return;
95
99
  }
96
- axios.interceptors.response.use(
100
+ this.instanceAxios.interceptors.response.use(
97
101
  (response) => {
98
102
  // console.log(response);
99
103
  // 统一处理响应正常
@@ -154,6 +158,7 @@ export class AxiosRequest {
154
158
  const { response } = error;
155
159
  // 登录过期是否自己处理
156
160
  const isHandleLoginExpiration = error?.config?.isHandleLoginExpiration;
161
+ const expirationHandle = error?.config?.expirationHandle;
157
162
  // 处理token失效
158
163
  // TODO: 刷新token逻辑
159
164
  if (response?.status === 401 && !isHandleLoginExpiration) {
@@ -163,7 +168,11 @@ export class AxiosRequest {
163
168
  this.messageWarning?.("登录过期", 3.5, () => {
164
169
  this.isExpired = false;
165
170
  });
166
- this.logoutHandle(response.status);
171
+ if (expirationHandle) {
172
+ expirationHandle(response);
173
+ } else {
174
+ this.logoutHandle(response.status);
175
+ }
167
176
  }
168
177
  return;
169
178
  }
@@ -175,7 +184,11 @@ export class AxiosRequest {
175
184
  this.messageWarning?.("你的账号已在其他地方登录!", 3.5, () => {
176
185
  this.isOffline = false;
177
186
  });
178
- this.logoutHandle(response.status);
187
+ if (expirationHandle) {
188
+ expirationHandle(response);
189
+ } else {
190
+ this.logoutHandle(response.status);
191
+ }
179
192
  }
180
193
  return;
181
194
  }
@@ -318,7 +331,7 @@ export class AxiosRequest {
318
331
  }
319
332
  }
320
333
 
321
- return axios(newOptions);
334
+ return this.instanceAxios(newOptions);
322
335
  }
323
336
  }
324
337
 
@@ -366,6 +379,10 @@ export interface IOptions extends AxiosRequestConfig {
366
379
  * 登录过期是否交给自己处理
367
380
  */
368
381
  isHandleLoginExpiration?: boolean;
382
+ /**
383
+ * 登录过期跳转逻辑交给实例自己处理
384
+ */
385
+ expirationHandle?: (options: any) => void;
369
386
  }
370
387
 
371
388
  export interface IOtherOptions {