@ibiz-template/core 0.0.1-alpha.4 → 0.0.1-alpha.40

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.
Files changed (114) hide show
  1. package/dist/system/index.system.js +1 -0
  2. package/out/constant/http-status-message/http-status-message.d.ts +5 -0
  3. package/out/constant/http-status-message/http-status-message.d.ts.map +1 -0
  4. package/out/constant/http-status-message/http-status-message.js +20 -0
  5. package/out/constant/index.d.ts +4 -0
  6. package/out/constant/index.d.ts.map +1 -1
  7. package/out/constant/index.js +4 -0
  8. package/out/constant/login-mode/login-mode.d.ts +23 -0
  9. package/out/constant/login-mode/login-mode.d.ts.map +1 -0
  10. package/out/constant/login-mode/login-mode.js +24 -0
  11. package/out/constant/menu-permission-mode/menu-permission-mode.d.ts +23 -0
  12. package/out/constant/menu-permission-mode/menu-permission-mode.d.ts.map +1 -0
  13. package/out/constant/menu-permission-mode/menu-permission-mode.js +24 -0
  14. package/out/constant/util/util.d.ts +5 -0
  15. package/out/constant/util/util.d.ts.map +1 -0
  16. package/out/constant/util/util.js +5 -0
  17. package/out/context/index.d.ts +2 -39
  18. package/out/context/index.d.ts.map +1 -1
  19. package/out/context/index.js +34 -65
  20. package/out/environment/environment.d.ts.map +1 -1
  21. package/out/environment/environment.js +11 -1
  22. package/out/error/http-error/http-error.d.ts +18 -0
  23. package/out/error/http-error/http-error.d.ts.map +1 -0
  24. package/out/error/http-error/http-error.js +36 -0
  25. package/out/error/index.d.ts +3 -0
  26. package/out/error/index.d.ts.map +1 -0
  27. package/out/error/index.js +2 -0
  28. package/out/error/runtime-error/runtime-error.d.ts +15 -0
  29. package/out/error/runtime-error/runtime-error.d.ts.map +1 -0
  30. package/out/error/runtime-error/runtime-error.js +15 -0
  31. package/out/factory-center.d.ts +11 -0
  32. package/out/factory-center.d.ts.map +1 -0
  33. package/out/factory-center.js +10 -0
  34. package/out/ibizsys.d.ts +8 -1
  35. package/out/ibizsys.d.ts.map +1 -1
  36. package/out/ibizsys.js +8 -1
  37. package/out/index.d.ts +3 -1
  38. package/out/index.d.ts.map +1 -1
  39. package/out/index.js +3 -1
  40. package/out/install.d.ts.map +1 -1
  41. package/out/install.js +4 -2
  42. package/out/interface/click-outside/click-outside.d.ts +70 -0
  43. package/out/interface/click-outside/click-outside.d.ts.map +1 -0
  44. package/out/interface/click-outside/click-outside.js +1 -0
  45. package/out/interface/i-environment/i-environment.d.ts +76 -0
  46. package/out/interface/i-environment/i-environment.d.ts.map +1 -1
  47. package/out/interface/index.d.ts +1 -0
  48. package/out/interface/index.d.ts.map +1 -1
  49. package/out/interface/index.js +1 -1
  50. package/out/types.d.ts +20 -0
  51. package/out/types.d.ts.map +1 -1
  52. package/out/utils/click-outside/click-outside.d.ts +14 -0
  53. package/out/utils/click-outside/click-outside.d.ts.map +1 -0
  54. package/out/utils/click-outside/click-outside.js +85 -0
  55. package/out/utils/event/event.d.ts +34 -0
  56. package/out/utils/event/event.d.ts.map +1 -0
  57. package/out/utils/event/event.js +60 -0
  58. package/out/utils/index.d.ts +5 -1
  59. package/out/utils/index.d.ts.map +1 -1
  60. package/out/utils/index.js +5 -1
  61. package/out/utils/interceptor/core-interceptor.d.ts +15 -0
  62. package/out/utils/interceptor/core-interceptor.d.ts.map +1 -0
  63. package/out/utils/interceptor/core-interceptor.js +30 -0
  64. package/out/utils/interceptor/index.d.ts +3 -0
  65. package/out/utils/interceptor/index.d.ts.map +1 -0
  66. package/out/utils/interceptor/index.js +2 -0
  67. package/out/utils/interceptor/interceptor.d.ts +74 -3
  68. package/out/utils/interceptor/interceptor.d.ts.map +1 -1
  69. package/out/utils/interceptor/interceptor.js +73 -22
  70. package/out/utils/logger/logger.d.ts +3 -0
  71. package/out/utils/logger/logger.d.ts.map +1 -0
  72. package/out/utils/logger/logger.js +2 -0
  73. package/out/utils/net/http-response.d.ts +4 -9
  74. package/out/utils/net/http-response.d.ts.map +1 -1
  75. package/out/utils/net/http-response.js +0 -7
  76. package/out/utils/net/net.d.ts +82 -31
  77. package/out/utils/net/net.d.ts.map +1 -1
  78. package/out/utils/net/net.js +145 -50
  79. package/out/utils/url-helper/url-helper.d.ts +51 -0
  80. package/out/utils/url-helper/url-helper.d.ts.map +1 -0
  81. package/out/utils/url-helper/url-helper.js +59 -0
  82. package/out/utils/util/util.d.ts +45 -0
  83. package/out/utils/util/util.d.ts.map +1 -1
  84. package/out/utils/util/util.js +96 -0
  85. package/package.json +18 -7
  86. package/src/constant/http-status-message/http-status-message.ts +20 -0
  87. package/src/constant/index.ts +4 -0
  88. package/src/constant/login-mode/login-mode.ts +23 -0
  89. package/src/constant/menu-permission-mode/menu-permission-mode.ts +23 -0
  90. package/src/constant/util/util.ts +5 -0
  91. package/src/context/index.ts +35 -69
  92. package/src/environment/environment.ts +11 -1
  93. package/src/error/http-error/http-error.ts +43 -0
  94. package/src/error/index.ts +2 -0
  95. package/src/error/runtime-error/runtime-error.ts +14 -0
  96. package/src/factory-center.ts +9 -0
  97. package/src/ibizsys.ts +9 -1
  98. package/src/index.ts +3 -1
  99. package/src/install.ts +4 -2
  100. package/src/interface/click-outside/click-outside.ts +71 -0
  101. package/src/interface/i-environment/i-environment.ts +86 -0
  102. package/src/interface/index.ts +1 -0
  103. package/src/types.ts +21 -0
  104. package/src/utils/click-outside/click-outside.ts +110 -0
  105. package/src/utils/event/event.ts +77 -0
  106. package/src/utils/index.ts +5 -1
  107. package/src/utils/interceptor/core-interceptor.ts +35 -0
  108. package/src/utils/interceptor/index.ts +2 -0
  109. package/src/utils/interceptor/interceptor.ts +109 -22
  110. package/src/utils/logger/logger.ts +3 -0
  111. package/src/utils/net/http-response.ts +6 -9
  112. package/src/utils/net/net.ts +175 -61
  113. package/src/utils/url-helper/url-helper.ts +62 -0
  114. package/src/utils/util/util.ts +115 -0
