-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutil.js
51 lines (41 loc) · 1.09 KB
/
util.js
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
var jssha = require('jssha');
//签名
exports.getSign = function (json) {
const tmpArr = [];
const shaObj = new jssha('SHA-1', 'TEXT');
let str, i;
for (i in json) {
tmpArr.push(i + '=' + json[i]);
}
str = tmpArr.join('&');
shaObj.update(str);
return shaObj.getHash('HEX');
// json.signature = shaObj.getHash('HEX');
// return json;
};
exports.createNonceStr = function () {
return Math.random().toString(36).substr(2, 15);
};
exports.createTimestamp = function () {
return parseInt(new Date().getTime() / 1000) + '';
};
//json转xml
exports.json2xml = function (json) {
var tmpArr = ['<xml>'];
for (var i in json) {
tmpArr.push('<' + i + '>' + json[i] + '</' + i + '>');
}
tmpArr.push('</xml>');
return tmpArr.join('');
};
var json2string = function (json) {
var tmpArr = [];
for (var i in json) {
tmpArr.push(i + '=' + encodeURIComponent(json[i]));
}
return tmpArr.join('&');
}
//生成随机串
exports.getNonceStr = function () {
return Math.floor(Math.pow(10, 12) * Math.random()) + '';
};