1 | 'use strict';
|
2 |
|
3 |
|
4 | var urllib = require('urllib');
|
5 | var util = require('./util');
|
6 | var extend = require('util')._extend;
|
7 | var wrapper = util.wrapper;
|
8 |
|
9 | var AccessToken = function (accessToken, expireTime) {
|
10 | if (!(this instanceof AccessToken)) {
|
11 | return new AccessToken(accessToken, expireTime);
|
12 | }
|
13 | this.accessToken = accessToken;
|
14 | this.expireTime = expireTime;
|
15 | };
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 | AccessToken.prototype.isValid = function () {
|
26 | return !!this.accessToken && (new Date().getTime()) < this.expireTime;
|
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 | var API = function (appid, appsecret, getToken, saveToken) {
|
67 | this.appid = appid;
|
68 | this.appsecret = appsecret;
|
69 | this.getToken = getToken || function (callback) {
|
70 | callback(null, this.store);
|
71 | };
|
72 | this.saveToken = saveToken || function (token, callback) {
|
73 | this.store = token;
|
74 | if (process.env.NODE_ENV === 'production') {
|
75 | console.warn('Don\'t save token in memory, when cluster or multi-computer!');
|
76 | }
|
77 | callback(null);
|
78 | };
|
79 | this.endpoint = 'https://api.weixin.qq.com';
|
80 | this.mpPrefix = 'https://mp.weixin.qq.com/cgi-bin/';
|
81 | this.fileServerPrefix = 'http://file.api.weixin.qq.com/cgi-bin/';
|
82 | this.defaults = {};
|
83 |
|
84 | this.registerTicketHandle();
|
85 | };
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 | API.prototype.setEndpoint = function (domain) {
|
102 | this.endpoint = 'https://' + domain;
|
103 | };
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 | API.prototype.setOpts = function (opts) {
|
115 | this.defaults = opts;
|
116 | };
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 | API.prototype.request = function (url, opts, callback) {
|
130 | var options = {};
|
131 | extend(options, this.defaults);
|
132 | if (typeof opts === 'function') {
|
133 | callback = opts;
|
134 | opts = {};
|
135 | }
|
136 | for (var key in opts) {
|
137 | if (key !== 'headers') {
|
138 | options[key] = opts[key];
|
139 | } else {
|
140 | if (opts.headers) {
|
141 | options.headers = options.headers || {};
|
142 | extend(options.headers, opts.headers);
|
143 | }
|
144 | }
|
145 | }
|
146 | urllib.request(url, options, callback);
|
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 | API.prototype.getAccessToken = function (callback) {
|
172 | var that = this;
|
173 | var url = this.endpoint + '/cgi-bin/token?grant_type=client_credential&appid=' + this.appid + '&secret=' + this.appsecret;
|
174 | this.request(url, {dataType: 'json'}, wrapper(function (err, data) {
|
175 | if (err) {
|
176 | return callback(err);
|
177 | }
|
178 |
|
179 | var expireTime = (new Date().getTime()) + (data.expires_in - 10) * 1000;
|
180 | var token = AccessToken(data.access_token, expireTime);
|
181 | that.saveToken(token, function (err) {
|
182 | if (err) {
|
183 | return callback(err);
|
184 | }
|
185 | callback(err, token);
|
186 | });
|
187 | }));
|
188 | return this;
|
189 | };
|
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 |
|
198 |
|
199 |
|
200 |
|
201 |
|
202 |
|
203 | API.prototype.preRequest = function (method, args, retryed) {
|
204 | var that = this;
|
205 | var callback = args[args.length - 1];
|
206 |
|
207 | that.getToken(function (err, token) {
|
208 | if (err) {
|
209 | return callback(err);
|
210 | }
|
211 | var accessToken;
|
212 |
|
213 | if (token && (accessToken = AccessToken(token.accessToken, token.expireTime)).isValid()) {
|
214 |
|
215 | that.token = accessToken;
|
216 | if (!retryed) {
|
217 | var retryHandle = function (err, data, res) {
|
218 |
|
219 | if (data && data.errcode && data.errcode === 40001) {
|
220 | return that.preRequest(method, args, true);
|
221 | }
|
222 | callback(err, data, res);
|
223 | };
|
224 |
|
225 | var newargs = Array.prototype.slice.call(args, 0, -1);
|
226 | newargs.push(retryHandle);
|
227 | method.apply(that, newargs);
|
228 | } else {
|
229 | method.apply(that, args);
|
230 | }
|
231 | } else {
|
232 |
|
233 | that.getAccessToken(function (err, token) {
|
234 |
|
235 | if (err) {
|
236 | return callback(err);
|
237 | }
|
238 |
|
239 | that.token = token;
|
240 | method.apply(that, args);
|
241 | });
|
242 | }
|
243 | });
|
244 | };
|
245 |
|
246 |
|
247 |
|
248 |
|
249 |
|
250 |
|
251 |
|
252 |
|
253 |
|
254 |
|
255 |
|
256 |
|
257 |
|
258 |
|
259 |
|
260 |
|
261 | API.prototype.getLatestToken = function (callback) {
|
262 | var that = this;
|
263 |
|
264 | that.getToken(function (err, token) {
|
265 | if (err) {
|
266 | return callback(err);
|
267 | }
|
268 | var accessToken;
|
269 |
|
270 | if (token && (accessToken = AccessToken(token.accessToken, token.expireTime)).isValid()) {
|
271 | return callback(null, accessToken);
|
272 | }
|
273 |
|
274 | that.getAccessToken(callback);
|
275 | });
|
276 | };
|
277 |
|
278 |
|
279 |
|
280 |
|
281 |
|
282 |
|
283 |
|
284 |
|
285 |
|
286 |
|
287 | API.mixin = function (obj) {
|
288 | for (var key in obj) {
|
289 | if (API.prototype.hasOwnProperty(key)) {
|
290 | throw new Error('Don\'t allow override existed prototype method. method: '+ key);
|
291 | }
|
292 | API.prototype[key] = obj[key];
|
293 | }
|
294 | };
|
295 |
|
296 |
|
297 |
|
298 |
|
299 |
|
300 |
|
301 |
|
302 |
|
303 |
|
304 |
|
305 |
|
306 |
|
307 |
|
308 |
|
309 |
|
310 |
|
311 |
|
312 |
|
313 |
|
314 | API.patch = function (functionName, apiUrl, override) {
|
315 | if (typeof apiUrl !== 'string') {
|
316 | throw new Error('The second argument expect a type of string as the request url of wechat');
|
317 | }
|
318 |
|
319 | if (typeof functionName !== 'string') {
|
320 | throw new Error('The first argument expect a type of string as the name of new function');
|
321 | }
|
322 |
|
323 | if (API.prototype[functionName] || API.prototype['_' + functionName] ) {
|
324 | if (override !== true) {
|
325 | throw new Error('wechat-api already has a prototype named ['+ functionName + '], use "true" as third param to override it or change your new function name.');
|
326 | } else {
|
327 | console.warn('wechat-api already has a prototype named ['+ functionName + '], will override the orignal one.');
|
328 | }
|
329 | }
|
330 |
|
331 | util.make(API.prototype, functionName, function (info, callback) {
|
332 | var hasMark = apiUrl.indexOf('?') >= 0;
|
333 | var url = apiUrl + (hasMark ? '&access_token=': '?access_token=') + this.token.accessToken;
|
334 | this.request(url, util.postJSON(info), wrapper(callback));
|
335 | });
|
336 | };
|
337 |
|
338 | API.AccessToken = AccessToken;
|
339 |
|
340 | module.exports = API;
|