@ibiz-template/core 0.0.1-beta.1 → 0.0.1-beta.130
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 +5 -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 +36 -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/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 +4 -0
- package/out/utils/index.d.ts.map +1 -1
- package/out/utils/index.js +4 -0
- package/out/utils/net/net.d.ts +18 -1
- package/out/utils/net/net.d.ts.map +1 -1
- package/out/utils/net/net.js +29 -3
- 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 +150 -0
- package/package.json +7 -6
- package/src/constant/core/core.ts +10 -0
- package/src/context/index.ts +115 -5
- package/src/environment/environment.ts +5 -0
- package/src/error/http-error/http-error.ts +0 -4
- package/src/interface/i-environment/i-environment.ts +41 -0
- package/src/types.ts +35 -0
- package/src/utils/download-file/download-file.ts +21 -0
- package/src/utils/index.ts +4 -0
- package/src/utils/net/net.ts +31 -3
- 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 +206 -0
- package/LICENSE +0 -21
|
@@ -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,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
|
+
}
|
package/LICENSE
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2022 千反田丷
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|