@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,136 @@
1
+ /* eslint-disable @typescript-eslint/no-non-null-assertion */
2
+ /* eslint-disable no-underscore-dangle */
3
+ /* eslint-disable max-classes-per-file */
4
+ class Node<E> {
5
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
6
+ static readonly Undefined = new Node<any>(undefined);
7
+
8
+ element: E;
9
+
10
+ next: Node<E>;
11
+
12
+ prev: Node<E>;
13
+
14
+ constructor(element: E) {
15
+ this.element = element;
16
+ this.next = Node.Undefined;
17
+ this.prev = Node.Undefined;
18
+ }
19
+ }
20
+
21
+ export class LinkedList<E> {
22
+ private _first: Node<E> = Node.Undefined;
23
+
24
+ private _last: Node<E> = Node.Undefined;
25
+
26
+ private _size: number = 0;
27
+
28
+ get size(): number {
29
+ return this._size;
30
+ }
31
+
32
+ isEmpty(): boolean {
33
+ return this._first === Node.Undefined;
34
+ }
35
+
36
+ clear(): void {
37
+ let node = this._first;
38
+ while (node !== Node.Undefined) {
39
+ const { next } = node;
40
+ node.prev = Node.Undefined;
41
+ node.next = Node.Undefined;
42
+ node = next;
43
+ }
44
+
45
+ this._first = Node.Undefined;
46
+ this._last = Node.Undefined;
47
+ this._size = 0;
48
+ }
49
+
50
+ unshift(element: E): () => void {
51
+ return this._insert(element, false);
52
+ }
53
+
54
+ push(element: E): () => void {
55
+ return this._insert(element, true);
56
+ }
57
+
58
+ private _insert(element: E, atTheEnd: boolean): () => void {
59
+ const newNode = new Node(element);
60
+ if (this._first === Node.Undefined) {
61
+ this._first = newNode;
62
+ this._last = newNode;
63
+ } else if (atTheEnd) {
64
+ // push
65
+ const oldLast = this._last!;
66
+ this._last = newNode;
67
+ newNode.prev = oldLast;
68
+ oldLast.next = newNode;
69
+ } else {
70
+ // unshift
71
+ const oldFirst = this._first;
72
+ this._first = newNode;
73
+ newNode.next = oldFirst;
74
+ oldFirst.prev = newNode;
75
+ }
76
+ this._size += 1;
77
+
78
+ let didRemove = false;
79
+ return () => {
80
+ if (!didRemove) {
81
+ didRemove = true;
82
+ this._remove(newNode);
83
+ }
84
+ };
85
+ }
86
+
87
+ shift(): E | undefined {
88
+ if (this._first === Node.Undefined) {
89
+ return undefined;
90
+ }
91
+ const res = this._first.element;
92
+ this._remove(this._first);
93
+ return res;
94
+ }
95
+
96
+ pop(): E | undefined {
97
+ if (this._last === Node.Undefined) {
98
+ return undefined;
99
+ }
100
+ const res = this._last.element;
101
+ this._remove(this._last);
102
+ return res;
103
+ }
104
+
105
+ private _remove(node: Node<E>): void {
106
+ if (node.prev !== Node.Undefined && node.next !== Node.Undefined) {
107
+ // middle
108
+ const anchor = node.prev;
109
+ anchor.next = node.next;
110
+ node.next.prev = anchor;
111
+ } else if (node.prev === Node.Undefined && node.next === Node.Undefined) {
112
+ // only node
113
+ this._first = Node.Undefined;
114
+ this._last = Node.Undefined;
115
+ } else if (node.next === Node.Undefined) {
116
+ // last
117
+ this._last = this._last!.prev!;
118
+ this._last.next = Node.Undefined;
119
+ } else if (node.prev === Node.Undefined) {
120
+ // first
121
+ this._first = this._first!.next!;
122
+ this._first.prev = Node.Undefined;
123
+ }
124
+
125
+ // done
126
+ this._size -= 1;
127
+ }
128
+
129
+ *[Symbol.iterator](): Iterator<E> {
130
+ let node = this._first;
131
+ while (node !== Node.Undefined) {
132
+ yield node.element;
133
+ node = node.next;
134
+ }
135
+ }
136
+ }
@@ -0,0 +1,95 @@
1
+ /* eslint-disable func-names */
2
+ /* eslint-disable prefer-rest-params */
3
+ /* eslint-disable no-underscore-dangle */
4
+ import { IDisposable } from '../interface';
5
+
6
+ // eslint-disable-next-line @typescript-eslint/ban-types
7
+ export function once<T extends Function>(this: unknown, fn: T): T {
8
+ // eslint-disable-next-line @typescript-eslint/no-this-alias
9
+ const _this = this;
10
+ let didCall = false;
11
+ let result: unknown;
12
+
13
+ return function () {
14
+ if (didCall) {
15
+ return result;
16
+ }
17
+
18
+ didCall = true;
19
+ result = fn.apply(_this, arguments);
20
+
21
+ return result;
22
+ } as unknown as T;
23
+ }
24
+
25
+ export function toDisposable(fn: () => void): IDisposable {
26
+ const self = {
27
+ dispose: once(() => {
28
+ fn();
29
+ }),
30
+ };
31
+ return self;
32
+ }
33
+
34
+ /**
35
+ * 函数防抖---“立即执行版本” 和 “非立即执行版本” 的组合版本
36
+ *
37
+ * @author chitanda
38
+ * @date 2022-06-29 19:06:15
39
+ * @export
40
+ * @param {((...args: unknown[]) => void | Promise<void>)} func 处理函数
41
+ * @param {number} wait 延迟执行时间(毫秒)
42
+ * @param {boolean} [immediate] 是否立即执行
43
+ * @return {*} {(...args: unknown[]) => void}
44
+ */
45
+ export function debounce(
46
+ func: (...args: unknown[]) => void | Promise<void>,
47
+ wait: number,
48
+ immediate?: boolean,
49
+ ): (...args: unknown[]) => void {
50
+ let timer: unknown;
51
+
52
+ return function (this: unknown, ...args: unknown[]): void {
53
+ if (timer) {
54
+ clearTimeout(timer as number);
55
+ }
56
+ if (immediate) {
57
+ const callNow = !timer;
58
+ timer = setTimeout(() => {
59
+ timer = null;
60
+ }, wait);
61
+ if (callNow) {
62
+ func.apply(this, args);
63
+ }
64
+ } else {
65
+ timer = setTimeout(() => {
66
+ func.apply(this, args);
67
+ }, wait);
68
+ }
69
+ };
70
+ }
71
+
72
+ /**
73
+ * 节流函数
74
+ *
75
+ * @author chitanda
76
+ * @date 2022-06-29 20:06:38
77
+ * @export
78
+ * @param {((...args: unknown[]) => void | Promise<void>)} fn
79
+ * @param {number} wait
80
+ * @return {*} {(...args: unknown[]) => void}
81
+ */
82
+ export function throttle(
83
+ fn: (...args: unknown[]) => void | Promise<void>,
84
+ wait: number,
85
+ ): (...args: unknown[]) => void {
86
+ let timer: unknown = null;
87
+ return function (this: unknown, ...args: unknown[]): void {
88
+ if (!timer) {
89
+ timer = setTimeout(() => {
90
+ fn.apply(this, args);
91
+ timer = null;
92
+ }, wait);
93
+ }
94
+ };
95
+ }
@@ -10,8 +10,25 @@ export class IBizContext implements IContext {
10
10
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
11
11
  [key: string | symbol]: any;
12
12
 
13
+ /**
14
+ * 修改的父上下文
15
+ *
16
+ * @author lxm
17
+ * @date 2022-12-08 18:12:16
18
+ * @protected
19
+ * @type {IContext}
20
+ */
13
21
  protected _context!: IContext;
14
22
 
23
+ /**
24
+ * 父的上下文源对象
25
+ *
26
+ * @author lxm
27
+ * @date 2022-12-08 18:12:31
28
+ * @type {IBizContext}
29
+ */
30
+ declare _parent?: IBizContext;
31
+
15
32
  /**
16
33
  * Creates an instance of IBizContext.
17
34
  *
@@ -25,7 +42,13 @@ export class IBizContext implements IContext {
25
42
  if (parent) {
26
43
  // eslint-disable-next-line @typescript-eslint/no-this-alias
27
44
  const self = this;
28
- // 定义私有变量,用于存储父已有上下文,方便覆盖
45
+ // 定义私有变量,存放父上下文源对象
46
+ Object.defineProperty(this, '_parent', {
47
+ enumerable: false,
48
+ value: parent,
49
+ });
50
+
51
+ // 定义私有变量,用于存储对父已有上下文的修改。
29
52
  Object.defineProperty(this, '_context', {
30
53
  enumerable: false,
31
54
  value: {},
@@ -56,4 +79,25 @@ export class IBizContext implements IContext {
56
79
  // 合并给入上下文
57
80
  Object.assign(this, context);
58
81
  }
82
+
83
+ /**
84
+ * 返回自身的上下文,独有的和与父有差异的。
85
+ *
86
+ * @author lxm
87
+ * @date 2022-12-08 17:12:26
88
+ * @returns {*} {IContext}
89
+ */
90
+ getOwnContext(): IContext {
91
+ const result: IContext = {};
92
+ Object.keys(this).forEach(key => {
93
+ // 父没有的,或者修改了父的上下文
94
+ if (
95
+ !Object.prototype.hasOwnProperty.call(this._parent, key) ||
96
+ Object.prototype.hasOwnProperty.call(this._context, key)
97
+ ) {
98
+ result[key] = this[key];
99
+ }
100
+ });
101
+ return result;
102
+ }
59
103
  }
