-
Notifications
You must be signed in to change notification settings - Fork 1
/
weixin.js
149 lines (122 loc) · 3.13 KB
/
weixin.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
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
'use strict';
/**
* Module dependencies.
*/
const Emitter = require('events');
const Url = require('url');
const request = require('co-request');
const debug = require('debug')('weixin:token');
const defaultExpires = 7200; // default 7200 sec
const beforeExpires = 100; // before expires 100sec update token
class Weixin extends Emitter {
/**
* Initialize
*/
constructor(opts) {
super();
opts = Object.assign({
'intval': 20, // retry time 20sec
}, opts);
debug('weixin.options', opts);
this.appid = opts.appid || '';
this.secret = opts.secret || '';
this.intval = opts.intval;
this._timer = null;
this.updateToken();
}
_parseUrl(url, query) {
return Url.format( Object.assign(Url.parse(url), {'query': query}) );
}
/**
* TODO: check signature
*
* @return {Boolean}
*/
checkSignature() {
return true;
}
updateToken() {
var context = this;
var data = {
'grant_type': 'client_credential',
'appid': this.appid,
'secret': this.secret
};
var setTimer = (() => {
// error retry in 20s
context._timer = setTimeout(() => {
context.updateToken();
}, context.intval * 1000);
});
var url = context._parseUrl('https://api.weixin.qq.com/cgi-bin/token', data);
return request({
method: 'GET',
url: url
}).then((response) => {
var ret = JSON.parse(response.body);
if( response.body && response.body.length && !ret.errcode ) {
debug('updateToken', ret);
context._setToken(ret);
}else{
setTimer()
}
}, (err) => {
setTimer()
});
}
_setToken(data) {
data = data || {};
var context = this;
var token = data.access_token || '';
var expires = data.expires_in || defaultExpires;
var timer = (expires - beforeExpires) * 1000;
context.token = token;
context.expires = Date.now() + (expires * 1000);
context.emit('token', token, expires);
// update token before token expire
context._timer = setTimeout(() => {
context.updateToken()
}, timer);
context.updateTicket();
}
getToken() {
return this.token;
}
getTicket() {
return this.ticket;
}
updateTicket() {
var context = this;
var data = {
'access_token': context.getToken(),
'type': 'jsapi'
};
var setTimer = (() => {
// error retry in 20s
context._jsapiTimer = setTimeout(() => {
context.updateTicket()
}, context.intval * 1000);
});
var url = context._parseUrl('https://api.weixin.qq.com/cgi-bin/ticket/getticket', data);
return request({
method: 'GET',
url: url
}).then((response) => {
var ret = JSON.parse(response.body);
var token;
var expires;
if( response.body && response.body.length && !ret.errcode ) {
debug('updateTicket', ret);
token = ret.ticket || '';
expires = ret.expires_in || defaultExpires;
context.ticket = token;
context.emit('ticket', token, expires);
}else{
setTimer()
}
}, (err) => {
setTimer()
});
}
};
module.exports = Weixin;