wechat.js
5.76 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/**
* 微信公众号签名获取
* @author: qiujun <jun.qiu@yoho.cn>
* @date: 14/11/2017
*/
const config = global.yoho.config;
const crypto = require('crypto');
const request = require('request-promise');
const WechatModel = require('../models/wechat');
const moment = require('moment');
const sha1 = (str) => {
const generator = crypto.createHash('sha1');
generator.update(str);
return generator.digest('hex');
};
// 微信 JS 接口签名校验工具 http://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=jsapisign
let getAccessToken = function(req, next, appId, secret, prefix) { // 获取access_token
return request({
url: prefix + 'token',
qs: {
grant_type: 'client_credential',
appid: appId,
secret: secret
},
json: true
}).then(res => {
return res;
}).catch(next);
};
let getTicket = function(req, accessToken, next, appId, secret, prefix) { // 获取ticket
return request({
url: prefix + 'ticket/getticket',
qs: {
access_token: accessToken,
type: 'jsapi'
},
json: true
}).then(res => {
return res;
});
};
let calcSigature = function(ticket_res, res) { // 计算signature
let appID = ticket_res.appID;
let ticket = ticket_res.access_ticket;
let nonceStr = Math.random().toString(36).substr(2, 15);
let timestamp = Math.floor(Date.now() / 1000) + '';
let url = ticket_res.url;
let str = `jsapi_ticket=${ticket}&noncestr=${nonceStr}×tamp=${timestamp}&url=${url}`;
let signature = sha1(str);
let result = {
appId: appID,
nonceStr,
timestamp,
url,
signature
};
return res.jsonp(result);
};
let getResult = function(wechat_res, req, res, next) { // 向微信接口获取access_token及ticket并计算signature
let appID = wechat_res.appID;
let appSecret = wechat_res.appSecret;
let prefix = wechat_res.prefix;
let mpPrefix = wechat_res.mpPrefix;
let url = wechat_res.url;
let type = wechat_res.type;
return getAccessToken(req, next, appID, appSecret, prefix)
.then(result => { // 先获取accessToken
let access_token = result.access_token;
return getTicket(req, access_token, next, appID, appSecret, mpPrefix)
.then(ticket_res => { // 通过accessToken获取ticket
if (ticket_res && ticket_res.ticket) {
let access_ticket = ticket_res.ticket;
let expired = parseInt(ticket_res.expires_in, 10) - 100;
let create_time = moment().format('YYYY-MM-DD HH:mm:ss');
let expired_time = moment().add(expired, 'seconds').format('YYYY-MM-DD HH:mm:ss');
req.ctx(WechatModel).
saveAccessKeys(access_token, access_ticket, create_time, expired_time, type)
.then(() => {
let access_res = {
appID: appID,
access_ticket: access_ticket,
url: url
};
calcSigature(access_res, res);
});
} else {
res.jsonp({
appID: appID,
nonceStr: '',
timestamp: '',
url: url,
signature: 'get failed'
});
}
}).catch(next);
});
};
const wechatShare = {
getSignPackage(req, res, next) {
const appID = config.wechat.appID;
const appSecret = config.wechat.appSecret;
const prefix = config.wechat.prefix;
const mpPrefix = config.wechat.mpPrefix;
const url = req.query.pageurl || 'http://www.yoho.cn';
req.ctx(WechatModel).getAccessKeys()
.then(keys_res => { // 先查询数据库中是否有access_token并且未过期
if (keys_res.length > 0) {
let access_token = keys_res[0].access_token;
let access_ticket = keys_res[0].access_ticket;
let expired = new Date(keys_res[0].access_expired).getTime();
let now = Date.now();
if (now > expired) { // 过期则去 取新的token与ticket 并且更新数据库
return Promise.reject({
type: 'update'
});
} else {
return Promise.resolve({ // 没过期则直接返回数据库中保存的access_ticket
access_token,
access_ticket
});
}
} else {
return Promise.reject({ // 数据库中没有token数据则新增
type: 'save'
});
}
})
.then(access_res => {
access_res = Object.assign({
appID,
url
}, access_res);
calcSigature(access_res, res); // 组合参数, 计算signature
},
fail_res => {
fail_res = Object.assign({
appID: appID,
appSecret: appSecret,
prefix: prefix,
mpPrefix: mpPrefix,
url: url
}, fail_res);
getResult(fail_res, req, res, next); // 向微信接口获取access_token及ticket并计算signature
});
}
};
module.exports = wechatShare;