@ibiz-template/core 0.0.1-alpha.6 → 0.0.1-beta.1
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/system/index.system.js +1 -0
- package/out/constant/index.d.ts +3 -0
- package/out/constant/index.d.ts.map +1 -1
- package/out/constant/index.js +3 -0
- package/out/constant/login-mode/login-mode.d.ts +23 -0
- package/out/constant/login-mode/login-mode.d.ts.map +1 -0
- package/out/constant/login-mode/login-mode.js +24 -0
- package/out/constant/menu-permission-mode/menu-permission-mode.d.ts +23 -0
- package/out/constant/menu-permission-mode/menu-permission-mode.d.ts.map +1 -0
- package/out/constant/menu-permission-mode/menu-permission-mode.js +24 -0
- package/out/constant/util/util.d.ts +5 -0
- package/out/constant/util/util.d.ts.map +1 -0
- package/out/constant/util/util.js +5 -0
- package/out/context/index.d.ts +2 -39
- package/out/context/index.d.ts.map +1 -1
- package/out/context/index.js +33 -64
- package/out/environment/environment.d.ts.map +1 -1
- package/out/environment/environment.js +11 -2
- package/out/factory-center.d.ts +11 -0
- package/out/factory-center.d.ts.map +1 -0
- package/out/factory-center.js +10 -0
- package/out/ibizsys.d.ts +8 -1
- package/out/ibizsys.d.ts.map +1 -1
- package/out/ibizsys.js +8 -1
- package/out/index.d.ts +2 -1
- package/out/index.d.ts.map +1 -1
- package/out/index.js +2 -1
- package/out/install.d.ts.map +1 -1
- package/out/install.js +4 -2
- package/out/interface/click-outside/click-outside.d.ts +70 -0
- package/out/interface/click-outside/click-outside.d.ts.map +1 -0
- package/out/interface/click-outside/click-outside.js +1 -0
- package/out/interface/i-environment/i-environment.d.ts +72 -4
- package/out/interface/i-environment/i-environment.d.ts.map +1 -1
- package/out/interface/index.d.ts +1 -0
- package/out/interface/index.d.ts.map +1 -1
- package/out/interface/index.js +1 -1
- package/out/types.d.ts +10 -0
- package/out/types.d.ts.map +1 -1
- package/out/utils/click-outside/click-outside.d.ts +14 -0
- package/out/utils/click-outside/click-outside.d.ts.map +1 -0
- package/out/utils/click-outside/click-outside.js +85 -0
- package/out/utils/color/color.d.ts +13 -0
- package/out/utils/color/color.d.ts.map +1 -0
- package/out/utils/color/color.js +76 -0
- package/out/utils/download-file/download-file.d.ts +21 -0
- package/out/utils/download-file/download-file.d.ts.map +1 -0
- package/out/utils/download-file/download-file.js +87 -0
- package/out/utils/event/event.d.ts +34 -0
- package/out/utils/event/event.d.ts.map +1 -0
- package/out/utils/event/event.js +60 -0
- package/out/utils/index.d.ts +7 -1
- package/out/utils/index.d.ts.map +1 -1
- package/out/utils/index.js +7 -1
- package/out/utils/interceptor/core-interceptor.d.ts +15 -0
- package/out/utils/interceptor/core-interceptor.d.ts.map +1 -0
- package/out/utils/interceptor/core-interceptor.js +30 -0
- package/out/utils/interceptor/index.d.ts +3 -0
- package/out/utils/interceptor/index.d.ts.map +1 -0
- package/out/utils/interceptor/index.js +2 -0
- package/out/utils/interceptor/interceptor.d.ts +74 -3
- package/out/utils/interceptor/interceptor.d.ts.map +1 -1
- package/out/utils/interceptor/interceptor.js +73 -22
- package/out/utils/logger/logger.d.ts +3 -0
- package/out/utils/logger/logger.d.ts.map +1 -0
- package/out/utils/logger/logger.js +2 -0
- package/out/utils/net/http-response.d.ts +4 -9
- package/out/utils/net/http-response.d.ts.map +1 -1
- package/out/utils/net/http-response.js +0 -7
- package/out/utils/net/net.d.ts +82 -31
- package/out/utils/net/net.d.ts.map +1 -1
- package/out/utils/net/net.js +109 -40
- package/out/utils/url-helper/url-helper.d.ts +51 -0
- package/out/utils/url-helper/url-helper.d.ts.map +1 -0
- package/out/utils/url-helper/url-helper.js +59 -0
- package/out/utils/util/util.d.ts +20 -4
- package/out/utils/util/util.d.ts.map +1 -1
- package/out/utils/util/util.js +59 -5
- package/package.json +19 -9
- package/src/constant/index.ts +3 -0
- package/src/constant/login-mode/login-mode.ts +23 -0
- package/src/constant/menu-permission-mode/menu-permission-mode.ts +23 -0
- package/src/constant/util/util.ts +5 -0
- package/src/context/index.ts +34 -68
- package/src/environment/environment.ts +11 -2
- package/src/factory-center.ts +9 -0
- package/src/ibizsys.ts +9 -1
- package/src/index.ts +2 -1
- package/src/install.ts +4 -2
- package/src/interface/click-outside/click-outside.ts +71 -0
- package/src/interface/i-environment/i-environment.ts +81 -4
- package/src/interface/index.ts +1 -0
- package/src/types.ts +10 -0
- package/src/utils/click-outside/click-outside.ts +110 -0
- package/src/utils/color/color.ts +84 -0
- package/src/utils/download-file/download-file.ts +88 -0
- package/src/utils/event/event.ts +77 -0
- package/src/utils/index.ts +7 -1
- package/src/utils/interceptor/core-interceptor.ts +35 -0
- package/src/utils/interceptor/index.ts +2 -0
- package/src/utils/interceptor/interceptor.ts +109 -22
- package/src/utils/logger/logger.ts +3 -0
- package/src/utils/net/http-response.ts +6 -9
- package/src/utils/net/net.ts +144 -51
- package/src/utils/url-helper/url-helper.ts +62 -0
- package/src/utils/util/util.ts +72 -14
package/src/utils/net/net.ts
CHANGED
|
@@ -1,7 +1,16 @@
|
|
|
1
|
-
import axios, {
|
|
1
|
+
import axios, {
|
|
2
|
+
AxiosError,
|
|
3
|
+
RawAxiosRequestHeaders,
|
|
4
|
+
AxiosResponse,
|
|
5
|
+
AxiosRequestConfig,
|
|
6
|
+
AxiosInstance,
|
|
7
|
+
CreateAxiosDefaults,
|
|
8
|
+
} from 'axios';
|
|
9
|
+
import { merge } from 'lodash-es';
|
|
2
10
|
import { stringify } from 'qs';
|
|
3
11
|
import { notNilEmpty } from 'qx-util';
|
|
4
12
|
import { HttpError } from '../../error';
|
|
13
|
+
import { Interceptor } from '../interceptor/interceptor';
|
|
5
14
|
import { IHttpResponse } from './http-response';
|
|
6
15
|
|
|
7
16
|
/**
|
|
@@ -14,58 +23,121 @@ import { IHttpResponse } from './http-response';
|
|
|
14
23
|
*/
|
|
15
24
|
export class Net {
|
|
16
25
|
/**
|
|
17
|
-
*
|
|
26
|
+
* axios实例
|
|
18
27
|
*
|
|
19
|
-
* @author
|
|
20
|
-
* @date 2022-
|
|
28
|
+
* @author lxm
|
|
29
|
+
* @date 2022-10-27 17:10:18
|
|
21
30
|
* @protected
|
|
22
|
-
* @type {
|
|
31
|
+
* @type {AxiosInstance}
|
|
23
32
|
*/
|
|
24
|
-
protected
|
|
33
|
+
protected instance: AxiosInstance;
|
|
25
34
|
|
|
26
|
-
|
|
27
|
-
|
|
35
|
+
/**
|
|
36
|
+
* Creates an instance of Net.
|
|
37
|
+
* @author lxm
|
|
38
|
+
* @date 2022-10-27 16:10:05
|
|
39
|
+
* @param {CreateAxiosDefaults} [config] 创建实例用的默认配置
|
|
40
|
+
*/
|
|
41
|
+
constructor(config?: CreateAxiosDefaults) {
|
|
42
|
+
this.instance = axios.create(config);
|
|
28
43
|
}
|
|
29
44
|
|
|
30
45
|
/**
|
|
31
|
-
*
|
|
46
|
+
* 注册的拦截器
|
|
32
47
|
*
|
|
33
|
-
* @author
|
|
34
|
-
* @date 2022-
|
|
35
|
-
* @
|
|
48
|
+
* @author lxm
|
|
49
|
+
* @date 2022-10-27 17:10:18
|
|
50
|
+
* @type {Map<string, Interceptor>}
|
|
51
|
+
*/
|
|
52
|
+
interceptors: Map<string, Interceptor> = new Map();
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* 添加拦截器
|
|
56
|
+
*
|
|
57
|
+
* @author lxm
|
|
58
|
+
* @date 2022-10-27 17:10:42
|
|
59
|
+
* @param {string} name 唯一标识
|
|
60
|
+
* @param {Interceptor} interceptor 拦截器
|
|
61
|
+
*/
|
|
62
|
+
addInterceptor(name: string, interceptor: Interceptor) {
|
|
63
|
+
interceptor.use(this.instance);
|
|
64
|
+
this.interceptors.set(name, interceptor);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* 删除拦截器
|
|
69
|
+
*
|
|
70
|
+
* @author lxm
|
|
71
|
+
* @date 2022-10-27 17:10:27
|
|
72
|
+
* @param {string} name 唯一标识
|
|
36
73
|
*/
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
74
|
+
removeInterceptor(name: string) {
|
|
75
|
+
const interceptor = this.interceptors.get(name);
|
|
76
|
+
if (interceptor) {
|
|
77
|
+
interceptor.eject(this.instance);
|
|
78
|
+
this.interceptors.delete(name);
|
|
40
79
|
}
|
|
41
80
|
}
|
|
42
81
|
|
|
82
|
+
/**
|
|
83
|
+
* 预置config,绑定动态的配置
|
|
84
|
+
*
|
|
85
|
+
* @author lxm
|
|
86
|
+
* @date 2022-10-27 16:10:48
|
|
87
|
+
* @readonly
|
|
88
|
+
* @protected
|
|
89
|
+
* @type {AxiosRequestConfig}
|
|
90
|
+
*/
|
|
91
|
+
protected get presetConfig(): AxiosRequestConfig {
|
|
92
|
+
return {
|
|
93
|
+
// 请求前缀路径
|
|
94
|
+
baseURL: this.instance.defaults.baseURL || ibiz.env.baseUrl,
|
|
95
|
+
headers: {
|
|
96
|
+
'Content-Type': 'application/json;charset=UTF-8',
|
|
97
|
+
Accept: 'application/json',
|
|
98
|
+
},
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* 从左到右递归合并配置参数(内置第一个合并的预置参数)
|
|
104
|
+
*
|
|
105
|
+
* @author lxm
|
|
106
|
+
* @date 2022-10-27 16:10:09
|
|
107
|
+
* @protected
|
|
108
|
+
* @param {...AxiosRequestConfig[]} configs
|
|
109
|
+
* @returns {*}
|
|
110
|
+
*/
|
|
111
|
+
protected mergeConfig(...configs: AxiosRequestConfig[]) {
|
|
112
|
+
if (configs.length === 0) {
|
|
113
|
+
return this.presetConfig;
|
|
114
|
+
}
|
|
115
|
+
return merge(this.presetConfig, ...configs);
|
|
116
|
+
}
|
|
117
|
+
|
|
43
118
|
/**
|
|
44
119
|
* Post 请求
|
|
45
120
|
*
|
|
46
121
|
* @author chitanda
|
|
47
|
-
* @date 2022-
|
|
122
|
+
* @date 2022-10-19 11:10:30
|
|
48
123
|
* @param {string} url
|
|
124
|
+
* @param {IData} data
|
|
49
125
|
* @param {IParams} [params={}]
|
|
126
|
+
* @param {RawAxiosRequestHeaders} [headers={}]
|
|
50
127
|
* @return {*} {Promise<IHttpResponse>}
|
|
51
128
|
*/
|
|
52
129
|
async post(
|
|
53
130
|
url: string,
|
|
54
131
|
data: IData,
|
|
55
132
|
params: IParams = {},
|
|
56
|
-
headers:
|
|
133
|
+
headers: RawAxiosRequestHeaders = {},
|
|
57
134
|
): Promise<IHttpResponse> {
|
|
58
135
|
url = this.handleAppPresetParam(url, params);
|
|
59
136
|
try {
|
|
60
|
-
const response = await
|
|
137
|
+
const response = await this.request(url, {
|
|
61
138
|
method: 'post',
|
|
62
|
-
url,
|
|
63
139
|
data,
|
|
64
|
-
headers
|
|
65
|
-
'Content-Type': 'application/json;charset=UTF-8',
|
|
66
|
-
Accept: 'application/json',
|
|
67
|
-
...headers,
|
|
68
|
-
},
|
|
140
|
+
headers,
|
|
69
141
|
});
|
|
70
142
|
return this.doResponseResult(response);
|
|
71
143
|
} catch (error) {
|
|
@@ -77,16 +149,18 @@ export class Net {
|
|
|
77
149
|
* Get 请求
|
|
78
150
|
*
|
|
79
151
|
* @author chitanda
|
|
80
|
-
* @date 2022-
|
|
152
|
+
* @date 2022-10-19 11:10:24
|
|
81
153
|
* @param {string} url
|
|
82
154
|
* @param {IParams} [params={}]
|
|
155
|
+
* @param {RawAxiosRequestHeaders} [headers={}]
|
|
156
|
+
* @param {IParams} [option={}]
|
|
83
157
|
* @return {*} {Promise<IHttpResponse>}
|
|
84
158
|
*/
|
|
85
159
|
async get(
|
|
86
160
|
url: string,
|
|
87
161
|
params: IParams = {},
|
|
88
|
-
headers:
|
|
89
|
-
option:
|
|
162
|
+
headers: RawAxiosRequestHeaders = {},
|
|
163
|
+
option: AxiosRequestConfig = {},
|
|
90
164
|
): Promise<IHttpResponse> {
|
|
91
165
|
// if (params.srfparentdata) {
|
|
92
166
|
// Object.assign(params, params.srfparentdata);
|
|
@@ -94,7 +168,10 @@ export class Net {
|
|
|
94
168
|
// }
|
|
95
169
|
url = this.attachUrlParam(url, params);
|
|
96
170
|
try {
|
|
97
|
-
const response = await
|
|
171
|
+
const response = await this.request(
|
|
172
|
+
url,
|
|
173
|
+
merge({ method: 'get', headers }, option),
|
|
174
|
+
);
|
|
98
175
|
return this.doResponseResult(response);
|
|
99
176
|
} catch (error) {
|
|
100
177
|
throw new HttpError(error as AxiosError);
|
|
@@ -105,19 +182,20 @@ export class Net {
|
|
|
105
182
|
* Delete 请求
|
|
106
183
|
*
|
|
107
184
|
* @author chitanda
|
|
108
|
-
* @date 2022-
|
|
185
|
+
* @date 2022-10-19 11:10:17
|
|
109
186
|
* @param {string} url
|
|
110
187
|
* @param {IParams} [params]
|
|
188
|
+
* @param {RawAxiosRequestHeaders} [headers={}]
|
|
111
189
|
* @return {*} {Promise<IHttpResponse>}
|
|
112
190
|
*/
|
|
113
191
|
async delete(
|
|
114
192
|
url: string,
|
|
115
193
|
params?: IParams,
|
|
116
|
-
headers:
|
|
194
|
+
headers: RawAxiosRequestHeaders = {},
|
|
117
195
|
): Promise<IHttpResponse> {
|
|
118
196
|
url = this.handleAppPresetParam(url, params);
|
|
119
197
|
try {
|
|
120
|
-
const response = await
|
|
198
|
+
const response = await this.request(url, { method: 'delete', headers });
|
|
121
199
|
return this.doResponseResult(response);
|
|
122
200
|
} catch (error) {
|
|
123
201
|
throw new HttpError(error as AxiosError);
|
|
@@ -128,20 +206,26 @@ export class Net {
|
|
|
128
206
|
* Put 请求
|
|
129
207
|
*
|
|
130
208
|
* @author chitanda
|
|
131
|
-
* @date 2022-
|
|
209
|
+
* @date 2022-10-19 11:10:11
|
|
132
210
|
* @param {string} url
|
|
133
211
|
* @param {IData} data
|
|
212
|
+
* @param {IParams} [params={}]
|
|
213
|
+
* @param {RawAxiosRequestHeaders} [headers={}]
|
|
134
214
|
* @return {*} {Promise<IHttpResponse>}
|
|
135
215
|
*/
|
|
136
216
|
async put(
|
|
137
217
|
url: string,
|
|
138
218
|
data: IData,
|
|
139
219
|
params: IParams = {},
|
|
140
|
-
headers:
|
|
220
|
+
headers: RawAxiosRequestHeaders = {},
|
|
141
221
|
): Promise<IHttpResponse> {
|
|
142
222
|
url = this.handleAppPresetParam(url, params);
|
|
143
223
|
try {
|
|
144
|
-
const response = await
|
|
224
|
+
const response = await this.request(url, {
|
|
225
|
+
method: 'put',
|
|
226
|
+
data,
|
|
227
|
+
headers,
|
|
228
|
+
});
|
|
145
229
|
return this.doResponseResult(response);
|
|
146
230
|
} catch (error) {
|
|
147
231
|
throw new HttpError(error as AxiosError);
|
|
@@ -154,15 +238,15 @@ export class Net {
|
|
|
154
238
|
* @author chitanda
|
|
155
239
|
* @date 2022-07-14 15:07:15
|
|
156
240
|
* @param {string} url
|
|
157
|
-
* @param {
|
|
241
|
+
* @param {RawAxiosRequestHeaders} [headers={}]
|
|
158
242
|
* @return {*} {Promise<IHttpResponse>}
|
|
159
243
|
*/
|
|
160
244
|
async getModel(
|
|
161
245
|
url: string,
|
|
162
|
-
headers:
|
|
246
|
+
headers: RawAxiosRequestHeaders = {},
|
|
163
247
|
): Promise<IHttpResponse> {
|
|
164
248
|
try {
|
|
165
|
-
const response = await
|
|
249
|
+
const response = await this.instance.get(url, {
|
|
166
250
|
headers,
|
|
167
251
|
});
|
|
168
252
|
return this.doResponseResult(response);
|
|
@@ -171,6 +255,29 @@ export class Net {
|
|
|
171
255
|
}
|
|
172
256
|
}
|
|
173
257
|
|
|
258
|
+
/**
|
|
259
|
+
* 基础请求方法,会合并预置配置
|
|
260
|
+
*
|
|
261
|
+
* @author lxm
|
|
262
|
+
* @date 2022-10-27 14:10:06
|
|
263
|
+
* @param {string} url
|
|
264
|
+
* @param {AxiosRequestConfig} [config={}]
|
|
265
|
+
* @returns {*} {Promise<IHttpResponse>}
|
|
266
|
+
*/
|
|
267
|
+
async request(
|
|
268
|
+
url: string,
|
|
269
|
+
config: AxiosRequestConfig = {},
|
|
270
|
+
): Promise<IHttpResponse> {
|
|
271
|
+
try {
|
|
272
|
+
const response = await this.instance.request(
|
|
273
|
+
this.mergeConfig({ url }, config),
|
|
274
|
+
);
|
|
275
|
+
return this.doResponseResult(response);
|
|
276
|
+
} catch (error) {
|
|
277
|
+
throw new HttpError(error as AxiosError);
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
|
|
174
281
|
/**
|
|
175
282
|
* 统一处理请求返回
|
|
176
283
|
*
|
|
@@ -223,7 +330,6 @@ export class Net {
|
|
|
223
330
|
* @return {*} {string}
|
|
224
331
|
*/
|
|
225
332
|
private attachUrlParam(url: string, params: IParams): string {
|
|
226
|
-
url = this.attachUrl(url);
|
|
227
333
|
const strParams: string = stringify(params);
|
|
228
334
|
if (notNilEmpty(strParams)) {
|
|
229
335
|
if (url.endsWith('?')) {
|
|
@@ -238,17 +344,4 @@ export class Net {
|
|
|
238
344
|
}
|
|
239
345
|
return url;
|
|
240
346
|
}
|
|
241
|
-
|
|
242
|
-
/**
|
|
243
|
-
* 处理 url 参数
|
|
244
|
-
*
|
|
245
|
-
* @author chitanda
|
|
246
|
-
* @date 2022-07-20 20:07:17
|
|
247
|
-
* @private
|
|
248
|
-
* @param {string} url
|
|
249
|
-
* @return {*} {string}
|
|
250
|
-
*/
|
|
251
|
-
private attachUrl(url: string): string {
|
|
252
|
-
return `${this.prefix}${url}`;
|
|
253
|
-
}
|
|
254
347
|
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 路径解析助手
|
|
3
|
+
*
|
|
4
|
+
* @author lxm
|
|
5
|
+
* @date 2022-10-11 14:10:12
|
|
6
|
+
* @export
|
|
7
|
+
* @class UrlHelper
|
|
8
|
+
*/
|
|
9
|
+
export class UrlHelper {
|
|
10
|
+
/**
|
|
11
|
+
* 路由路径前面的基础路径
|
|
12
|
+
* 如:http://172.16.103.120:30061/portalwebapp/#/index/appportalview?params=123
|
|
13
|
+
* 返回:http://172.16.103.120:30061/portalwebapp/#
|
|
14
|
+
*
|
|
15
|
+
* @author lxm
|
|
16
|
+
* @date 2022-10-11 20:10:29
|
|
17
|
+
* @returns {*}
|
|
18
|
+
*/
|
|
19
|
+
static get routeBase() {
|
|
20
|
+
return `${this.appBase}/#`;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* 应用的的基础路径
|
|
25
|
+
* 如:http://172.16.103.120:30061/portalwebapp/#/index/appportalview?params=123
|
|
26
|
+
* 返回:http://172.16.103.120:30061/portalwebapp
|
|
27
|
+
*
|
|
28
|
+
* @author lxm
|
|
29
|
+
* @date 2022-10-11 20:10:29
|
|
30
|
+
* @returns {*}
|
|
31
|
+
*/
|
|
32
|
+
static get appBase() {
|
|
33
|
+
const { origin, pathname } = window.location;
|
|
34
|
+
return `${origin}${pathname}`.replace(/\/$/, '');
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* #开始到末尾,即路由地址
|
|
39
|
+
* 如:http://172.16.103.120:30061/portalwebapp/#/index/appportalview?params=123
|
|
40
|
+
* 返回:/index/appportalview?params=123
|
|
41
|
+
*
|
|
42
|
+
* @author lxm
|
|
43
|
+
* @date 2022-10-11 16:10:40
|
|
44
|
+
* @returns {*}
|
|
45
|
+
*/
|
|
46
|
+
static get routePath() {
|
|
47
|
+
return window.location.hash.replace('#', '');
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* 当前地址的全路径,包含域名和参数
|
|
52
|
+
* 如:http://172.16.103.120:30061/portalwebapp/#/index/appportalview?params=123
|
|
53
|
+
*
|
|
54
|
+
* @author lxm
|
|
55
|
+
* @date 2022-10-11 21:10:39
|
|
56
|
+
* @readonly
|
|
57
|
+
* @static
|
|
58
|
+
*/
|
|
59
|
+
static get fullPath() {
|
|
60
|
+
return window.location.href;
|
|
61
|
+
}
|
|
62
|
+
}
|
package/src/utils/util/util.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
-
import { debounce
|
|
2
|
+
import { debounce } from 'lodash-es';
|
|
3
3
|
import { getCookie } from 'qx-util';
|
|
4
4
|
import { CoreConst } from '../../constant';
|
|
5
5
|
|
|
@@ -27,11 +27,12 @@ export function getToken(): string | null {
|
|
|
27
27
|
*/
|
|
28
28
|
export function isOverlap(arr1: any[], arr2: any[]): boolean {
|
|
29
29
|
const newArr = Array.from(new Set([...arr1, ...arr2]));
|
|
30
|
-
return newArr.length
|
|
30
|
+
return newArr.length !== arr1.length + arr2.length;
|
|
31
31
|
}
|
|
32
32
|
|
|
33
33
|
/**
|
|
34
|
-
*
|
|
34
|
+
* 防抖并合并每次的参数,
|
|
35
|
+
* 最后一次才会执行
|
|
35
36
|
*
|
|
36
37
|
* @author lxm
|
|
37
38
|
* @date 2022-09-20 21:09:53
|
|
@@ -43,7 +44,6 @@ export function isOverlap(arr1: any[], arr2: any[]): boolean {
|
|
|
43
44
|
* newParams: Parameters<T>,
|
|
44
45
|
* ) => Parameters<T>} mergeFunc 合并的回调函数
|
|
45
46
|
* @param {number} [wait] 防抖的延迟毫秒数
|
|
46
|
-
* @param {DebounceSettings} [options] 防抖函数的额外参数
|
|
47
47
|
* @returns {*}
|
|
48
48
|
*/
|
|
49
49
|
export function debounceAndMerge<T extends (...args: any[]) => any>(
|
|
@@ -53,19 +53,14 @@ export function debounceAndMerge<T extends (...args: any[]) => any>(
|
|
|
53
53
|
newParams: Parameters<T>,
|
|
54
54
|
) => Parameters<T>,
|
|
55
55
|
wait?: number,
|
|
56
|
-
options?: DebounceSettings,
|
|
57
56
|
) {
|
|
58
57
|
// 缓存上一次的函数参数
|
|
59
58
|
let oldParams: Parameters<T> | undefined;
|
|
60
|
-
const debounceFunc = debounce(
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
},
|
|
66
|
-
wait,
|
|
67
|
-
options,
|
|
68
|
-
);
|
|
59
|
+
const debounceFunc = debounce((...params: Parameters<T>) => {
|
|
60
|
+
// 防抖函数执行的时候清空缓存参数
|
|
61
|
+
oldParams = undefined;
|
|
62
|
+
return func(...params);
|
|
63
|
+
}, wait);
|
|
69
64
|
return (...args: Parameters<T>) => {
|
|
70
65
|
// 合并参数,并把参数缓存到oldParams里,用新参数调用防抖函数
|
|
71
66
|
let newParams = args;
|
|
@@ -76,3 +71,66 @@ export function debounceAndMerge<T extends (...args: any[]) => any>(
|
|
|
76
71
|
return debounceFunc(...newParams);
|
|
77
72
|
};
|
|
78
73
|
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* 防抖并合并每次的参数,最后一次才会执行
|
|
77
|
+
* 绑定方法为异步方法,给每次调用返回最终执行那次的结果
|
|
78
|
+
*
|
|
79
|
+
* @author lxm
|
|
80
|
+
* @date 2022-09-20 21:09:53
|
|
81
|
+
* @export
|
|
82
|
+
* @template T
|
|
83
|
+
* @param {T} func 要防抖的函数
|
|
84
|
+
* @param {(
|
|
85
|
+
* oldParams: Parameters<T>,
|
|
86
|
+
* newParams: Parameters<T>,
|
|
87
|
+
* ) => Parameters<T>} mergeFunc 合并的回调函数
|
|
88
|
+
* @param {number} [wait] 防抖的延迟毫秒数
|
|
89
|
+
* @returns {*}
|
|
90
|
+
*/
|
|
91
|
+
export function debounceAndAsyncMerge<
|
|
92
|
+
T extends (...args: any[]) => Promise<any>,
|
|
93
|
+
>(
|
|
94
|
+
func: T,
|
|
95
|
+
mergeFunc: (
|
|
96
|
+
oldParams: Parameters<T>,
|
|
97
|
+
newParams: Parameters<T>,
|
|
98
|
+
) => Parameters<T>,
|
|
99
|
+
wait?: number,
|
|
100
|
+
): T {
|
|
101
|
+
// 缓存上一次的函数参数
|
|
102
|
+
let oldParams: Parameters<T> | undefined;
|
|
103
|
+
let promises: Array<{ resolve: any; reject: any }> = [];
|
|
104
|
+
const debounceFunc = debounce(async (...params: Parameters<T>) => {
|
|
105
|
+
// 防抖函数执行的时候清空缓存参数
|
|
106
|
+
oldParams = undefined;
|
|
107
|
+
try {
|
|
108
|
+
const result = await func(...params);
|
|
109
|
+
promises.forEach(promise => {
|
|
110
|
+
promise.resolve(result);
|
|
111
|
+
});
|
|
112
|
+
promises = [];
|
|
113
|
+
return result;
|
|
114
|
+
} catch (error) {
|
|
115
|
+
promises.forEach(promise => {
|
|
116
|
+
promise.reject(error);
|
|
117
|
+
});
|
|
118
|
+
promises = [];
|
|
119
|
+
}
|
|
120
|
+
}, wait);
|
|
121
|
+
const fun = async (...args: Parameters<T>) => {
|
|
122
|
+
// 合并参数,并把参数缓存到oldParams里,用新参数调用防抖函数
|
|
123
|
+
let newParams = args;
|
|
124
|
+
if (oldParams) {
|
|
125
|
+
newParams = mergeFunc(oldParams, newParams);
|
|
126
|
+
}
|
|
127
|
+
oldParams = newParams;
|
|
128
|
+
// 执行防抖函数
|
|
129
|
+
debounceFunc(...newParams);
|
|
130
|
+
return new Promise((resolve, reject) => {
|
|
131
|
+
// 阻塞当前调用,当防抖函数被执行后返回
|
|
132
|
+
promises.push({ resolve, reject });
|
|
133
|
+
});
|
|
134
|
+
};
|
|
135
|
+
return fun as T;
|
|
136
|
+
}
|