@ibiz-template/core 0.0.1-beta.1 → 0.0.1-beta.141
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 -1
- package/out/constant/core/core.d.ts +9 -0
- package/out/constant/core/core.d.ts.map +1 -1
- package/out/constant/core/core.js +9 -0
- package/out/context/index.d.ts +63 -4
- package/out/context/index.d.ts.map +1 -1
- package/out/context/index.js +73 -3
- package/out/environment/environment.d.ts.map +1 -1
- package/out/environment/environment.js +6 -0
- package/out/error/http-error/http-error.d.ts.map +1 -1
- package/out/error/http-error/http-error.js +0 -4
- package/out/interface/click-outside/click-outside.d.ts +1 -1
- package/out/interface/click-outside/click-outside.d.ts.map +1 -1
- package/out/interface/i-environment/i-environment.d.ts +44 -0
- package/out/interface/i-environment/i-environment.d.ts.map +1 -1
- package/out/types.d.ts +31 -0
- package/out/types.d.ts.map +1 -1
- package/out/utils/data-type/data-types.d.ts +47 -0
- package/out/utils/data-type/data-types.d.ts.map +1 -0
- package/out/utils/data-type/data-types.js +96 -0
- package/out/utils/download-file/download-file.d.ts +10 -0
- package/out/utils/download-file/download-file.d.ts.map +1 -1
- package/out/utils/download-file/download-file.js +20 -0
- package/out/utils/index.d.ts +6 -0
- package/out/utils/index.d.ts.map +1 -1
- package/out/utils/index.js +6 -0
- package/out/utils/interceptor/core-interceptor.d.ts +1 -2
- package/out/utils/interceptor/core-interceptor.d.ts.map +1 -1
- package/out/utils/interceptor/core-interceptor.js +12 -7
- package/out/utils/interceptor/interceptor.d.ts +6 -6
- package/out/utils/interceptor/interceptor.d.ts.map +1 -1
- package/out/utils/interceptor/interceptor.js +2 -2
- package/out/utils/logger/logger.d.ts +1 -1
- package/out/utils/logger/logger.d.ts.map +1 -1
- package/out/utils/logger/logger.js +1 -1
- package/out/utils/net/http-response.d.ts +1 -3
- package/out/utils/net/http-response.d.ts.map +1 -1
- package/out/utils/net/net.d.ts +30 -13
- package/out/utils/net/net.d.ts.map +1 -1
- package/out/utils/net/net.js +37 -11
- package/out/utils/plural/plural.d.ts.map +1 -1
- package/out/utils/plural/plural.js +2 -0
- package/out/utils/string-util/string-util.d.ts +46 -0
- package/out/utils/string-util/string-util.d.ts.map +1 -0
- package/out/utils/string-util/string-util.js +71 -0
- package/out/utils/sync/await-timeout.d.ts +14 -0
- package/out/utils/sync/await-timeout.d.ts.map +1 -0
- package/out/utils/sync/await-timeout.js +23 -0
- 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 +90 -0
- package/out/utils/sync/index.d.ts +3 -0
- package/out/utils/sync/index.d.ts.map +1 -0
- package/out/utils/sync/index.js +2 -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 +151 -0
- package/out/utils/util/ie-helper.d.ts +12 -0
- package/out/utils/util/ie-helper.d.ts.map +1 -0
- package/out/utils/util/ie-helper.js +20 -0
- package/package.json +13 -11
- package/src/constant/core/core.ts +10 -0
- package/src/context/index.ts +115 -5
- package/src/environment/environment.ts +6 -0
- package/src/error/http-error/http-error.ts +0 -4
- package/src/interface/i-environment/i-environment.ts +50 -0
- package/src/types.ts +35 -0
- package/src/utils/data-type/data-types.ts +99 -0
- package/src/utils/download-file/download-file.ts +21 -0
- package/src/utils/index.ts +6 -0
- package/src/utils/interceptor/core-interceptor.ts +16 -11
- package/src/utils/interceptor/interceptor.ts +7 -9
- package/src/utils/logger/logger.ts +1 -1
- package/src/utils/net/http-response.ts +1 -5
- package/src/utils/net/net.ts +45 -22
- package/src/utils/plural/plural.ts +3 -0
- package/src/utils/string-util/string-util.ts +75 -0
- package/src/utils/sync/await-timeout.ts +27 -0
- package/src/utils/sync/count-latch.ts +96 -0
- package/src/utils/sync/index.ts +2 -0
- package/src/utils/upload/select-file.ts +86 -0
- package/src/utils/upload/upload-file.ts +207 -0
- package/src/utils/util/ie-helper.ts +42 -0
- package/LICENSE +0 -21
package/src/utils/net/net.ts
CHANGED
|
@@ -1,15 +1,14 @@
|
|
|
1
1
|
import axios, {
|
|
2
2
|
AxiosError,
|
|
3
|
-
RawAxiosRequestHeaders,
|
|
4
3
|
AxiosResponse,
|
|
5
4
|
AxiosRequestConfig,
|
|
6
5
|
AxiosInstance,
|
|
7
|
-
CreateAxiosDefaults,
|
|
8
6
|
} from 'axios';
|
|
9
7
|
import { merge } from 'lodash-es';
|
|
10
|
-
import
|
|
8
|
+
import qs from 'qs';
|
|
11
9
|
import { notNilEmpty } from 'qx-util';
|
|
12
10
|
import { HttpError } from '../../error';
|
|
11
|
+
import { CoreInterceptor } from '../interceptor';
|
|
13
12
|
import { Interceptor } from '../interceptor/interceptor';
|
|
14
13
|
import { IHttpResponse } from './http-response';
|
|
15
14
|
|
|
@@ -32,14 +31,24 @@ export class Net {
|
|
|
32
31
|
*/
|
|
33
32
|
protected instance: AxiosInstance;
|
|
34
33
|
|
|
34
|
+
/**
|
|
35
|
+
* 是否为 http || https 开头
|
|
36
|
+
*
|
|
37
|
+
* @author chitanda
|
|
38
|
+
* @date 2022-11-07 14:11:28
|
|
39
|
+
* @protected
|
|
40
|
+
*/
|
|
41
|
+
protected urlReg = /^http[s]?:\/\/[^\s]*/;
|
|
42
|
+
|
|
35
43
|
/**
|
|
36
44
|
* Creates an instance of Net.
|
|
37
45
|
* @author lxm
|
|
38
46
|
* @date 2022-10-27 16:10:05
|
|
39
|
-
* @param {
|
|
47
|
+
* @param {IData} [config] 创建实例用的默认配置
|
|
40
48
|
*/
|
|
41
|
-
constructor(config?:
|
|
49
|
+
constructor(config?: IData) {
|
|
42
50
|
this.instance = axios.create(config);
|
|
51
|
+
this.addInterceptor('Default', new CoreInterceptor());
|
|
43
52
|
}
|
|
44
53
|
|
|
45
54
|
/**
|
|
@@ -91,7 +100,7 @@ export class Net {
|
|
|
91
100
|
protected get presetConfig(): AxiosRequestConfig {
|
|
92
101
|
return {
|
|
93
102
|
// 请求前缀路径
|
|
94
|
-
baseURL:
|
|
103
|
+
baseURL: ibiz.env.baseUrl,
|
|
95
104
|
headers: {
|
|
96
105
|
'Content-Type': 'application/json;charset=UTF-8',
|
|
97
106
|
Accept: 'application/json',
|
|
@@ -109,10 +118,15 @@ export class Net {
|
|
|
109
118
|
* @returns {*}
|
|
110
119
|
*/
|
|
111
120
|
protected mergeConfig(...configs: AxiosRequestConfig[]) {
|
|
121
|
+
const config = this.presetConfig;
|
|
112
122
|
if (configs.length === 0) {
|
|
113
|
-
return
|
|
123
|
+
return config;
|
|
114
124
|
}
|
|
115
|
-
|
|
125
|
+
const { url } = configs[0];
|
|
126
|
+
if (url && this.urlReg.test(url)) {
|
|
127
|
+
delete config.baseURL;
|
|
128
|
+
}
|
|
129
|
+
return merge(config, ...configs);
|
|
116
130
|
}
|
|
117
131
|
|
|
118
132
|
/**
|
|
@@ -123,14 +137,14 @@ export class Net {
|
|
|
123
137
|
* @param {string} url
|
|
124
138
|
* @param {IData} data
|
|
125
139
|
* @param {IParams} [params={}]
|
|
126
|
-
* @param {
|
|
140
|
+
* @param {IData} [headers={}]
|
|
127
141
|
* @return {*} {Promise<IHttpResponse>}
|
|
128
142
|
*/
|
|
129
143
|
async post(
|
|
130
144
|
url: string,
|
|
131
145
|
data: IData,
|
|
132
146
|
params: IParams = {},
|
|
133
|
-
headers:
|
|
147
|
+
headers: IData = {},
|
|
134
148
|
): Promise<IHttpResponse> {
|
|
135
149
|
url = this.handleAppPresetParam(url, params);
|
|
136
150
|
try {
|
|
@@ -152,14 +166,14 @@ export class Net {
|
|
|
152
166
|
* @date 2022-10-19 11:10:24
|
|
153
167
|
* @param {string} url
|
|
154
168
|
* @param {IParams} [params={}]
|
|
155
|
-
* @param {
|
|
169
|
+
* @param {IData} [headers={}]
|
|
156
170
|
* @param {IParams} [option={}]
|
|
157
171
|
* @return {*} {Promise<IHttpResponse>}
|
|
158
172
|
*/
|
|
159
173
|
async get(
|
|
160
174
|
url: string,
|
|
161
175
|
params: IParams = {},
|
|
162
|
-
headers:
|
|
176
|
+
headers: IData = {},
|
|
163
177
|
option: AxiosRequestConfig = {},
|
|
164
178
|
): Promise<IHttpResponse> {
|
|
165
179
|
// if (params.srfparentdata) {
|
|
@@ -185,13 +199,13 @@ export class Net {
|
|
|
185
199
|
* @date 2022-10-19 11:10:17
|
|
186
200
|
* @param {string} url
|
|
187
201
|
* @param {IParams} [params]
|
|
188
|
-
* @param {
|
|
202
|
+
* @param {IData} [headers={}]
|
|
189
203
|
* @return {*} {Promise<IHttpResponse>}
|
|
190
204
|
*/
|
|
191
205
|
async delete(
|
|
192
206
|
url: string,
|
|
193
207
|
params?: IParams,
|
|
194
|
-
headers:
|
|
208
|
+
headers: IData = {},
|
|
195
209
|
): Promise<IHttpResponse> {
|
|
196
210
|
url = this.handleAppPresetParam(url, params);
|
|
197
211
|
try {
|
|
@@ -210,14 +224,14 @@ export class Net {
|
|
|
210
224
|
* @param {string} url
|
|
211
225
|
* @param {IData} data
|
|
212
226
|
* @param {IParams} [params={}]
|
|
213
|
-
* @param {
|
|
227
|
+
* @param {IData} [headers={}]
|
|
214
228
|
* @return {*} {Promise<IHttpResponse>}
|
|
215
229
|
*/
|
|
216
230
|
async put(
|
|
217
231
|
url: string,
|
|
218
232
|
data: IData,
|
|
219
233
|
params: IParams = {},
|
|
220
|
-
headers:
|
|
234
|
+
headers: IData = {},
|
|
221
235
|
): Promise<IHttpResponse> {
|
|
222
236
|
url = this.handleAppPresetParam(url, params);
|
|
223
237
|
try {
|
|
@@ -238,13 +252,10 @@ export class Net {
|
|
|
238
252
|
* @author chitanda
|
|
239
253
|
* @date 2022-07-14 15:07:15
|
|
240
254
|
* @param {string} url
|
|
241
|
-
* @param {
|
|
255
|
+
* @param {IData} [headers={}]
|
|
242
256
|
* @return {*} {Promise<IHttpResponse>}
|
|
243
257
|
*/
|
|
244
|
-
async getModel(
|
|
245
|
-
url: string,
|
|
246
|
-
headers: RawAxiosRequestHeaders = {},
|
|
247
|
-
): Promise<IHttpResponse> {
|
|
258
|
+
async getModel(url: string, headers: IData = {}): Promise<IHttpResponse> {
|
|
248
259
|
try {
|
|
249
260
|
const response = await this.instance.get(url, {
|
|
250
261
|
headers,
|
|
@@ -278,6 +289,18 @@ export class Net {
|
|
|
278
289
|
}
|
|
279
290
|
}
|
|
280
291
|
|
|
292
|
+
/**
|
|
293
|
+
* 创建标准 axios 请求
|
|
294
|
+
*
|
|
295
|
+
* @author chitanda
|
|
296
|
+
* @date 2023-01-30 15:01:27
|
|
297
|
+
* @param {AxiosRequestConfig<IData>} config
|
|
298
|
+
* @return {*}
|
|
299
|
+
*/
|
|
300
|
+
axios(config: AxiosRequestConfig<IData>) {
|
|
301
|
+
return axios(config);
|
|
302
|
+
}
|
|
303
|
+
|
|
281
304
|
/**
|
|
282
305
|
* 统一处理请求返回
|
|
283
306
|
*
|
|
@@ -330,7 +353,7 @@ export class Net {
|
|
|
330
353
|
* @return {*} {string}
|
|
331
354
|
*/
|
|
332
355
|
private attachUrlParam(url: string, params: IParams): string {
|
|
333
|
-
const strParams: string = stringify(params);
|
|
356
|
+
const strParams: string = qs.stringify(params);
|
|
334
357
|
if (notNilEmpty(strParams)) {
|
|
335
358
|
if (url.endsWith('?')) {
|
|
336
359
|
url = `${url}${strParams}`;
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { notNilEmpty } from 'qx-util';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 字符串工具类
|
|
5
|
+
*
|
|
6
|
+
* @author chitanda
|
|
7
|
+
* @date 2021-04-23 20:04:27
|
|
8
|
+
* @export
|
|
9
|
+
* @class StringUtil
|
|
10
|
+
*/
|
|
11
|
+
export class StringUtil {
|
|
12
|
+
/**
|
|
13
|
+
* 上下文替换正则
|
|
14
|
+
*
|
|
15
|
+
* @author chitanda
|
|
16
|
+
* @date 2021-04-23 20:04:01
|
|
17
|
+
* @static
|
|
18
|
+
*/
|
|
19
|
+
static contextReg = /\$\{context.[a-zA-Z_$][a-zA-Z0-9_$]{1,}\}/g;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* 数据替换正则
|
|
23
|
+
*
|
|
24
|
+
* @author chitanda
|
|
25
|
+
* @date 2021-04-23 20:04:09
|
|
26
|
+
* @static
|
|
27
|
+
*/
|
|
28
|
+
static dataReg = /\$\{data.[a-zA-Z_$][a-zA-Z0-9_$]{1,}\}/g;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* 填充字符串中的数据
|
|
32
|
+
*
|
|
33
|
+
* @author chitanda
|
|
34
|
+
* @date 2021-04-23 20:04:17
|
|
35
|
+
* @static
|
|
36
|
+
* @param {string} str
|
|
37
|
+
* @param {*} [context]
|
|
38
|
+
* @param {*} [data]
|
|
39
|
+
* @return {*} {string}
|
|
40
|
+
*/
|
|
41
|
+
static fill(str: string, context?: IContext, data?: IData): string {
|
|
42
|
+
if (notNilEmpty(str)) {
|
|
43
|
+
if (notNilEmpty(context)) {
|
|
44
|
+
const strArr = str.match(this.contextReg);
|
|
45
|
+
strArr?.forEach(_key => {
|
|
46
|
+
const key = _key.slice(10, _key.length - 1);
|
|
47
|
+
str = str.replace(`\${context.${key}}`, context![key] || '');
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
if (notNilEmpty(data)) {
|
|
51
|
+
const strArr = str.match(this.dataReg);
|
|
52
|
+
strArr?.forEach(_key => {
|
|
53
|
+
const key = _key.slice(7, _key.length - 1);
|
|
54
|
+
str = str.replace(`\${data.${key}}`, data![key] || '');
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return str;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* 动态匹配${}
|
|
63
|
+
*
|
|
64
|
+
* @param str
|
|
65
|
+
* @param values
|
|
66
|
+
*/
|
|
67
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
68
|
+
static dynamicMatch(str: string, values: any): string {
|
|
69
|
+
return str.replace(/\${(.*?)}/g, (_, key) => {
|
|
70
|
+
const [objKey, propName] = key.split('.');
|
|
71
|
+
const obj = values[objKey];
|
|
72
|
+
return obj ? obj[propName] : '';
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
+
/**
|
|
3
|
+
* 设置延迟wait毫米后执行fun方法,返回fun的返回值
|
|
4
|
+
*
|
|
5
|
+
* @author lxm
|
|
6
|
+
* @date 2023-03-06 08:20:57
|
|
7
|
+
* @export
|
|
8
|
+
* @template T
|
|
9
|
+
* @param {number} wait
|
|
10
|
+
* @param {T} fun
|
|
11
|
+
* @param {any[]} params
|
|
12
|
+
* @returns {*} {Promise<ReturnType<T>>}
|
|
13
|
+
*/
|
|
14
|
+
export async function awaitTimeout<T extends (...args: any) => any>(
|
|
15
|
+
wait: number,
|
|
16
|
+
fun?: T,
|
|
17
|
+
params?: any[],
|
|
18
|
+
): Promise<ReturnType<T> | undefined> {
|
|
19
|
+
await new Promise(resolve => {
|
|
20
|
+
setTimeout(() => {
|
|
21
|
+
resolve(true);
|
|
22
|
+
}, wait);
|
|
23
|
+
});
|
|
24
|
+
if (fun) {
|
|
25
|
+
return fun(...(params || []));
|
|
26
|
+
}
|
|
27
|
+
}
|
|
@@ -0,0 +1,96 @@
|
|
|
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
|
+
this.count += 1;
|
|
62
|
+
if (!this.promise) {
|
|
63
|
+
this.startPromise();
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* 解锁,计数减一
|
|
69
|
+
* 归零时结束异步
|
|
70
|
+
*
|
|
71
|
+
* @author lxm
|
|
72
|
+
* @date 2022-11-24 19:11:47
|
|
73
|
+
*/
|
|
74
|
+
unlock() {
|
|
75
|
+
if (this.count < 1) {
|
|
76
|
+
throw new RuntimeError('lock和unlock次数不匹配!');
|
|
77
|
+
}
|
|
78
|
+
this.count -= 1;
|
|
79
|
+
if (this.count === 0) {
|
|
80
|
+
debugger;
|
|
81
|
+
this.endPromise();
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* 等待,计数归零异步结束
|
|
87
|
+
*
|
|
88
|
+
* @author lxm
|
|
89
|
+
* @date 2022-11-24 19:11:20
|
|
90
|
+
*/
|
|
91
|
+
async await() {
|
|
92
|
+
if (this.promise) {
|
|
93
|
+
await this.promise;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
@@ -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,207 @@
|
|
|
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
|
+
import { promiseAllSettled } from '../util/ie-helper';
|
|
7
|
+
|
|
8
|
+
export interface IUploadFile {
|
|
9
|
+
name: string;
|
|
10
|
+
uid: string;
|
|
11
|
+
status: 'uploading' | 'finished' | 'fail' | 'cancel';
|
|
12
|
+
percentage: number;
|
|
13
|
+
response?: HttpResponse;
|
|
14
|
+
error?: unknown;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface IUploadFileOpts {
|
|
18
|
+
uploadUrl: string;
|
|
19
|
+
/**
|
|
20
|
+
* 接受的文件类型
|
|
21
|
+
*
|
|
22
|
+
* @author lxm
|
|
23
|
+
* @date 2022-11-20 21:11:52
|
|
24
|
+
* @type {string}
|
|
25
|
+
*/
|
|
26
|
+
accept?: string;
|
|
27
|
+
/**
|
|
28
|
+
* 是否支持多选
|
|
29
|
+
*
|
|
30
|
+
* @author lxm
|
|
31
|
+
* @date 2022-11-20 21:11:52
|
|
32
|
+
* @type {Boolean}
|
|
33
|
+
*/
|
|
34
|
+
multiple?: boolean;
|
|
35
|
+
separate?: string;
|
|
36
|
+
request?: (_files: File[]) => Promise<HttpResponse>;
|
|
37
|
+
beforeUpload?: (_fileData: File[], _files: IUploadFile[]) => boolean;
|
|
38
|
+
finish?: (_resultFiles: IUploadFile[]) => void;
|
|
39
|
+
success?: (_resultFiles: IUploadFile[], _res: HttpResponse) => void;
|
|
40
|
+
error?: (_resultFiles: IUploadFile[], _error: unknown) => void;
|
|
41
|
+
progress?: (_files: IUploadFile[]) => void;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* 使用上传文件逻辑
|
|
46
|
+
*
|
|
47
|
+
* @author lxm
|
|
48
|
+
* @date 2022-11-20 21:11:52
|
|
49
|
+
* @export
|
|
50
|
+
* @param {IUploadFileOpts} _opts
|
|
51
|
+
* @returns {*}
|
|
52
|
+
*/
|
|
53
|
+
export function uploadFile(_opts: IUploadFileOpts) {
|
|
54
|
+
const opts: Required<IUploadFileOpts> = merge(
|
|
55
|
+
{
|
|
56
|
+
multiple: true,
|
|
57
|
+
accept: '',
|
|
58
|
+
separate: true,
|
|
59
|
+
beforeUpload: (_fileData: File[], _files: IUploadFile[]) => true,
|
|
60
|
+
finish: (_resultFiles: IUploadFile[]) => {},
|
|
61
|
+
success: (_resultFiles: IUploadFile[], _res: HttpResponse) => {},
|
|
62
|
+
error: (_resultFiles: IUploadFile[], _error: unknown) => {},
|
|
63
|
+
progress: (_files: IUploadFile[]) => {},
|
|
64
|
+
},
|
|
65
|
+
_opts,
|
|
66
|
+
);
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* 进度条回调
|
|
70
|
+
*
|
|
71
|
+
* @author lxm
|
|
72
|
+
* @date 2022-11-20 21:11:47
|
|
73
|
+
* @param {IParams} event
|
|
74
|
+
* @param {IUploadFile[]} files
|
|
75
|
+
*/
|
|
76
|
+
const onUploadProgress = (event: IParams, files: IUploadFile[]) => {
|
|
77
|
+
files.forEach(file => {
|
|
78
|
+
file.percentage = round(event.progress * 100);
|
|
79
|
+
});
|
|
80
|
+
opts.progress(cloneDeep(files));
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* 上传请求方法,返回响应对象
|
|
85
|
+
*
|
|
86
|
+
* @author lxm
|
|
87
|
+
* @date 2022-11-18 13:11:57
|
|
88
|
+
* @param {File[]} files
|
|
89
|
+
* @returns {*} {Promise<HttpResponse>}
|
|
90
|
+
*/
|
|
91
|
+
const uploadRequest = async (
|
|
92
|
+
files: File[],
|
|
93
|
+
onProgress: (_event: IParams) => void,
|
|
94
|
+
): Promise<HttpResponse> => {
|
|
95
|
+
// 自定义上传请求
|
|
96
|
+
if (opts.request && isFunction(opts.request)) {
|
|
97
|
+
return opts.request(files);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// 默认每次单个文件上传,可能存在接口一次上传多个文件,
|
|
101
|
+
const data = new FormData();
|
|
102
|
+
files.forEach(file => {
|
|
103
|
+
data.append('file', file);
|
|
104
|
+
});
|
|
105
|
+
const res = await ibiz.net.request(opts.uploadUrl, {
|
|
106
|
+
method: 'post',
|
|
107
|
+
baseURL: '',
|
|
108
|
+
data,
|
|
109
|
+
headers: { 'Content-Type': 'multipart/form-data' },
|
|
110
|
+
onUploadProgress: onProgress,
|
|
111
|
+
});
|
|
112
|
+
return res;
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* 执行一次上传,可能是一个文件也可能是多个文件
|
|
117
|
+
*
|
|
118
|
+
* @author lxm
|
|
119
|
+
* @date 2022-11-18 14:11:54
|
|
120
|
+
* @param {File[]} files 此次上传文件的集合
|
|
121
|
+
* @returns {*} {Promise<IUploadFile[]>}
|
|
122
|
+
*/
|
|
123
|
+
const executeSingleUpload = async (files: File[]): Promise<IUploadFile[]> => {
|
|
124
|
+
const resultFiles: IUploadFile[] = files.map(file => {
|
|
125
|
+
return {
|
|
126
|
+
status: 'uploading',
|
|
127
|
+
name: file.name,
|
|
128
|
+
uid: uniqueId(),
|
|
129
|
+
percentage: 0,
|
|
130
|
+
};
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
// 上传前回调,可以取消此次下载
|
|
134
|
+
const pass = opts.beforeUpload(files, resultFiles);
|
|
135
|
+
if (!pass) {
|
|
136
|
+
resultFiles.forEach(file => {
|
|
137
|
+
file.status = 'cancel';
|
|
138
|
+
});
|
|
139
|
+
ibiz.log.debug('取消上传', resultFiles);
|
|
140
|
+
return resultFiles;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
try {
|
|
144
|
+
const res = await uploadRequest(files, event => {
|
|
145
|
+
onUploadProgress(event, resultFiles);
|
|
146
|
+
});
|
|
147
|
+
resultFiles.forEach(file => {
|
|
148
|
+
file.status = 'finished';
|
|
149
|
+
});
|
|
150
|
+
// 上传成功事件
|
|
151
|
+
opts.success(resultFiles, res);
|
|
152
|
+
resultFiles.forEach(file => {
|
|
153
|
+
file.response = res;
|
|
154
|
+
});
|
|
155
|
+
} catch (error) {
|
|
156
|
+
resultFiles.forEach(file => {
|
|
157
|
+
file.status = 'fail';
|
|
158
|
+
});
|
|
159
|
+
// 上传失败事件
|
|
160
|
+
opts.error(resultFiles, error);
|
|
161
|
+
resultFiles.forEach(file => {
|
|
162
|
+
file.error = error;
|
|
163
|
+
});
|
|
164
|
+
ibiz.log.error(error);
|
|
165
|
+
ibiz.log.error(`${files.map(file => file.name).join(',')}上传失败`);
|
|
166
|
+
}
|
|
167
|
+
return resultFiles;
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* 执行上传文件逻辑
|
|
172
|
+
*
|
|
173
|
+
* @author lxm
|
|
174
|
+
* @date 2022-11-18 13:11:21
|
|
175
|
+
* @param {files} File[] 文件集合
|
|
176
|
+
*/
|
|
177
|
+
const uploadFiles = async (files: File[]) => {
|
|
178
|
+
const uploadSequence = opts.separate ? files.map(file => [file]) : [files];
|
|
179
|
+
const res = await promiseAllSettled(
|
|
180
|
+
uploadSequence.map(async sequence => {
|
|
181
|
+
return executeSingleUpload(sequence);
|
|
182
|
+
}),
|
|
183
|
+
);
|
|
184
|
+
|
|
185
|
+
// 整合所有的返回文件
|
|
186
|
+
const resultFiles: IUploadFile[] = [];
|
|
187
|
+
res.forEach(result => {
|
|
188
|
+
if (result.status === 'fulfilled') {
|
|
189
|
+
resultFiles.push(...result.value);
|
|
190
|
+
}
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
opts.finish(resultFiles);
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
const select = () => {
|
|
197
|
+
selectFile({
|
|
198
|
+
accept: opts.accept,
|
|
199
|
+
multiple: opts.multiple,
|
|
200
|
+
onSelected: files => {
|
|
201
|
+
uploadFiles(files);
|
|
202
|
+
},
|
|
203
|
+
});
|
|
204
|
+
};
|
|
205
|
+
|
|
206
|
+
select();
|
|
207
|
+
}
|