@ibiz-template/core 0.0.1-beta.1 → 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 -1
- 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/context/index.d.ts +24 -0
- package/out/context/index.d.ts.map +1 -1
- package/out/context/index.js +24 -1
- package/out/error/http-error/http-error.d.ts.map +1 -1
- package/out/error/http-error/http-error.js +0 -4
- package/out/ibizsys.d.ts +9 -0
- package/out/ibizsys.d.ts.map +1 -1
- package/out/ibizsys.js +9 -0
- package/out/index.d.ts +1 -0
- package/out/index.d.ts.map +1 -1
- package/out/index.js +1 -0
- 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/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 +3 -0
- package/out/utils/index.d.ts.map +1 -1
- package/out/utils/index.js +3 -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 +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/package.json +3 -3
- 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/context/index.ts +45 -1
- package/src/error/http-error/http-error.ts +0 -4
- package/src/ibizsys.ts +10 -0
- package/src/index.ts +1 -0
- package/src/utils/download-file/download-file.ts +21 -0
- package/src/utils/index.ts +3 -0
- 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
|
@@ -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
|
+
}
|