@zl-ui-kit/utils 0.5.1 → 0.6.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.
- package/README.md +6 -1
- package/dist/index.js +57 -61
- package/package.json +1 -1
- package/vendor/mtoken.js +4088 -3873
package/README.md
CHANGED
|
@@ -10,7 +10,12 @@ ZL UI 通用工具函数库(TypeScript):数组/对象/DOM/函数、AES 加
|
|
|
10
10
|
```ts
|
|
11
11
|
import { createRequest, convertArrayToTree, paginate, aesEncrypt } from '@zl-ui-kit/utils'
|
|
12
12
|
|
|
13
|
-
const request = createRequest({
|
|
13
|
+
const request = createRequest({
|
|
14
|
+
baseURL: '/api',
|
|
15
|
+
onError(code, err) {
|
|
16
|
+
/* ... */
|
|
17
|
+
},
|
|
18
|
+
})
|
|
14
19
|
```
|
|
15
20
|
|
|
16
21
|
注入式设计(决策 §12-Q3):`baseURL`、调试开关等均由参数注入,**不读取构建环境变量**,Vite/webpack 消费方均可使用。
|
package/dist/index.js
CHANGED
|
@@ -23,8 +23,7 @@ function convertArrayToTree(list, options = {}) {
|
|
|
23
23
|
while (true) {
|
|
24
24
|
const item = Object.entries(parentChain)[index];
|
|
25
25
|
index += 1;
|
|
26
|
-
if (!item)
|
|
27
|
-
break;
|
|
26
|
+
if (!item) break;
|
|
28
27
|
const childrenIds = item[1].slice();
|
|
29
28
|
for (const child of childrenIds) {
|
|
30
29
|
childrenIds.push(...parentChain[child] ?? []);
|
|
@@ -47,28 +46,20 @@ var DATABASE_AES_KEY = "b6fa92796c6431c5";
|
|
|
47
46
|
var DATABASE_AES_IV_KEY = "7b51fd7053196308";
|
|
48
47
|
function aesEncrypt(password, aesKey = DEFAULT_AES_KEY, aesIvKey = DEFAULT_AES_IV_KEY) {
|
|
49
48
|
const ivs = CryptoJS.enc.Utf8.parse(aesIvKey);
|
|
50
|
-
const encrypted = CryptoJS.AES.encrypt(
|
|
51
|
-
|
|
52
|
-
CryptoJS.
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
padding: CryptoJS.pad.Pkcs7,
|
|
56
|
-
mode: CryptoJS.mode.CBC
|
|
57
|
-
}
|
|
58
|
-
);
|
|
49
|
+
const encrypted = CryptoJS.AES.encrypt(password, CryptoJS.enc.Utf8.parse(aesKey), {
|
|
50
|
+
iv: ivs,
|
|
51
|
+
padding: CryptoJS.pad.Pkcs7,
|
|
52
|
+
mode: CryptoJS.mode.CBC
|
|
53
|
+
});
|
|
59
54
|
return encrypted.toString();
|
|
60
55
|
}
|
|
61
56
|
function aesDecrypt(encrypted, aesKey = DEFAULT_AES_KEY, aesIvKey = DEFAULT_AES_IV_KEY) {
|
|
62
57
|
const ivs = CryptoJS.enc.Utf8.parse(aesIvKey);
|
|
63
|
-
const decrypted = CryptoJS.AES.decrypt(
|
|
64
|
-
|
|
65
|
-
CryptoJS.
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
padding: CryptoJS.pad.Pkcs7,
|
|
69
|
-
mode: CryptoJS.mode.CBC
|
|
70
|
-
}
|
|
71
|
-
);
|
|
58
|
+
const decrypted = CryptoJS.AES.decrypt(encrypted, CryptoJS.enc.Utf8.parse(aesKey), {
|
|
59
|
+
iv: ivs,
|
|
60
|
+
padding: CryptoJS.pad.Pkcs7,
|
|
61
|
+
mode: CryptoJS.mode.CBC
|
|
62
|
+
});
|
|
72
63
|
return decrypted.toString(CryptoJS.enc.Utf8);
|
|
73
64
|
}
|
|
74
65
|
|
|
@@ -86,8 +77,7 @@ function getRelativeRect(target2, relative) {
|
|
|
86
77
|
};
|
|
87
78
|
}
|
|
88
79
|
function getStyle(element, prop) {
|
|
89
|
-
if (!element || !prop)
|
|
90
|
-
return null;
|
|
80
|
+
if (!element || !prop) return null;
|
|
91
81
|
let styleName = prop;
|
|
92
82
|
if (prop === "float") {
|
|
93
83
|
styleName = "cssFloat";
|
|
@@ -278,47 +268,50 @@ function createRequest(options = {}) {
|
|
|
278
268
|
}
|
|
279
269
|
return config;
|
|
280
270
|
});
|
|
281
|
-
request.interceptors.response.use(
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
271
|
+
request.interceptors.response.use(
|
|
272
|
+
(res) => {
|
|
273
|
+
const key = generateRequestKey(res.config);
|
|
274
|
+
requestMap.delete(key);
|
|
275
|
+
let data = res.data;
|
|
276
|
+
if (res.request.responseType === "blob" || res.request.responseType === "arraybuffer" || res.headers?.["content-type"] === "text/csv") {
|
|
277
|
+
return res.data;
|
|
278
|
+
} else if (enableCipher && typeof data === "string" && data?.startsWith?.(CRYPTO_CIPHER_CODE)) {
|
|
279
|
+
try {
|
|
280
|
+
const dataStr = aesDecrypt(data.slice(CRYPTO_CIPHER_CODE.length));
|
|
281
|
+
data = JSON.parse(dataStr);
|
|
282
|
+
if (logDecrypt) {
|
|
283
|
+
console.log("decrypt data: ", res.config.url, "\n", dataStr);
|
|
284
|
+
}
|
|
285
|
+
} catch {
|
|
293
286
|
}
|
|
294
|
-
} catch {
|
|
295
287
|
}
|
|
288
|
+
onResponse?.(data, res);
|
|
289
|
+
const code = Number(data?.code);
|
|
290
|
+
if (code === successCode) {
|
|
291
|
+
return data;
|
|
292
|
+
}
|
|
293
|
+
onError?.(code, res);
|
|
294
|
+
return Promise.reject(new RequestError(code, data?.msg));
|
|
295
|
+
},
|
|
296
|
+
(error) => {
|
|
297
|
+
if (isCancel(error)) {
|
|
298
|
+
return Promise.resolve();
|
|
299
|
+
}
|
|
300
|
+
let code = 9e3;
|
|
301
|
+
let errorExtraInfo;
|
|
302
|
+
const message = error.message ?? "";
|
|
303
|
+
if (message.includes("Network Error")) {
|
|
304
|
+
code = 9501;
|
|
305
|
+
} else if (message.includes("timeout")) {
|
|
306
|
+
code = 9502;
|
|
307
|
+
} else if (message.includes("Request failed with status code")) {
|
|
308
|
+
code = 9503;
|
|
309
|
+
errorExtraInfo = message.slice(message.length - 3);
|
|
310
|
+
}
|
|
311
|
+
onError?.(code, error, errorExtraInfo);
|
|
312
|
+
return Promise.reject(new RequestError(code, message, errorExtraInfo));
|
|
296
313
|
}
|
|
297
|
-
|
|
298
|
-
const code = Number(data?.code);
|
|
299
|
-
if (code === successCode) {
|
|
300
|
-
return data;
|
|
301
|
-
}
|
|
302
|
-
onError?.(code, res);
|
|
303
|
-
return Promise.reject(new RequestError(code, data?.msg));
|
|
304
|
-
}, (error) => {
|
|
305
|
-
if (isCancel(error)) {
|
|
306
|
-
return Promise.resolve();
|
|
307
|
-
}
|
|
308
|
-
let code = 9e3;
|
|
309
|
-
let errorExtraInfo;
|
|
310
|
-
const message = error.message ?? "";
|
|
311
|
-
if (message.includes("Network Error")) {
|
|
312
|
-
code = 9501;
|
|
313
|
-
} else if (message.includes("timeout")) {
|
|
314
|
-
code = 9502;
|
|
315
|
-
} else if (message.includes("Request failed with status code")) {
|
|
316
|
-
code = 9503;
|
|
317
|
-
errorExtraInfo = message.slice(message.length - 3);
|
|
318
|
-
}
|
|
319
|
-
onError?.(code, error, errorExtraInfo);
|
|
320
|
-
return Promise.reject(new RequestError(code, message, errorExtraInfo));
|
|
321
|
-
});
|
|
314
|
+
);
|
|
322
315
|
return request;
|
|
323
316
|
}
|
|
324
317
|
function setupRequest(request, onRequest, onError) {
|
|
@@ -335,7 +328,10 @@ var request_default = createRequest;
|
|
|
335
328
|
|
|
336
329
|
// src/index.ts
|
|
337
330
|
function generateUUID() {
|
|
338
|
-
return ([1e7].toString() + -1e3 + -4e3 + -8e3 + -1e11).replace(
|
|
331
|
+
return ([1e7].toString() + -1e3 + -4e3 + -8e3 + -1e11).replace(
|
|
332
|
+
/[018]/g,
|
|
333
|
+
(c) => (Number(c) ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> Number(c) / 4).toString(16)
|
|
334
|
+
);
|
|
339
335
|
}
|
|
340
336
|
function getRandomInt(min, max) {
|
|
341
337
|
min = Math.ceil(min);
|