@@ -1,5 +1,4 @@
1
1
  import { AxiosError, AxiosResponse } from 'axios';
2
- import { HttpStatusMessageConst } from '../../constant';
3
2
 
4
3
  /**
5
4
  * 请求异常
@@ -28,9 +27,6 @@ export class HttpError implements Error {
28
27
  } else {
29
28
  this.message = res.statusText;
30
29
  }
31
- if (!this.message) {
32
- this.message = HttpStatusMessageConst[res.status];
33
- }
34
30
  if (!this.message) {
35
31
  this.message = '网络异常,请稍后重试!';
36
32
  }
package/src/ibizsys.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { CommandController } from './command';
1
2
  import { Environment } from './environment/environment';
2
3
  import { OrgData } from './interface';
3
4
  import { Logger, Net } from './utils';
@@ -36,6 +37,15 @@ export class IBizSys {
36
37
  */
37
38
  net: Net = new Net();
38
39
 
40
+ /**
41
+ * 认证服务
42
+ *
43
+ * @author chitanda
44
+ * @date 2022-07-20 10:07:33
45
+ * @type {AuthService}
46
+ */
47
+ commands: CommandController = new CommandController();
48
+
39
49
  /**
40
50
  * sass 模式下的中心系统标识
41
51
  *
package/src/index.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import './types';
2
2
 
3
+ export * from './command';
3
4
  export * from './constant';
4
5
  export { IBizContext } from './context';
5
6
  export { Environment } from './environment/environment';
@@ -51,12 +51,33 @@ export function calcMimeByFileName(fileName: string) {
51
51
  case '.tar':
52
52
  mime = 'application/x-tar';
53
53
  break;
54
+ case '.xlsx':
55
+ mime = 'application/vnd.ms-excel';
56
+ break;
54
57
  default:
55
58
  mime = '';
56
59
  }
57
60
  return mime;
58
61
  }
59
62
 
63
+ /**
64
+ * 判断是否是图片格式
65
+ *
66
+ * @author lxm
67
+ * @date 2022-11-21 13:11:23
68
+ * @export
69
+ * @param {string} fileName
70
+ * @returns {*} {boolean}
71
+ */
72
+ export function isImage(fileName: string): boolean {
73
+ const ext = fileName.split('.').pop();
74
+ if (!ext) {
75
+ return false;
76
+ }
77
+ const imageTypes = ['.jpeg', 'jpg', 'gif', 'png', 'bmp', 'svg'];
78
+ return imageTypes.includes(ext);
79
+ }
80
+
60
81
  /**
61
82
  * 纯JS触发下载文件
62
83
  *
@@ -10,3 +10,6 @@ export * from './event/event';
10
10
  export * from './click-outside/click-outside';
11
11
  export * from './color/color';
12
12
  export * from './download-file/download-file';
13
+ export * from './upload/select-file';
14
+ export * from './upload/upload-file';
15
+ export * from './sync';
@@ -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
+ }