back-helper.js
945 Bytes
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
/**
* Created by TaoHuang on 2016/6/27.
*/
'use strict';
/**
* 签名算法参考微信支付加密算法
* 参考链接:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=4_3
*
*/
const SALT = "_+@#$%^";
/**
* 生成加密token
* @param data json
* @returns string
*/
const makeToken = (data) => {
let saltData = Object.assign(data, {key: this.SALT});
let str = this._packageObject(saltData);
return this._encodeMD5(str);
};
const validateToken = (data, token) => {
let saltData = Object.assign(data, {key: this.SALT});
let str = this._packageObject(saltData);
return this._encodeMD5(str) === token;
};
const _packageObject = (data) => {
return _.keys(data).sort().map(key => `${key}=${data[key]}`).join('&').toUpperCase();
};
const _encodeMD5 = (str) => {
const md5 = require('md5');
return md5(str).toUpperCase();
};
module.exports = {
makeToken,
validateToken
};