@freelog/tools-lib 0.1.155 → 0.1.157
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/service-API/activities.d.ts +9 -0
- package/dist/service-API/operation.d.ts +9 -0
- package/dist/service-API/presentables.d.ts +1 -0
- package/dist/tools-lib.cjs.development.js +89 -13
- package/dist/tools-lib.cjs.development.js.map +1 -1
- package/dist/tools-lib.cjs.production.min.js +1 -1
- package/dist/tools-lib.cjs.production.min.js.map +1 -1
- package/dist/tools-lib.esm.js +89 -13
- package/dist/tools-lib.esm.js.map +1 -1
- package/package.json +1 -3
- package/src/service-API/activities.ts +286 -259
- package/src/service-API/operation.ts +42 -15
- package/src/service-API/presentables.ts +293 -292
- package/src/service-API/resources.ts +645 -645
- package/src/utils/tools.ts +102 -93
package/src/utils/tools.ts
CHANGED
|
@@ -1,93 +1,102 @@
|
|
|
1
|
-
import * as CryptoJS from 'crypto-js';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* 根据 File 获取 SHA1 Hash 字符串
|
|
5
|
-
* @param file
|
|
6
|
-
* @return {Promise<string>}
|
|
7
|
-
*/
|
|
8
|
-
export function getSHA1Hash(file: File): Promise<string> {
|
|
9
|
-
return new Promise((resolve) => {
|
|
10
|
-
const reader: FileReader = new FileReader();
|
|
11
|
-
reader.
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
}
|
|
1
|
+
// import * as CryptoJS from 'crypto-js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 根据 File 获取 SHA1 Hash 字符串
|
|
5
|
+
* @param file
|
|
6
|
+
* @return {Promise<string>}
|
|
7
|
+
*/
|
|
8
|
+
export function getSHA1Hash(file: File): Promise<string> {
|
|
9
|
+
return new Promise((resolve) => {
|
|
10
|
+
const reader: FileReader = new FileReader();
|
|
11
|
+
reader.readAsArrayBuffer(file);
|
|
12
|
+
reader.onload = async () => {
|
|
13
|
+
if (!reader.result) {
|
|
14
|
+
resolve('');
|
|
15
|
+
return '';
|
|
16
|
+
}
|
|
17
|
+
if (typeof reader.result === 'string') {
|
|
18
|
+
resolve('');
|
|
19
|
+
return '';
|
|
20
|
+
}
|
|
21
|
+
const sha1: string = await self.crypto.subtle.digest('SHA-1', reader.result).then(a => Array.from(new Uint8Array(a)).map(a => a.toString(16).padStart(2, '0')).join(''));
|
|
22
|
+
resolve(sha1);
|
|
23
|
+
return '';
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* 生成随机码
|
|
31
|
+
*/
|
|
32
|
+
export function generateRandomCode(strLen: number = 5): string {
|
|
33
|
+
const allStr: string = 'ABCDEFGHIJKLMNPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890';
|
|
34
|
+
const newStrArr: string[] = [];
|
|
35
|
+
for (let i = 0; i < strLen; i++) {
|
|
36
|
+
newStrArr.push(allStr[Math.floor(Math.random() * 61)]);
|
|
37
|
+
}
|
|
38
|
+
return newStrArr.join('');
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* 通过读取 cookies 获取用户 ID
|
|
43
|
+
*/
|
|
44
|
+
export function getUserIDByCookies(): number {
|
|
45
|
+
const uid: string | undefined = document.cookie.split('; ').find((co) => co.startsWith('uid='));
|
|
46
|
+
if (!uid) {
|
|
47
|
+
return -1;
|
|
48
|
+
}
|
|
49
|
+
return Number(uid.replace('uid=', ''));
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* 将服务端的合约状态转换成前端需要的状态
|
|
55
|
+
*/
|
|
56
|
+
interface TransformServerAPIContractStateParams {
|
|
57
|
+
status: 0 | 1 | 2; // 合同综合状态: 0:正常 1:已终止(不接受任何事件,也不给授权,事实上无效的合约) 2:异常
|
|
58
|
+
authStatus: 1 | 2 | 128 | number; // 合同授权状态 1:正式授权 2:测试授权 128:未获得授权
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function transformServerAPIContractState({
|
|
62
|
+
status,
|
|
63
|
+
authStatus,
|
|
64
|
+
}: TransformServerAPIContractStateParams): 'active' | 'testActive' | 'inactive' | 'terminal' | 'exception' {
|
|
65
|
+
if (status === 0) {
|
|
66
|
+
if (authStatus === 1 || authStatus === 3) {
|
|
67
|
+
return 'active';
|
|
68
|
+
}
|
|
69
|
+
if (authStatus === 2) {
|
|
70
|
+
return 'testActive';
|
|
71
|
+
}
|
|
72
|
+
if (authStatus === 128) {
|
|
73
|
+
return 'inactive';
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (status === 1) {
|
|
78
|
+
return 'terminal';
|
|
79
|
+
}
|
|
80
|
+
return 'exception';
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* 暂时休眠
|
|
85
|
+
* @param ms 休眠时常(毫秒)
|
|
86
|
+
*/
|
|
87
|
+
export function promiseSleep(ms: number = 300): Promise<void> {
|
|
88
|
+
return new Promise((resolve) => {
|
|
89
|
+
setTimeout(() => {
|
|
90
|
+
resolve();
|
|
91
|
+
}, ms);
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* 获取用户头像URL
|
|
97
|
+
* @param userID
|
|
98
|
+
*/
|
|
99
|
+
export function getAvatarUrl(userID: number = 0): string {
|
|
100
|
+
// return `${completeUrlByDomain('image')}/avatar/${userID || getUserIDByCookies()}?t=${Date.now()}`;
|
|
101
|
+
return `https://image.freelog.com/avatar/${userID || getUserIDByCookies()}?t=${Date.now()}`;
|
|
102
|
+
}
|