@@ -1,6 +1,16 @@
1
- import axios, { AxiosRequestHeaders, AxiosResponse } from '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';
12
+ import { HttpError } from '../../error';
13
+ import { Interceptor } from '../interceptor/interceptor';
4
14
  import { IHttpResponse } from './http-response';
5
15
 
6
16
  /**
@@ -13,122 +23,213 @@ import { IHttpResponse } from './http-response';
13
23
  */
14
24
  export class Net {
15
25
  /**
16
- * 标准请求前缀
26
+ * axios实例
17
27
  *
18
- * @author chitanda
19
- * @date 2022-07-21 10:07:48
28
+ * @author lxm
29
+ * @date 2022-10-27 17:10:18
20
30
  * @protected
21
- * @type {string}
31
+ * @type {AxiosInstance}
22
32
  */
23
- protected _prefix: string = '';
33
+ protected instance: AxiosInstance;
24
34
 
25
- get prefix(): string {
26
- return this._prefix || ibiz.env.baseUrl;
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);
27
43
  }
28
44
 
29
45
  /**
30
- * Creates an instance of Net.
46
+ * 注册的拦截器
31
47
  *
32
- * @author chitanda
33
- * @date 2022-07-21 10:07:27
34
- * @param {string} [prefix] 请求前缀
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 唯一标识
73
+ */
74
+ removeInterceptor(name: string) {
75
+ const interceptor = this.interceptors.get(name);
76
+ if (interceptor) {
77
+ interceptor.eject(this.instance);
78
+ this.interceptors.delete(name);
79
+ }
80
+ }
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 {*}
35
110
  */
