Skip to content

Commit 528ab1f

Browse files
committed
添加分组API和二维码API
1 parent a1a8560 commit 528ab1f

File tree

2 files changed

+168
-9
lines changed

2 files changed

+168
-9
lines changed

lib/common.js

Lines changed: 110 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,17 @@ var wrapper = function (callback) {
1616
};
1717
};
1818

19+
var postJSON = function (data) {
20+
return {
21+
dataType: 'json',
22+
type: 'POST',
23+
data: data,
24+
headers: {
25+
'Content-Type': 'application/json'
26+
}
27+
};
28+
};
29+
1930
var API = function (appid, appsecret) {
2031
this.appid = appid;
2132
this.appsecret = appsecret;
@@ -48,15 +59,7 @@ API.prototype.getAccessToken = function (callback) {
4859
*/
4960
API.prototype.createMenu = function (menu, callback) {
5061
var url = this.prefix + 'menu/create?access_token=' + this.token;
51-
var args = {
52-
dataType: 'json',
53-
type: 'POST',
54-
data: menu,
55-
headers: {
56-
'Content-Type': 'application/json'
57-
}
58-
};
59-
urllib.request(url, args, wrapper(callback));
62+
urllib.request(url, postJSON(menu), wrapper(callback));
6063
};
6164

6265
/**
@@ -84,6 +87,104 @@ API.prototype.removeMenu = function (callback) {
8487
*/
8588
API.prototype.getQRCode = function (data, callback) {
8689
var url = this.prefix + 'qrcode/get?access_token=' + this.token;
90+
urllib.request(url, postJSON(data), wrapper(callback));
91+
};
92+
93+
/**
94+
* 创建临时二维码
95+
* @param {String} sceneId 场景ID
96+
* @param {Number} expire 过期时间,单位秒。最大不超过1800
97+
* @param {Function} callback 回调函数
98+
*/
99+
API.prototype.createTmpQRCode = function (sceneId, expire, callback) {
100+
var url = this.prefix + 'qrcode/create?access_token=' + this.token;
101+
var data = {
102+
"expire_seconds": expire,
103+
"action_name": "QR_SCENE",
104+
"action_info": {"scene": {"scene_id": sceneId}}
105+
};
106+
urllib.request(url, postJSON(data), wrapper(callback));
107+
};
108+
109+
/**
110+
* 创建永久二维码
111+
* @param {String} sceneId 场景ID。ID不能大于1000
112+
* @param {Function} callback 回调函数
113+
*/
114+
API.prototype.createLimitQRCode = function (sceneId, callback) {
115+
var url = this.prefix + 'qrcode/create?access_token=' + this.token;
116+
var data = {
117+
"action_name": "QR_LIMIT_SCENE",
118+
"action_info": {"scene": {"scene_id": sceneId}}
119+
};
120+
urllib.request(url, postJSON(data), wrapper(callback));
121+
};
122+
123+
/**
124+
* 生成显示二维码的链接
125+
* @param {String} ticket 二维码Ticket
126+
*/
127+
API.prototype.showQRCodeURL = function (ticket) {
128+
return this.prefix + 'showqrcode?ticket=' + ticket;
129+
};
130+
131+
// ## 分组API http://mp.weixin.qq.com/wiki/index.php?title=%E5%88%86%E7%BB%84%E7%AE%A1%E7%90%86%E6%8E%A5%E5%8F%A3
132+
/**
133+
* 获取分组列表
134+
*/
135+
API.prototype.getGroups = function (callback) {
136+
// https://api.weixin.qq.com/cgi-bin/groups/get?access_token=ACCESS_TOKEN
137+
var url = this.prefix + 'groups/get?access_token=' + this.token;
138+
urllib.request(url, {dataType: 'json'}, wrapper(callback));
139+
};
140+
141+
/**
142+
* 创建分组
143+
* @param {String} name 分组名字
144+
*/
145+
API.prototype.createGroup = function (name, callback) {
146+
// https://api.weixin.qq.com/cgi-bin/groups/create?access_token=ACCESS_TOKEN
147+
// POST数据格式:json
148+
// POST数据例子:{"group":{"name":"test"}}
149+
var url = this.prefix + 'groups/create?access_token=' + this.token;
150+
var data = {
151+
"group": {"name": name}
152+
};
153+
urllib.request(url, postJSON(data), wrapper(callback));
154+
};
155+
156+
/**
157+
* 更新分组名字
158+
* @param {Number} id 分组ID
159+
* @param {String} name 新的分组名字
160+
*/
161+
API.prototype.updateGroup = function (id, name, callback) {
162+
// http请求方式: POST(请使用https协议)
163+
// https://api.weixin.qq.com/cgi-bin/groups/update?access_token=ACCESS_TOKEN
164+
// POST数据格式:json
165+
// POST数据例子:{"group":{"id":108,"name":"test2_modify2"}}
166+
var url = this.prefix + 'groups/update?access_token=' + this.token;
167+
var data = {
168+
"group": {"id": id, "name": name}
169+
};
170+
urllib.request(url, postJSON(data), wrapper(callback));
171+
};
172+
173+
/**
174+
* 移动用户进分组
175+
* @param {String} openid 用户的openid
176+
* @param {Number} groupId 分组ID
177+
*/
178+
API.prototype.moveUserToGroup = function (openid, groupId, callback) {
179+
// http请求方式: POST(请使用https协议)
180+
// https://api.weixin.qq.com/cgi-bin/groups/members/update?access_token=ACCESS_TOKEN
181+
// POST数据格式:json
182+
// POST数据例子:{"openid":"oDF3iYx0ro3_7jD4HFRDfrjdCM58","to_groupid":108}
183+
var url = this.prefix + 'groups/update?access_token=' + this.token;
184+
var data = {
185+
"openid": openid,
186+
"to_groupid": groupId
187+
};
87188
var args = {
88189
dataType: 'json',
89190
type: 'POST',

test/common.test.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,64 @@ describe('common.js', function () {
106106
done();
107107
});
108108
});
109+
110+
it('createTmpQRCode should not ok', function (done) {
111+
api.createTmpQRCode(123, 1800, function (err, data, res) {
112+
should.exist(err);
113+
err.name.should.be.equal('WeChatAPIError');
114+
err.message.should.be.equal('invalid credential');
115+
done();
116+
});
117+
});
118+
119+
it('createLimitQRCode should not ok', function (done) {
120+
api.createLimitQRCode(123, function (err, data, res) {
121+
should.exist(err);
122+
err.name.should.be.equal('WeChatAPIError');
123+
err.message.should.be.equal('invalid credential');
124+
done();
125+
});
126+
});
127+
128+
it('showQRCodeURL should not ok', function () {
129+
api.showQRCodeURL('ticket').should.be.equal('https://api.weixin.qq.com/cgi-bin/showqrcode?ticket=ticket');
130+
});
131+
132+
it('getGroups should not ok', function (done) {
133+
api.getGroups(function (err, data, res) {
134+
should.exist(err);
135+
err.name.should.be.equal('WeChatAPIError');
136+
err.message.should.be.equal('invalid credential');
137+
done();
138+
});
139+
});
140+
141+
it('createGroup should not ok', function (done) {
142+
api.createGroup('new group', function (err, data, res) {
143+
should.exist(err);
144+
err.name.should.be.equal('WeChatAPIError');
145+
err.message.should.be.equal('invalid credential');
146+
done();
147+
});
148+
});
149+
150+
it('updateGroup should not ok', function (done) {
151+
api.updateGroup(123, 'new group', function (err, data, res) {
152+
should.exist(err);
153+
err.name.should.be.equal('WeChatAPIError');
154+
err.message.should.be.equal('invalid credential');
155+
done();
156+
});
157+
});
158+
159+
it('moveUserToGroup should not ok', function (done) {
160+
api.moveUserToGroup('openid', 123, function (err, data, res) {
161+
should.exist(err);
162+
err.name.should.be.equal('WeChatAPIError');
163+
err.message.should.be.equal('invalid credential');
164+
done();
165+
});
166+
});
109167
});
110168

111169
describe('mock', function () {

0 commit comments

Comments
 (0)