@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.
Files changed (83) hide show
  1. package/dist/system/index.system.js +1 -1
  2. package/out/command/command-register.d.ts +66 -0
  3. package/out/command/command-register.d.ts.map +1 -0
  4. package/out/command/command-register.js +113 -0
  5. package/out/command/command.d.ts +54 -0
  6. package/out/command/command.d.ts.map +1 -0
  7. package/out/command/command.js +71 -0
  8. package/out/command/index.d.ts +10 -0
  9. package/out/command/index.d.ts.map +1 -0
  10. package/out/command/index.js +9 -0
  11. package/out/command/interface/command/command-option.d.ts +23 -0
  12. package/out/command/interface/command/command-option.d.ts.map +1 -0
  13. package/out/command/interface/command/command-option.js +1 -0
  14. package/out/command/interface/command/command.d.ts +74 -0
  15. package/out/command/interface/command/command.d.ts.map +1 -0
  16. package/out/command/interface/command/command.js +1 -0
  17. package/out/command/interface/disposable/disposable.d.ts +4 -0
  18. package/out/command/interface/disposable/disposable.d.ts.map +1 -0
  19. package/out/command/interface/disposable/disposable.js +1 -0
  20. package/out/command/interface/index.d.ts +4 -0
  21. package/out/command/interface/index.d.ts.map +1 -0
  22. package/out/command/interface/index.js +1 -0
  23. package/out/command/utils/index.d.ts +3 -0
  24. package/out/command/utils/index.d.ts.map +1 -0
  25. package/out/command/utils/index.js +2 -0
  26. package/out/command/utils/linked-list.d.ts +16 -0
  27. package/out/command/utils/linked-list.d.ts.map +1 -0
  28. package/out/command/utils/linked-list.js +120 -0
  29. package/out/command/utils/util.d.ts +27 -0
  30. package/out/command/utils/util.d.ts.map +1 -0
  31. package/out/command/utils/util.js +77 -0
  32. package/out/context/index.d.ts +24 -0
  33. package/out/context/index.d.ts.map +1 -1
  34. package/out/context/index.js +24 -1
  35. package/out/error/http-error/http-error.d.ts.map +1 -1
  36. package/out/error/http-error/http-error.js +0 -4
  37. package/out/ibizsys.d.ts +9 -0
  38. package/out/ibizsys.d.ts.map +1 -1
  39. package/out/ibizsys.js +9 -0
  40. package/out/index.d.ts +1 -0
  41. package/out/index.d.ts.map +1 -1
  42. package/out/index.js +1 -0
  43. package/out/interface/click-outside/click-outside.d.ts +1 -1
  44. package/out/interface/click-outside/click-outside.d.ts.map +1 -1
  45. package/out/utils/download-file/download-file.d.ts +10 -0
  46. package/out/utils/download-file/download-file.d.ts.map +1 -1
  47. package/out/utils/download-file/download-file.js +20 -0
  48. package/out/utils/index.d.ts +3 -0
  49. package/out/utils/index.d.ts.map +1 -1
  50. package/out/utils/index.js +3 -0
  51. package/out/utils/sync/count-latch.d.ts +60 -0
  52. package/out/utils/sync/count-latch.d.ts.map +1 -0
  53. package/out/utils/sync/count-latch.js +93 -0
  54. package/out/utils/sync/index.d.ts +2 -0
  55. package/out/utils/sync/index.d.ts.map +1 -0
  56. package/out/utils/sync/index.js +1 -0
  57. package/out/utils/upload/select-file.d.ts +53 -0
  58. package/out/utils/upload/select-file.d.ts.map +1 -0
  59. package/out/utils/upload/select-file.js +47 -0
  60. package/out/utils/upload/upload-file.d.ts +46 -0
  61. package/out/utils/upload/upload-file.d.ts.map +1 -0
  62. package/out/utils/upload/upload-file.js +150 -0
  63. package/package.json +3 -3
  64. package/src/command/command-register.ts +135 -0
  65. package/src/command/command.ts +79 -0
  66. package/src/command/index.ts +11 -0
  67. package/src/command/interface/command/command-option.ts +22 -0
  68. package/src/command/interface/command/command.ts +86 -0
  69. package/src/command/interface/disposable/disposable.ts +3 -0
  70. package/src/command/interface/index.ts +9 -0
  71. package/src/command/utils/index.ts +2 -0
  72. package/src/command/utils/linked-list.ts +136 -0
  73. package/src/command/utils/util.ts +95 -0
  74. package/src/context/index.ts +45 -1
  75. package/src/error/http-error/http-error.ts +0 -4
  76. package/src/ibizsys.ts +10 -0
  77. package/src/index.ts +1 -0
  78. package/src/utils/download-file/download-file.ts +21 -0
  79. package/src/utils/index.ts +3 -0
  80. package/src/utils/sync/count-latch.ts +99 -0
  81. package/src/utils/sync/index.ts +1 -0
  82. package/src/utils/upload/select-file.ts +86 -0
  83. 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
+ }