36
- constructor(prefix?: string) {
37
- if (prefix) {
38
- this._prefix = prefix;
111
+ protected mergeConfig(...configs: AxiosRequestConfig[]) {
112
+ if (configs.length === 0) {
113
+ return this.presetConfig;
39
114
  }
115
+ return merge(this.presetConfig, ...configs);
40
116
  }
41
117
 
42
118
  /**
43
119
  * Post 请求
44
120
  *
45
121
  * @author chitanda
46
- * @date 2022-07-14 15:07:01
122
+ * @date 2022-10-19 11:10:30
47
123
  * @param {string} url
124
+ * @param {IData} data
48
125
  * @param {IParams} [params={}]
126
+ * @param {RawAxiosRequestHeaders} [headers={}]
49
127
  * @return {*} {Promise<IHttpResponse>}
50
128
  */
51
129
  async post(
52
130
  url: string,
53
131
  data: IData,
54
132
  params: IParams = {},
55
- headers: AxiosRequestHeaders = {},
133
+ headers: RawAxiosRequestHeaders = {},
56
134
  ): Promise<IHttpResponse> {
57
135
  url = this.handleAppPresetParam(url, params);
58
- const response = await axios({
59
- method: 'post',
60
- url,
61
- data,
62
- headers: {
63
- 'Content-Type': 'application/json;charset=UTF-8',
64
- Accept: 'application/json',
65
- ...headers,
66
- },
67
- });
68
- return this.doResponseResult(response);
136
+ try {
137
+ const response = await this.request(url, {
138
+ method: 'post',
139
+ data,
140
+ headers,
141
+ });
142
+ return this.doResponseResult(response);
143
+ } catch (error) {
144
+ throw new HttpError(error as AxiosError);
145
+ }
69
146
  }
70
147
 
71
148
  /**
72
149
  * Get 请求
73
150
  *
74
151
  * @author chitanda
75
- * @date 2022-07-14 15:07:08
152
+ * @date 2022-10-19 11:10:24
76
153
  * @param {string} url
77
154
  * @param {IParams} [params={}]
155
+ * @param {RawAxiosRequestHeaders} [headers={}]
156
+ * @param {IParams} [option={}]
78
157
  * @return {*} {Promise<IHttpResponse>}
79
158
  */
80
159
  async get(
81
160
  url: string,
82
161
  params: IParams = {},
83
- headers: AxiosRequestHeaders = {},
84
- option: IParams = {},
162
+ headers: RawAxiosRequestHeaders = {},
163
+ option: AxiosRequestConfig = {},
85
164
  ): Promise<IHttpResponse> {
86
165
  // if (params.srfparentdata) {
87
166
  // Object.assign(params, params.srfparentdata);
88
167
  // delete params.srfparentdata;
89
168
  // }
90
169
  url = this.attachUrlParam(url, params);
91
- const response = await axios.get(url, { headers, ...option });
92
- return this.doResponseResult(response);
170
+ try {
171
+ const response = await this.request(
172
+ url,
173
+ merge({ method: 'get', headers }, option),
174
+ );
175
+ return this.doResponseResult(response);
176
+ } catch (error) {
177
+ throw new HttpError(error as AxiosError);
178
+ }
93
179
  }
94
180
 
95
181
  /**
96
182
  * Delete 请求
97
183
  *
98
184
  * @author chitanda
99
- * @date 2022-07-14 15:07:43
185
+ * @date 2022-10-19 11:10:17
100
186
  * @param {string} url
101
187
  * @param {IParams} [params]
188
+ * @param {RawAxiosRequestHeaders} [headers={}]
102
189
  * @return {*} {Promise<IHttpResponse>}
103
190
  */
104
191
  async delete(
105
192
  url: string,
106
193
  params?: IParams,
107
- headers: AxiosRequestHeaders = {},
194
+ headers: RawAxiosRequestHeaders = {},
108
195
  ): Promise<IHttpResponse> {
109
196
  url = this.handleAppPresetParam(url, params);
110
- const response = await axios.delete(url, { headers });
111
- return this.doResponseResult(response);
197
+ try {
198
+ const response = await this.request(url, { method: 'delete', headers });
199
+ return this.doResponseResult(response);
200
+ } catch (error) {
201
+ throw new HttpError(error as AxiosError);
202
+ }
112
203
  }
113
204
 
114
205
  /**
115
206
  * Put 请求
116
207
  *
117
208
  * @author chitanda
118
- * @date 2022-07-14 15:07:49
209
+ * @date 2022-10-19 11:10:11
119
210
  * @param {string} url
120
211
  * @param {IData} data
212
+ * @param {IParams} [params={}]
213
+ * @param {RawAxiosRequestHeaders} [headers={}]
121
214
  * @return {*} {Promise<IHttpResponse>}
122
215
  */
123
216
  async put(
124
217
  url: string,
125
218
  data: IData,
126
219
  params: IParams = {},
127
- headers: AxiosRequestHeaders = {},
220
+ headers: RawAxiosRequestHeaders = {},
128
221
  ): Promise<IHttpResponse> {
129
222
  url = this.handleAppPresetParam(url, params);
130
- const response = await axios.put(url, data, { headers });
131
- return this.doResponseResult(response);
223
+ try {
224
+ const response = await this.request(url, {
225
+ method: 'put',
226
+ data,
227
+ headers,
228
+ });
229
+ return this.doResponseResult(response);
230
+ } catch (error) {
231
+ throw new HttpError(error as AxiosError);
232
+ }
132
233
  }
133
234
 
134
235
  /**
@@ -137,17 +238,44 @@ export class Net {
137
238
  * @author chitanda
138
239
  * @date 2022-07-14 15:07:15
139
240
  * @param {string} url
140
- * @param {AxiosRequestHeaders} [headers={}]
241
+ * @param {RawAxiosRequestHeaders} [headers={}]
141
242
  * @return {*} {Promise<IHttpResponse>}
142
243
  */
143
244
  async getModel(
144
245
  url: string,
145
- headers: AxiosRequestHeaders = {},
246
+ headers: RawAxiosRequestHeaders = {},
247
+ ): Promise<IHttpResponse> {
248
+ try {
249
+ const response = await this.instance.get(url, {
250
+ headers,
251
+ });
252
+ return this.doResponseResult(response);
253
+ } catch (error) {
254
+ throw new HttpError(error as AxiosError);
255
+ }
256
+ }
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 = {},
146
270
  ): Promise<IHttpResponse> {
147
- const response = await axios.get(url, {
148
- headers,
149
- });
150
- return this.doResponseResult(response);
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
+ }
151
279
  }
