Skip to content

Commit 326ed71

Browse files
committed
分拆模块
1 parent 93fefb5 commit 326ed71

16 files changed

+1232
-1169
lines changed

Makefile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ test:
1010
$(TESTS)
1111

1212
test-cov:
13-
@$(MAKE) test REPORTER=dot
1413
@$(MAKE) test MOCHA_OPTS='--require blanket' REPORTER=html-cov > coverage.html
1514
@$(MAKE) test MOCHA_OPTS='--require blanket' REPORTER=travis-cov
1615

index.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
var wechat = require('./lib/wechat');
22
wechat.List = require('./lib/list');
3-
wechat.API = require('./lib/common');
3+
var API = require('./lib/api_common');
4+
// 菜单接口
5+
API.mixin(require('./lib/api_menu'));
6+
// 分组管理
7+
API.mixin(require('./lib/api_group'));
8+
// 用户信息
9+
API.mixin(require('./lib/api_user'));
10+
// 二维码
11+
API.mixin(require('./lib/api_qrcode'));
12+
// 客服消息
13+
API.mixin(require('./lib/api_customer'));
14+
// 媒体管理(上传、下载)
15+
API.mixin(require('./lib/api_media'));
16+
// 支付接口
17+
API.mixin(require('./lib/api_pay'));
18+
wechat.API = API;
419
wechat.OAuth = require('./lib/oauth');
20+
wechat.util = require('./lib/util');
521
module.exports = wechat;

lib/api_common.js

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
var inherits = require('util').inherits;
2+
var EventEmitter = require('events').EventEmitter;
3+
var urllib = require('urllib');
4+
var util = require('./util');
5+
var wrapper = util.wrapper;
6+
7+
var AccessToken = function (accessToken, expireTime) {
8+
if (!(this instanceof AccessToken)) {
9+
return new AccessToken(accessToken, expireTime);
10+
}
11+
this.accessToken = accessToken;
12+
this.expireTime = expireTime;
13+
};
14+
15+
/**
16+
* 检查AccessToken是否有效,检查规则为当前时间和过期时间进行对比
17+
*
18+
* Examples:
19+
* ```
20+
* api.isAccessTokenValid();
21+
* ```
22+
*/
23+
AccessToken.prototype.isValid = function () {
24+
return !!this.accessToken && (new Date().getTime()) < this.expireTime;
25+
};
26+
27+
/**
28+
* 根据appid和appsecret创建API的构造函数
29+
*
30+
* Examples:
31+
* ```
32+
* var API = require('wechat').API;
33+
* var api = new API('appid', 'secret');
34+
* api.on('token', function (token) {
35+
* // 请将token存储到全局,跨进程级别的全局
36+
* // 比如写到数据库、redis等
37+
* // 这样才能在cluster模式下使用
38+
* });
39+
* // 或者
40+
* getTokenFromGlobal(function (token) {
41+
* // var api = new API('appid', 'secret', token);
42+
* });
43+
* ```
44+
* @param {String} appid 在公众平台上申请得到的appid
45+
* @param {String} appsecret 在公众平台上申请得到的app secret
46+
* @param {Object} token 可选的。全局token对象,该对象在调用一次`getAccessToken`后得到
47+
*/
48+
var API = function (appid, appsecret, token) {
49+
this.appid = appid;
50+
this.appsecret = appsecret;
51+
if (token) {
52+
this.token = AccessToken(token.accessToken, token.expireTime);
53+
}
54+
this.prefix = 'https://api.weixin.qq.com/cgi-bin/';
55+
this.mpPrefix = 'https://mp.weixin.qq.com/cgi-bin/';
56+
this.fileServerPrefix = 'http://file.api.weixin.qq.com/cgi-bin/';
57+
this.payPrefix = 'https://api.weixin.qq.com/pay/';
58+
EventEmitter.call(this);
59+
};
60+
inherits(API, EventEmitter);
61+
62+
/**
63+
* 根据创建API时传入的appid和appsecret获取access token
64+
* 进行后续所有API调用时,需要先获取access token
65+
* 详细请看:<http://mp.weixin.qq.com/wiki/index.php?title=获取access_token>
66+
*
67+
* Examples:
68+
* ```
69+
* api.getAccessToken(callback);
70+
* ```
71+
* Callback:
72+
*
73+
* - `err`, 获取access token出现异常时的异常对象
74+
* - `result`, 成功时得到的响应结果
75+
*
76+
* Result:
77+
* ```
78+
* {"access_token": "ACCESS_TOKEN","expires_in": 7200}
79+
* ```
80+
* @param {Function} callback 回调函数
81+
*/
82+
API.prototype.getAccessToken = function (callback) {
83+
var that = this;
84+
var url = this.prefix + 'token?grant_type=client_credential&appid=' + this.appid + '&secret=' + this.appsecret;
85+
urllib.request(url, {dataType: 'json'}, wrapper(function (err, data) {
86+
if (err) {
87+
return callback(err);
88+
}
89+
// 过期时间,因网络延迟等,将实际过期时间提前10秒,以防止临界点
90+
var expireTime = (new Date().getTime()) + (data.expires_in - 10) * 1000;
91+
that.token = AccessToken(data.access_token, expireTime);
92+
that.emit('token', that.token);
93+
callback(err, that.token);
94+
}));
95+
return this;
96+
};
97+
98+
/**
99+
* 需要access token的接口调用如果采用preRequest进行封装后,就可以直接调用
100+
* 无需依赖getAccessToken为前置调用
101+
*
102+
* @param {Function} method 需要封装的方法
103+
* @param {Array} args 方法需要的参数
104+
*/
105+
API.prototype.preRequest = function (method, args) {
106+
var that = this;
107+
var callback = args[args.length - 1];
108+
if (that.token && that.token.isValid()) {
109+
method.apply(that, args);
110+
} else {
111+
that.getAccessToken(function (err, data) {
112+
// 如遇错误,通过回调函数传出
113+
if (err) {
114+
callback(err, data);
115+
return;
116+
}
117+
method.apply(that, args);
118+
});
119+
}
120+
};
121+
122+
/**
123+
* 用于支持对象合并。将对象合并到API.prototype上,使得能够支持扩展
124+
* Examples:
125+
* ```
126+
* // 媒体管理(上传、下载)
127+
* API.mixin(require('./lib/api_media'));
128+
* ```
129+
* @param {Object} obj 要合并的对象
130+
*/
131+
API.mixin = function (obj) {
132+
for (var key in obj) {
133+
if (obj.hasOwnProperty(key)) {
134+
if (API.prototype.hasOwnProperty(key)) {
135+
throw new Error('Don\'t allow override existed prototype method.');
136+
}
137+
API.prototype[key] = obj[key];
138+
}
139+
}
140+
};
141+
142+
module.exports = API;

0 commit comments

Comments
 (0)