zmdms-utils 0.0.56 → 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.
- package/dist/es/request.d.ts +9 -0
- package/dist/es/request.js +37 -21
- package/package.json +1 -1
- package/src/request.ts +34 -11
package/dist/es/request.d.ts
CHANGED
|
@@ -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;
|
|
@@ -40,6 +41,14 @@ interface IOptions extends AxiosRequestConfig {
|
|
|
40
41
|
* 实例返回信息 简化处理
|
|
41
42
|
*/
|
|
42
43
|
isEasyMessage?: boolean;
|
|
44
|
+
/**
|
|
45
|
+
* 登录过期是否交给自己处理
|
|
46
|
+
*/
|
|
47
|
+
isHandleLoginExpiration?: boolean;
|
|
48
|
+
/**
|
|
49
|
+
* 登录过期跳转逻辑交给实例自己处理
|
|
50
|
+
*/
|
|
51
|
+
expirationHandle?: (options: any) => void;
|
|
43
52
|
}
|
|
44
53
|
interface IOtherOptions {
|
|
45
54
|
/** token过期等 需要跳转到登录页 */
|
package/dist/es/request.js
CHANGED
|
@@ -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
|
-
|
|
37
|
-
|
|
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
|
-
|
|
41
|
-
|
|
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
|
-
|
|
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
|
-
|
|
69
|
+
this.instanceAxios.interceptors.response.use(function (response) {
|
|
67
70
|
// console.log(response);
|
|
68
71
|
// 统一处理响应正常
|
|
69
72
|
var result = _this.responseSuccess(response);
|
|
@@ -117,31 +120,44 @@ 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;
|
|
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;
|
|
126
|
+
// 登录过期是否自己处理
|
|
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;
|
|
123
129
|
// 处理token失效
|
|
124
130
|
// TODO: 刷新token逻辑
|
|
125
|
-
if ((response === null || response === void 0 ? void 0 : response.status) === 401) {
|
|
131
|
+
if ((response === null || response === void 0 ? void 0 : response.status) === 401 && !isHandleLoginExpiration) {
|
|
126
132
|
removeToken();
|
|
127
133
|
if (!this.isExpired) {
|
|
128
134
|
this.isExpired = true;
|
|
129
|
-
(
|
|
135
|
+
(_c = this.messageWarning) === null || _c === void 0 ? void 0 : _c.call(this, "登录过期", 3.5, function () {
|
|
130
136
|
_this.isExpired = false;
|
|
131
137
|
});
|
|
132
|
-
|
|
138
|
+
if (expirationHandle) {
|
|
139
|
+
expirationHandle(response);
|
|
140
|
+
}
|
|
141
|
+
else {
|
|
142
|
+
this.logoutHandle(response.status);
|
|
143
|
+
}
|
|
133
144
|
}
|
|
134
145
|
return;
|
|
135
146
|
}
|
|
136
147
|
// 处理被挤下线逻辑
|
|
137
|
-
if ((response === null || response === void 0 ? void 0 : response.status) === 501) {
|
|
148
|
+
if ((response === null || response === void 0 ? void 0 : response.status) === 501 && !isHandleLoginExpiration) {
|
|
138
149
|
removeToken();
|
|
139
150
|
if (!this.isOffline) {
|
|
140
151
|
this.isOffline = true;
|
|
141
|
-
(
|
|
152
|
+
(_d = this.messageWarning) === null || _d === void 0 ? void 0 : _d.call(this, "你的账号已在其他地方登录!", 3.5, function () {
|
|
142
153
|
_this.isOffline = false;
|
|
143
154
|
});
|
|
144
|
-
|
|
155
|
+
if (expirationHandle) {
|
|
156
|
+
expirationHandle(response);
|
|
157
|
+
}
|
|
158
|
+
else {
|
|
159
|
+
this.logoutHandle(response.status);
|
|
160
|
+
}
|
|
145
161
|
}
|
|
146
162
|
return;
|
|
147
163
|
}
|
|
@@ -149,7 +165,7 @@ var AxiosRequest = /** @class */ (function () {
|
|
|
149
165
|
if ((response === null || response === void 0 ? void 0 : response.status) === 404) {
|
|
150
166
|
if (!this.isNotFound) {
|
|
151
167
|
this.isNotFound = true;
|
|
152
|
-
(
|
|
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 () {
|
|
153
169
|
_this.isNotFound = false;
|
|
154
170
|
});
|
|
155
171
|
}
|
|
@@ -157,24 +173,24 @@ var AxiosRequest = /** @class */ (function () {
|
|
|
157
173
|
}
|
|
158
174
|
// 处理接口超时逻辑
|
|
159
175
|
if (this.isTimeoutConfirm &&
|
|
160
|
-
((
|
|
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" &&
|
|
161
177
|
!response &&
|
|
162
178
|
error.code === "ECONNABORTED" &&
|
|
163
|
-
((
|
|
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) {
|
|
164
180
|
if (!this.isTimeout) {
|
|
165
181
|
this.isTimeout = true;
|
|
166
|
-
(
|
|
182
|
+
(_l = this.modalConfirm) === null || _l === void 0 ? void 0 : _l.call(this, "".concat(this.isTimeoutConfirmStr
|
|
167
183
|
? this.isTimeoutConfirmStr
|
|
168
|
-
: "请求超时,请稍后再试,或联系相关人员确认后处理!", "\n \u63A5\u53E3\u5730\u5740: ").concat((
|
|
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 () {
|
|
169
185
|
_this.isTimeout = false;
|
|
170
186
|
});
|
|
171
187
|
}
|
|
172
188
|
return;
|
|
173
189
|
}
|
|
174
190
|
// 提示业务错误
|
|
175
|
-
if ((
|
|
191
|
+
if ((_q = error === null || error === void 0 ? void 0 : error.config) === null || _q === void 0 ? void 0 : _q.isErrorMessage) {
|
|
176
192
|
// 详细信息 再开发模式开启 之后上到别的环境会去掉
|
|
177
|
-
(
|
|
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) || "服务器异常"));
|
|
178
194
|
// 接口地址: ${error?.config?.url}
|
|
179
195
|
// 响应状态码: ${error?.response?.status}
|
|
180
196
|
// 请求方式: ${error?.config?.method}
|
|
@@ -251,7 +267,7 @@ var AxiosRequest = /** @class */ (function () {
|
|
|
251
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));
|
|
252
268
|
}
|
|
253
269
|
}
|
|
254
|
-
return
|
|
270
|
+
return this.instanceAxios(newOptions);
|
|
255
271
|
};
|
|
256
272
|
return AxiosRequest;
|
|
257
273
|
}());
|
package/package.json
CHANGED
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
|
-
|
|
63
|
-
|
|
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
|
-
|
|
68
|
-
|
|
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
|
-
|
|
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
|
-
|
|
100
|
+
this.instanceAxios.interceptors.response.use(
|
|
97
101
|
(response) => {
|
|
98
102
|
// console.log(response);
|
|
99
103
|
// 统一处理响应正常
|
|
@@ -152,28 +156,39 @@ export class AxiosRequest {
|
|
|
152
156
|
// 响应错误处理
|
|
153
157
|
private responseError(error: any = {}) {
|
|
154
158
|
const { response } = error;
|
|
159
|
+
// 登录过期是否自己处理
|
|
160
|
+
const isHandleLoginExpiration = error?.config?.isHandleLoginExpiration;
|
|
161
|
+
const expirationHandle = error?.config?.expirationHandle;
|
|
155
162
|
// 处理token失效
|
|
156
163
|
// TODO: 刷新token逻辑
|
|
157
|
-
if (response?.status === 401) {
|
|
164
|
+
if (response?.status === 401 && !isHandleLoginExpiration) {
|
|
158
165
|
removeToken();
|
|
159
166
|
if (!this.isExpired) {
|
|
160
167
|
this.isExpired = true;
|
|
161
168
|
this.messageWarning?.("登录过期", 3.5, () => {
|
|
162
169
|
this.isExpired = false;
|
|
163
170
|
});
|
|
164
|
-
|
|
171
|
+
if (expirationHandle) {
|
|
172
|
+
expirationHandle(response);
|
|
173
|
+
} else {
|
|
174
|
+
this.logoutHandle(response.status);
|
|
175
|
+
}
|
|
165
176
|
}
|
|
166
177
|
return;
|
|
167
178
|
}
|
|
168
179
|
// 处理被挤下线逻辑
|
|
169
|
-
if (response?.status === 501) {
|
|
180
|
+
if (response?.status === 501 && !isHandleLoginExpiration) {
|
|
170
181
|
removeToken();
|
|
171
182
|
if (!this.isOffline) {
|
|
172
183
|
this.isOffline = true;
|
|
173
184
|
this.messageWarning?.("你的账号已在其他地方登录!", 3.5, () => {
|
|
174
185
|
this.isOffline = false;
|
|
175
186
|
});
|
|
176
|
-
|
|
187
|
+
if (expirationHandle) {
|
|
188
|
+
expirationHandle(response);
|
|
189
|
+
} else {
|
|
190
|
+
this.logoutHandle(response.status);
|
|
191
|
+
}
|
|
177
192
|
}
|
|
178
193
|
return;
|
|
179
194
|
}
|
|
@@ -316,7 +331,7 @@ export class AxiosRequest {
|
|
|
316
331
|
}
|
|
317
332
|
}
|
|
318
333
|
|
|
319
|
-
return
|
|
334
|
+
return this.instanceAxios(newOptions);
|
|
320
335
|
}
|
|
321
336
|
}
|
|
322
337
|
|
|
@@ -360,6 +375,14 @@ export interface IOptions extends AxiosRequestConfig {
|
|
|
360
375
|
* 实例返回信息 简化处理
|
|
361
376
|
*/
|
|
362
377
|
isEasyMessage?: boolean;
|
|
378
|
+
/**
|
|
379
|
+
* 登录过期是否交给自己处理
|
|
380
|
+
*/
|
|
381
|
+
isHandleLoginExpiration?: boolean;
|
|
382
|
+
/**
|
|
383
|
+
* 登录过期跳转逻辑交给实例自己处理
|
|
384
|
+
*/
|
|
385
|
+
expirationHandle?: (options: any) => void;
|
|
363
386
|
}
|
|
364
387
|
|
|
365
388
|
export interface IOtherOptions {
|