152
280
 
153
281
  /**
@@ -202,7 +330,6 @@ export class Net {
202
330
  * @return {*} {string}
203
331
  */
204
332
  private attachUrlParam(url: string, params: IParams): string {
205
- url = this.attachUrl(url);
206
333
  const strParams: string = stringify(params);
207
334
  if (notNilEmpty(strParams)) {
208
335
  if (url.endsWith('?')) {
@@ -217,17 +344,4 @@ export class Net {
217
344
  }
218
345
  return url;
219
346
  }
220
-
221
- /**
222
- * 处理 url 参数
223
- *
224
- * @author chitanda
225
- * @date 2022-07-20 20:07:17
226
- * @private
227
- * @param {string} url
228
- * @return {*} {string}
229
- */
230
- private attachUrl(url: string): string {
231
- return `${this.prefix}${url}`;
232
- }
233
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
+ }
@@ -1,3 +1,5 @@
1
+ /* eslint-disable @typescript-eslint/no-explicit-any */
2
+ import { debounce } from 'lodash-es';
1
3
  import { getCookie } from 'qx-util';
2
4
  import { CoreConst } from '../../constant';
3
5
 
@@ -12,3 +14,116 @@ import { CoreConst } from '../../constant';
12
14
  export function getToken(): string | null {
13
15
  return getCookie(CoreConst.TOKEN);
14
16
  }
17
+
18
+ /**
19
+ * 判断两个数组是否有相同的元素
20
+ *
21
+ * @author lxm
22
+ * @date 2022-09-20 19:09:29
23
+ * @export
24
+ * @param {any[]} arr1
25
+ * @param {any[]} arr2
26
+ * @returns {*} {boolean}
27
+ */
28
+ export function isOverlap(arr1: any[], arr2: any[]): boolean {
29
+ const newArr = Array.from(new Set([...arr1, ...arr2]));
30
+ return newArr.length !== arr1.length + arr2.length;
31
+ }
32
+
33
+ /**
34
+ * 防抖并合并每次的参数,
35
+ * 最后一次才会执行
36
+ *
37
+ * @author lxm
38
+ * @date 2022-09-20 21:09:53
39
+ * @export
40
+ * @template T
41
+ * @param {T} func 要防抖的函数
42
+ * @param {(
43
+ * oldParams: Parameters<T>,
44
+ * newParams: Parameters<T>,
45
+ * ) => Parameters<T>} mergeFunc 合并的回调函数
46
+ * @param {number} [wait] 防抖的延迟毫秒数
47
+ * @returns {*}
48
+ */
49
+ export function debounceAndMerge<T extends (...args: any[]) => any>(
50
+ func: T,
51
+ mergeFunc: (
52
+ oldParams: Parameters<T>,
53
+ newParams: Parameters<T>,
54
+ ) => Parameters<T>,
55
+ wait?: number,
56
+ ) {
57
+ // 缓存上一次的函数参数
58
+ let oldParams: Parameters<T> | undefined;
59
+ const debounceFunc = debounce((...params: Parameters<T>) => {
60
+ // 防抖函数执行的时候清空缓存参数
61
+ oldParams = undefined;
62
+ return func(...params);
63
+ }, wait);
64
+ return (...args: Parameters<T>) => {
65
+ // 合并参数,并把参数缓存到oldParams里,用新参数调用防抖函数
66
+ let newParams = args;
67
+ if (oldParams) {
68
+ newParams = mergeFunc(oldParams, newParams);
69
+ }
70
+ oldParams = newParams;
71
+ return debounceFunc(...newParams);
72
+ };
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
+ const result = await func(...params);
108
+ promises.forEach(promise => {
109
+ promise.resolve(result);
110
+ });
111
+ promises = [];
112
+ return result;
113
+ }, wait);
114
+ const fun = async (...args: Parameters<T>) => {
115
+ // 合并参数,并把参数缓存到oldParams里,用新参数调用防抖函数
116
+ let newParams = args;
117
+ if (oldParams) {
118
+ newParams = mergeFunc(oldParams, newParams);
119
+ }
120
+ oldParams = newParams;
121
+ // 执行防抖函数
122
+ debounceFunc(...newParams);
123
+ return new Promise((resolve, reject) => {
124
+ // 阻塞当前调用,当防抖函数被执行后返回
125
+ promises.push({ resolve, reject });
126
+ });
127
+ };
128
+ return fun as T;
129
+ }