back-helper.js
1.05 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
/**
* Created by TaoHuang on 2016/6/27.
*/
'use strict';
/**
* 签名算法参考微信支付加密算法
* 参考链接:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=4_3
*
*/
const _ = require('lodash');
const SALT = '_+@#$%^';
/**
* 加密手机的数据,生成加密token
* @param data json
* @returns string
*/
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();
};
/**
* 返回一个 md5加密后的字符串
* @param data json
*/
const makeToken = (data) => {
let saltData = Object.assign({}, data, {key: SALT});
let str = _packageObject(saltData);
return _encodeMD5(str);
};
/**
* 验证 token 是否一致
*/
const validateToken = (data, token) => {
let saltData = Object.assign({}, data, {key: SALT});
let str = _packageObject(saltData);
return _encodeMD5(str) === token;
};
module.exports = {
makeToken,
validateToken
};