-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsignature.js
45 lines (44 loc) · 2.03 KB
/
signature.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
var request = require('request'),
//cache = require('memory-cache'),
sha1 = require('sha1'),
config = require('./wechatConfig');
exports.sign = function (url, callback) {
console.log("sign")
var noncestr = config.noncestr,
timestamp = Math.floor(Date.now()/1000), //精确到秒
jsapi_ticket;
//if(cache.get('ticket')){
if (false) {
// jsapi_ticket = cache.get('ticket');
// console.log('1' + 'jsapi_ticket=' + jsapi_ticket + '&noncestr=' + noncestr + '×tamp=' + timestamp + '&url=' + url);
// callback({
// noncestr:noncestr,
// timestamp:timestamp,
// url:url,
// jsapi_ticket:jsapi_ticket,
// signature:sha1('jsapi_ticket=' + jsapi_ticket + '&noncestr=' + noncestr + '×tamp=' + timestamp + '&url=' + url)
// });
} else{
request(config.accessTokenUrl + '?grant_type=' + config.grant_type + '&appid=' + config.appid + '&secret=' + config.secret ,function(error, response, body){
if (!error && response.statusCode == 200) {
var tokenMap = JSON.parse(body);
request(config.ticketUrl + '?access_token=' + tokenMap.access_token + '&type=jsapi', function(error, resp, json){
if (!error && response.statusCode == 200) {
var ticketMap = JSON.parse(json);
//cache.put('ticket',ticketMap.ticket,config.cache_duration); //加入缓存
console.log('jsapi_ticket=' + ticketMap.ticket + '&noncestr=' + noncestr + '×tamp=' + timestamp + '&url=' + url);
callback({
noncestr:noncestr,
timestamp:timestamp,
url:url,
jsapi_ticket:ticketMap.ticket,
signature:sha1('jsapi_ticket=' + ticketMap.ticket + '&noncestr=' + noncestr + '×tamp=' + timestamp + '&url=' + url)
});
}
})
} else {
console.error("request error");
}
})
}
}