util.ts
1.54 KB
1
2
3
4
5
6
7
8
9
10
11
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
/*
* @Description:
* @Author: huyue
* @Date: 2022-10-10 10:54:14
* @LastEditors: huyue
* @LastEditTime: 2024-07-29 10:07:19
*/
import sha1 from "sha1";
import qs from "qs";
import md5 from "js-md5";
export const Util = {
createSign: function (
params: object,
secret: string,
noSignAttr = [],
way = "sha1"
) {
delete params.sign;
params.timestamp = new Date().getTime();
var strQs = qs.stringify(params, {
arrayFormat: "indices",
allowDots: true,
encodeValuesOnly: true,
});
const objQs = strQs.split("&");
const collect = objQs.reduce((collect: any, item) => {
let [key, value] = item.split("=");
if (value === null) {
value = "";
} else if (typeof value === "object") {
value = JSON.stringify(value);
}
collect[key] = decodeURIComponent(value);
return collect;
}, {});
const keys = Object.keys(collect).sort();
const str = keys.reduce((col, key) => {
if (!noSignAttr.includes(key)) {
col.push(key, collect[key]);
}
return col;
}, []);
collect.sign = { sha1, md5 }
[way](secret + str.join("") + secret)
.toUpperCase();
return collect;
},
rgb: function () {
//rgb颜色随机
const r = Math.floor(Math.random() * 256);
const g = Math.floor(Math.random() * 256);
const b = Math.floor(Math.random() * 256);
return `rgb(${r},${g},${b})`;
},
};
export const createSign = Util.createSign;
export default Util;