@ibiz-template/core 0.0.1-alpha.6 → 0.0.1-beta.17
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/command/command-register.d.ts +66 -0
- package/out/command/command-register.d.ts.map +1 -0
- package/out/command/command-register.js +113 -0
- package/out/command/command.d.ts +54 -0
- package/out/command/command.d.ts.map +1 -0
- package/out/command/command.js +71 -0
- package/out/command/index.d.ts +10 -0
- package/out/command/index.d.ts.map +1 -0
- package/out/command/index.js +9 -0
- package/out/command/interface/command/command-option.d.ts +23 -0
- package/out/command/interface/command/command-option.d.ts.map +1 -0
- package/out/command/interface/command/command-option.js +1 -0
- package/out/command/interface/command/command.d.ts +74 -0
- package/out/command/interface/command/command.d.ts.map +1 -0
- package/out/command/interface/command/command.js +1 -0
- package/out/command/interface/disposable/disposable.d.ts +4 -0
- package/out/command/interface/disposable/disposable.d.ts.map +1 -0
- package/out/command/interface/disposable/disposable.js +1 -0
- package/out/command/interface/index.d.ts +4 -0
- package/out/command/interface/index.d.ts.map +1 -0
- package/out/command/interface/index.js +1 -0
- package/out/command/utils/index.d.ts +3 -0
- package/out/command/utils/index.d.ts.map +1 -0
- package/out/command/utils/index.js +2 -0
- package/out/command/utils/linked-list.d.ts +16 -0
- package/out/command/utils/linked-list.d.ts.map +1 -0
- package/out/command/utils/linked-list.js +120 -0
- package/out/command/utils/util.d.ts +27 -0
- package/out/command/utils/util.d.ts.map +1 -0
- package/out/command/utils/util.js +77 -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 +19 -32
- package/out/context/index.d.ts.map +1 -1
- package/out/context/index.js +52 -60
- package/out/environment/environment.d.ts.map +1 -1
- package/out/environment/environment.js +11 -2
- package/out/error/http-error/http-error.d.ts.map +1 -1
- package/out/error/http-error/http-error.js +0 -4
- 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 +17 -1
- package/out/ibizsys.d.ts.map +1 -1
- package/out/ibizsys.js +17 -1
- package/out/index.d.ts +3 -1
- package/out/index.d.ts.map +1 -1
- package/out/index.js +3 -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 +31 -0
- package/out/utils/download-file/download-file.d.ts.map +1 -0
- package/out/utils/download-file/download-file.js +107 -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 +10 -1
- package/out/utils/index.d.ts.map +1 -1
- package/out/utils/index.js +10 -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/sync/count-latch.d.ts +60 -0
- package/out/utils/sync/count-latch.d.ts.map +1 -0
- package/out/utils/sync/count-latch.js +93 -0
- package/out/utils/sync/index.d.ts +2 -0
- package/out/utils/sync/index.d.ts.map +1 -0
- package/out/utils/sync/index.js +1 -0
- package/out/utils/upload/select-file.d.ts +53 -0
- package/out/utils/upload/select-file.d.ts.map +1 -0
- package/out/utils/upload/select-file.js +47 -0
- package/out/utils/upload/upload-file.d.ts +46 -0
- package/out/utils/upload/upload-file.d.ts.map +1 -0
- package/out/utils/upload/upload-file.js +150 -0
- 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/command/command-register.ts +135 -0
- package/src/command/command.ts +79 -0
- package/src/command/index.ts +11 -0
- package/src/command/interface/command/command-option.ts +22 -0
- package/src/command/interface/command/command.ts +86 -0
- package/src/command/interface/disposable/disposable.ts +3 -0
- package/src/command/interface/index.ts +9 -0
- package/src/command/utils/index.ts +2 -0
- package/src/command/utils/linked-list.ts +136 -0
- package/src/command/utils/util.ts +95 -0
- 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 +69 -59
- package/src/environment/environment.ts +11 -2
- package/src/error/http-error/http-error.ts +0 -4
- package/src/factory-center.ts +9 -0
- package/src/ibizsys.ts +19 -1
- package/src/index.ts +3 -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 +109 -0
- package/src/utils/event/event.ts +77 -0
- package/src/utils/index.ts +10 -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/sync/count-latch.ts +99 -0
- package/src/utils/sync/index.ts +1 -0
- package/src/utils/upload/select-file.ts +86 -0
- package/src/utils/upload/upload-file.ts +206 -0
- 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,99 @@
|
|
|
1
|
+
import { RuntimeError } from '../../error';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 计数插销工具类
|
|
5
|
+
*
|
|
6
|
+
* @author lxm
|
|
7
|
+
* @date 2022-11-24 19:11:49
|
|
8
|
+
* @export
|
|
9
|
+
* @class CountLatch
|
|
10
|
+
*/
|
|
11
|
+
export class CountLatch {
|
|
12
|
+
private promise: Promise<void> | null = null;
|
|
13
|
+
|
|
14
|
+
private resolve: ((value: void) => void) | null = null;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* 计数,当前等待的异步逻辑个数
|
|
18
|
+
*
|
|
19
|
+
* @author lxm
|
|
20
|
+
* @date 2022-11-24 19:11:59
|
|
21
|
+
* @type {number}
|
|
22
|
+
*/
|
|
23
|
+
count: number = 0;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* 开启promise
|
|
27
|
+
*
|
|
28
|
+
* @author lxm
|
|
29
|
+
* @date 2022-11-24 19:11:32
|
|
30
|
+
* @private
|
|
31
|
+
*/
|
|
32
|
+
private startPromise() {
|
|
33
|
+
this.promise = new Promise(resolve => {
|
|
34
|
+
this.resolve = resolve;
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* 结束promise
|
|
40
|
+
*
|
|
41
|
+
* @author lxm
|
|
42
|
+
* @date 2022-11-24 19:11:44
|
|
43
|
+
* @private
|
|
44
|
+
*/
|
|
45
|
+
private endPromise() {
|
|
46
|
+
if (this.resolve) {
|
|
47
|
+
this.resolve();
|
|
48
|
+
this.resolve = null;
|
|
49
|
+
this.promise = null;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* 上锁,计数加一
|
|
55
|
+
* 第一次计数,开启异步
|
|
56
|
+
*
|
|
57
|
+
* @author lxm
|
|
58
|
+
* @date 2022-11-24 19:11:27
|
|
59
|
+
*/
|
|
60
|
+
lock() {
|
|
61
|
+
console.log('lock');
|
|
62
|
+
this.count += 1;
|
|
63
|
+
if (!this.promise) {
|
|
64
|
+
console.log('startPromise');
|
|
65
|
+
this.startPromise();
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* 解锁,计数减一
|
|
71
|
+
* 归零时结束异步
|
|
72
|
+
*
|
|
73
|
+
* @author lxm
|
|
74
|
+
* @date 2022-11-24 19:11:47
|
|
75
|
+
*/
|
|
76
|
+
unlock() {
|
|
77
|
+
if (this.count < 1) {
|
|
78
|
+
throw new RuntimeError('lock和unlock次数不匹配!');
|
|
79
|
+
}
|
|
80
|
+
console.log('unlock');
|
|
81
|
+
this.count -= 1;
|
|
82
|
+
if (this.count === 0) {
|
|
83
|
+
debugger;
|
|
84
|
+
this.endPromise();
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* 等待,计数归零异步结束
|
|
90
|
+
*
|
|
91
|
+
* @author lxm
|
|
92
|
+
* @date 2022-11-24 19:11:20
|
|
93
|
+
*/
|
|
94
|
+
async await() {
|
|
95
|
+
if (this.promise) {
|
|
96
|
+
await this.promise;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { CountLatch } from './count-latch';
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { merge } from 'lodash-es';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 文件List转数组
|
|
5
|
+
*
|
|
6
|
+
* @author lxm
|
|
7
|
+
* @date 2022-11-18 13:11:03
|
|
8
|
+
* @export
|
|
9
|
+
* @param {FileList} fileList
|
|
10
|
+
* @returns {*}
|
|
11
|
+
*/
|
|
12
|
+
export function fileListToArr(fileList: FileList): File[] {
|
|
13
|
+
const files = [];
|
|
14
|
+
for (let i = 0; i < fileList.length; i++) {
|
|
15
|
+
files.push(fileList[i]);
|
|
16
|
+
}
|
|
17
|
+
return files;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* JS打开文件上传操作配置参数
|
|
22
|
+
*
|
|
23
|
+
* @author lxm
|
|
24
|
+
* @date 2022-11-20 21:11:47
|
|
25
|
+
* @export
|
|
26
|
+
* @interface SelectFileOpts
|
|
27
|
+
*/
|
|
28
|
+
export interface SelectFileOpts {
|
|
29
|
+
/**
|
|
30
|
+
* 接受的文件类型
|
|
31
|
+
*
|
|
32
|
+
* @author lxm
|
|
33
|
+
* @date 2022-11-20 21:11:52
|
|
34
|
+
* @type {string}
|
|
35
|
+
*/
|
|
36
|
+
accept?: string;
|
|
37
|
+
/**
|
|
38
|
+
* 是否支持多选
|
|
39
|
+
*
|
|
40
|
+
* @author lxm
|
|
41
|
+
* @date 2022-11-20 21:11:52
|
|
42
|
+
* @type {boolean}
|
|
43
|
+
*/
|
|
44
|
+
multiple?: boolean;
|
|
45
|
+
/**
|
|
46
|
+
* 选中文件后回调
|
|
47
|
+
*
|
|
48
|
+
* @author lxm
|
|
49
|
+
* @date 2022-11-20 21:11:50
|
|
50
|
+
*/
|
|
51
|
+
onSelected: (_fileList: File[]) => void;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* JS打开文件上传操作
|
|
56
|
+
*
|
|
57
|
+
* @author lxm
|
|
58
|
+
* @date 2022-11-20 21:11:31
|
|
59
|
+
* @export
|
|
60
|
+
* @param {SelectFileOpts} _opts 配置参数
|
|
61
|
+
*/
|
|
62
|
+
export function selectFile(_opts: SelectFileOpts) {
|
|
63
|
+
const opts: Required<SelectFileOpts> = merge(
|
|
64
|
+
{
|
|
65
|
+
multiple: true,
|
|
66
|
+
accept: '',
|
|
67
|
+
},
|
|
68
|
+
_opts,
|
|
69
|
+
);
|
|
70
|
+
const input = document.createElement('input');
|
|
71
|
+
input.setAttribute('type', 'file');
|
|
72
|
+
input.setAttribute('multiple', `${opts.multiple}`);
|
|
73
|
+
input.setAttribute('accept', opts.accept);
|
|
74
|
+
input.onchange = e => {
|
|
75
|
+
const inputEl = e.target as HTMLInputElement;
|
|
76
|
+
const files = inputEl.files ? fileListToArr(inputEl.files) : [];
|
|
77
|
+
if (files.length === 0) {
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
opts.onSelected(files);
|
|
81
|
+
inputEl.value = ''; // 如果不置空,相同的文件不会触发change事件
|
|
82
|
+
};
|
|
83
|
+
document.body.appendChild(input);
|
|
84
|
+
input.click();
|
|
85
|
+
document.body.removeChild(input);
|
|
86
|
+
}
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-empty-function */
|
|
2
|
+
/* eslint-disable no-param-reassign */
|
|
3
|
+
import { cloneDeep, isFunction, merge, round, uniqueId } from 'lodash-es';
|
|
4
|
+
import { HttpResponse } from '../net/http-response';
|
|
5
|
+
import { selectFile } from './select-file';
|
|
6
|
+
|
|
7
|
+
export interface IUploadFile {
|
|
8
|
+
name: string;
|
|
9
|
+
uid: string;
|
|
10
|
+
status: 'uploading' | 'finished' | 'fail' | 'cancel';
|
|
11
|
+
percentage: number;
|
|
12
|
+
response?: HttpResponse;
|
|
13
|
+
error?: unknown;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface IUploadFileOpts {
|
|
17
|
+
uploadUrl: string;
|
|
18
|
+
/**
|
|
19
|
+
* 接受的文件类型
|
|
20
|
+
*
|
|
21
|
+
* @author lxm
|
|
22
|
+
* @date 2022-11-20 21:11:52
|
|
23
|
+
* @type {string}
|
|
24
|
+
*/
|
|
25
|
+
accept?: string;
|
|
26
|
+
/**
|
|
27
|
+
* 是否支持多选
|
|
28
|
+
*
|
|
29
|
+
* @author lxm
|
|
30
|
+
* @date 2022-11-20 21:11:52
|
|
31
|
+
* @type {Boolean}
|
|
32
|
+
*/
|
|
33
|
+
multiple?: boolean;
|
|
34
|
+
separate?: string;
|
|
35
|
+
request?: (_files: File[]) => Promise<HttpResponse>;
|
|
36
|
+
beforeUpload?: (_fileData: File[], _files: IUploadFile[]) => boolean;
|
|
37
|
+
finish?: (_resultFiles: IUploadFile[]) => void;
|
|
38
|
+
success?: (_resultFiles: IUploadFile[], _res: HttpResponse) => void;
|
|
39
|
+
error?: (_resultFiles: IUploadFile[], _error: unknown) => void;
|
|
40
|
+
progress?: (_files: IUploadFile[]) => void;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* 使用上传文件逻辑
|
|
45
|
+
*
|
|
46
|
+
* @author lxm
|
|
47
|
+
* @date 2022-11-20 21:11:52
|
|
48
|
+
* @export
|
|
49
|
+
* @param {IUploadFileOpts} _opts
|
|
50
|
+
* @returns {*}
|
|
51
|
+
*/
|
|
52
|
+
export function uploadFile(_opts: IUploadFileOpts) {
|
|
53
|
+
const opts: Required<IUploadFileOpts> = merge(
|
|
54
|
+
{
|
|
55
|
+
multiple: true,
|
|
56
|
+
accept: '',
|
|
57
|
+
separate: true,
|
|
58
|
+
beforeUpload: (_fileData: File[], _files: IUploadFile[]) => true,
|
|
59
|
+
finish: (_resultFiles: IUploadFile[]) => {},
|
|
60
|
+
success: (_resultFiles: IUploadFile[], _res: HttpResponse) => {},
|
|
61
|
+
error: (_resultFiles: IUploadFile[], _error: unknown) => {},
|
|
62
|
+
progress: (_files: IUploadFile[]) => {},
|
|
63
|
+
},
|
|
64
|
+
_opts,
|
|
65
|
+
);
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* 进度条回调
|
|
69
|
+
*
|
|
70
|
+
* @author lxm
|
|
71
|
+
* @date 2022-11-20 21:11:47
|
|
72
|
+
* @param {IParams} event
|
|
73
|
+
* @param {IUploadFile[]} files
|
|
74
|
+
*/
|
|
75
|
+
const onUploadProgress = (event: IParams, files: IUploadFile[]) => {
|
|
76
|
+
files.forEach(file => {
|
|
77
|
+
file.percentage = round(event.progress * 100);
|
|
78
|
+
});
|
|
79
|
+
opts.progress(cloneDeep(files));
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* 上传请求方法,返回响应对象
|
|
84
|
+
*
|
|
85
|
+
* @author lxm
|
|
86
|
+
* @date 2022-11-18 13:11:57
|
|
87
|
+
* @param {File[]} files
|
|
88
|
+
* @returns {*} {Promise<HttpResponse>}
|
|
89
|
+
*/
|
|
90
|
+
const uploadRequest = async (
|
|
91
|
+
files: File[],
|
|
92
|
+
onProgress: (_event: IParams) => void,
|
|
93
|
+
): Promise<HttpResponse> => {
|
|
94
|
+
// 自定义上传请求
|
|
95
|
+
if (opts.request && isFunction(opts.request)) {
|
|
96
|
+
return opts.request(files);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// 默认每次单个文件上传,可能存在接口一次上传多个文件,
|
|
100
|
+
const data = new FormData();
|
|
101
|
+
files.forEach(file => {
|
|
102
|
+
data.append('file', file);
|
|
103
|
+
});
|
|
104
|
+
const res = await ibiz.net.request(opts.uploadUrl, {
|
|
105
|
+
method: 'post',
|
|
106
|
+
baseURL: '',
|
|
107
|
+
data,
|
|
108
|
+
headers: { 'Content-Type': 'multipart/form-data' },
|
|
109
|
+
onUploadProgress: onProgress,
|
|
110
|
+
});
|
|
111
|
+
return res;
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* 执行一次上传,可能是一个文件也可能是多个文件
|
|
116
|
+
*
|
|
117
|
+
* @author lxm
|
|
118
|
+
* @date 2022-11-18 14:11:54
|
|
119
|
+
* @param {File[]} files 此次上传文件的集合
|
|
120
|
+
* @returns {*} {Promise<IUploadFile[]>}
|
|
121
|
+
*/
|
|
122
|
+
const executeSingleUpload = async (files: File[]): Promise<IUploadFile[]> => {
|
|
123
|
+
const resultFiles: IUploadFile[] = files.map(file => {
|
|
124
|
+
return {
|
|
125
|
+
status: 'uploading',
|
|
126
|
+
name: file.name,
|
|
127
|
+
uid: uniqueId(),
|
|
128
|
+
percentage: 0,
|
|
129
|
+
};
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
// 上传前回调,可以取消此次下载
|
|
133
|
+
const pass = opts.beforeUpload(files, resultFiles);
|
|
134
|
+
if (!pass) {
|
|
135
|
+
resultFiles.forEach(file => {
|
|
136
|
+
file.status = 'cancel';
|
|
137
|
+
});
|
|
138
|
+
ibiz.log.debug('取消上传', resultFiles);
|
|
139
|
+
return resultFiles;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
try {
|
|
143
|
+
const res = await uploadRequest(files, event => {
|
|
144
|
+
onUploadProgress(event, resultFiles);
|
|
145
|
+
});
|
|
146
|
+
resultFiles.forEach(file => {
|
|
147
|
+
file.status = 'finished';
|
|
148
|
+
});
|
|
149
|
+
// 上传成功事件
|
|
150
|
+
opts.success(resultFiles, res);
|
|
151
|
+
resultFiles.forEach(file => {
|
|
152
|
+
file.response = res;
|
|
153
|
+
});
|
|
154
|
+
} catch (error) {
|
|
155
|
+
resultFiles.forEach(file => {
|
|
156
|
+
file.status = 'fail';
|
|
157
|
+
});
|
|
158
|
+
// 上传失败事件
|
|
159
|
+
opts.error(resultFiles, error);
|
|
160
|
+
resultFiles.forEach(file => {
|
|
161
|
+
file.error = error;
|
|
162
|
+
});
|
|
163
|
+
ibiz.log.error(error);
|
|
164
|
+
ibiz.log.error(`${files.map(file => file.name).join(',')}上传失败`);
|
|
165
|
+
}
|
|
166
|
+
return resultFiles;
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* 执行上传文件逻辑
|
|
171
|
+
*
|
|
172
|
+
* @author lxm
|
|
173
|
+
* @date 2022-11-18 13:11:21
|
|
174
|
+
* @param {files} File[] 文件集合
|
|
175
|
+
*/
|
|
176
|
+
const uploadFiles = async (files: File[]) => {
|
|
177
|
+
const uploadSequence = opts.separate ? files.map(file => [file]) : [files];
|
|
178
|
+
const res = await Promise.allSettled(
|
|
179
|
+
uploadSequence.map(async sequence => {
|
|
180
|
+
return executeSingleUpload(sequence);
|
|
181
|
+
}),
|
|
182
|
+
);
|
|
183
|
+
|
|
184
|
+
// 整合所有的返回文件
|
|
185
|
+
const resultFiles: IUploadFile[] = [];
|
|
186
|
+
res.forEach(result => {
|
|
187
|
+
if (result.status === 'fulfilled') {
|
|
188
|
+
resultFiles.push(...result.value);
|
|
189
|
+
}
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
opts.finish(resultFiles);
|
|
193
|
+
};
|
|
194
|
+
|
|
195
|
+
const select = () => {
|
|
196
|
+
selectFile({
|
|
197
|
+
accept: opts.accept,
|
|
198
|
+
multiple: opts.multiple,
|
|
199
|
+
onSelected: files => {
|
|
200
|
+
uploadFiles(files);
|
|
201
|
+
},
|
|
202
|
+
});
|
|
203
|
+
};
|
|
204
|
+
|
|
205
|
+
select();
|
|
206
|
+
}
|