@ibiz-template/core 0.0.1-beta.1 → 0.0.2-beta.0

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 (95) 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/environment/environment.d.ts.map +1 -1
  36. package/out/environment/environment.js +2 -1
  37. package/out/error/http-error/http-error.d.ts.map +1 -1
  38. package/out/error/http-error/http-error.js +0 -4
  39. package/out/ibizsys.d.ts +9 -0
  40. package/out/ibizsys.d.ts.map +1 -1
  41. package/out/ibizsys.js +9 -0
  42. package/out/index.d.ts +1 -0
  43. package/out/index.d.ts.map +1 -1
  44. package/out/index.js +1 -0
  45. package/out/install.d.ts.map +1 -1
  46. package/out/install.js +0 -2
  47. package/out/interface/click-outside/click-outside.d.ts +1 -1
  48. package/out/interface/click-outside/click-outside.d.ts.map +1 -1
  49. package/out/interface/i-environment/i-environment.d.ts +15 -8
  50. package/out/interface/i-environment/i-environment.d.ts.map +1 -1
  51. package/out/utils/download-file/download-file.d.ts +10 -0
  52. package/out/utils/download-file/download-file.d.ts.map +1 -1
  53. package/out/utils/download-file/download-file.js +20 -0
  54. package/out/utils/index.d.ts +3 -0
  55. package/out/utils/index.d.ts.map +1 -1
  56. package/out/utils/index.js +3 -0
  57. package/out/utils/net/net.d.ts.map +1 -1
  58. package/out/utils/net/net.js +4 -1
  59. package/out/utils/sync/count-latch.d.ts +60 -0
  60. package/out/utils/sync/count-latch.d.ts.map +1 -0
  61. package/out/utils/sync/count-latch.js +93 -0
  62. package/out/utils/sync/index.d.ts +2 -0
  63. package/out/utils/sync/index.d.ts.map +1 -0
  64. package/out/utils/sync/index.js +1 -0
  65. package/out/utils/upload/select-file.d.ts +53 -0
  66. package/out/utils/upload/select-file.d.ts.map +1 -0
  67. package/out/utils/upload/select-file.js +47 -0
  68. package/out/utils/upload/upload-file.d.ts +46 -0
  69. package/out/utils/upload/upload-file.d.ts.map +1 -0
  70. package/out/utils/upload/upload-file.js +151 -0
  71. package/package.json +3 -3
  72. package/src/command/command-register.ts +135 -0
  73. package/src/command/command.ts +79 -0
  74. package/src/command/index.ts +11 -0
  75. package/src/command/interface/command/command-option.ts +22 -0
  76. package/src/command/interface/command/command.ts +86 -0
  77. package/src/command/interface/disposable/disposable.ts +3 -0
  78. package/src/command/interface/index.ts +9 -0
  79. package/src/command/utils/index.ts +2 -0
  80. package/src/command/utils/linked-list.ts +136 -0
  81. package/src/command/utils/util.ts +95 -0
  82. package/src/context/index.ts +45 -1
  83. package/src/environment/environment.ts +2 -1
  84. package/src/error/http-error/http-error.ts +0 -4
  85. package/src/ibizsys.ts +10 -0
  86. package/src/index.ts +1 -0
  87. package/src/install.ts +0 -2
  88. package/src/interface/i-environment/i-environment.ts +16 -8
  89. package/src/utils/download-file/download-file.ts +21 -0
  90. package/src/utils/index.ts +3 -0
  91. package/src/utils/net/net.ts +5 -1
  92. package/src/utils/sync/count-latch.ts +99 -0
  93. package/src/utils/sync/index.ts +1 -0
  94. package/src/utils/upload/select-file.ts +86 -0
  95. package/src/utils/upload/upload-file.ts +207 -0
@@ -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,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
+
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
+ throw new Error(`多应用模式等待重新实现请求`);
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 Promise.allSettled(
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
+ }