UNPKG

5.23 kBJavaScriptView Raw
1'use strict';
2
3var util = require('./util');
4var wrapper = util.wrapper;
5var postJSON = util.postJSON;
6var make = util.make;
7
8/**
9 * 获取分组列表
10 * 详情请见:<http://mp.weixin.qq.com/wiki/0/56d992c605a97245eb7e617854b169fc.html>
11 * Examples:
12 * ```
13 * api.getGroups(callback);
14 * ```
15 * Callback:
16 *
17 * - `err`, 调用失败时得到的异常
18 * - `result`, 调用正常时得到的对象
19 *
20 * Result:
21 * ```
22 * {
23 * "groups": [
24 * {"id": 0, "name": "未分组", "count": 72596},
25 * {"id": 1, "name": "黑名单", "count": 36}
26 * ]
27 * }
28 * ```
29 * @param {Function} callback 回调函数
30 */
31make(exports, 'getGroups', function (callback) {
32 // https://api.weixin.qq.com/cgi-bin/groups/get?access_token=ACCESS_TOKEN
33 var url = this.endpoint + '/cgi-bin/groups/get?access_token=' + this.token.accessToken;
34 this.request(url, {dataType: 'json'}, wrapper(callback));
35});
36
37/**
38 * 查询用户在哪个分组
39 * 详情请见:<http://mp.weixin.qq.com/wiki/0/56d992c605a97245eb7e617854b169fc.html>
40 * Examples:
41 * ```
42 * api.getWhichGroup(openid, callback);
43 * ```
44 * Callback:
45 *
46 * - `err`, 调用失败时得到的异常
47 * - `result`, 调用正常时得到的对象
48 *
49 * Result:
50 * ```
51 * {
52 * "groupid": 102
53 * }
54 * ```
55 * @param {String} openid Open ID
56 * @param {Function} callback 回调函数
57 */
58make(exports, 'getWhichGroup', function (openid, callback) {
59 // https://api.weixin.qq.com/cgi-bin/groups/getid?access_token=ACCESS_TOKEN
60 var url = this.endpoint + '/cgi-bin/groups/getid?access_token=' + this.token.accessToken;
61 var data = {
62 'openid': openid
63 };
64 this.request(url, postJSON(data), wrapper(callback));
65});
66
67/**
68 * 创建分组
69 * 详情请见:<http://mp.weixin.qq.com/wiki/0/56d992c605a97245eb7e617854b169fc.html>
70 * Examples:
71 * ```
72 * api.createGroup('groupname', callback);
73 * ```
74 * Callback:
75 *
76 * - `err`, 调用失败时得到的异常
77 * - `result`, 调用正常时得到的对象
78 *
79 * Result:
80 * ```
81 * {"group": {"id": 107, "name": "test"}}
82 * ```
83 * @param {String} name 分组名字
84 * @param {Function} callback 回调函数
85 */
86make(exports, 'createGroup', function (name, callback) {
87 // https://api.weixin.qq.com/cgi-bin/groups/create?access_token=ACCESS_TOKEN
88 // POST数据格式:json
89 // POST数据例子:{"group":{"name":"test"}}
90 var url = this.endpoint + '/cgi-bin/groups/create?access_token=' + this.token.accessToken;
91 var data = {
92 'group': {'name': name}
93 };
94 this.request(url, postJSON(data), wrapper(callback));
95});
96
97/**
98 * 更新分组名字
99 * 详情请见:<http://mp.weixin.qq.com/wiki/0/56d992c605a97245eb7e617854b169fc.html>
100 * Examples:
101 * ```
102 * api.updateGroup(107, 'new groupname', callback);
103 * ```
104 * Callback:
105 *
106 * - `err`, 调用失败时得到的异常
107 * - `result`, 调用正常时得到的对象
108 *
109 * Result:
110 * ```
111 * {"errcode": 0, "errmsg": "ok"}
112 * ```
113 * @param {Number} id 分组ID
114 * @param {String} name 新的分组名字
115 * @param {Function} callback 回调函数
116 */
117make(exports, 'updateGroup', function (id, name, callback) {
118 // http请求方式: POST(请使用https协议)
119 // https://api.weixin.qq.com/cgi-bin/groups/update?access_token=ACCESS_TOKEN
120 // POST数据格式:json
121 // POST数据例子:{"group":{"id":108,"name":"test2_modify2"}}
122 var url = this.endpoint + '/cgi-bin/groups/update?access_token=' + this.token.accessToken;
123 var data = {
124 'group': {'id': id, 'name': name}
125 };
126 this.request(url, postJSON(data), wrapper(callback));
127});
128
129/**
130 * 移动用户进分组
131 * 详情请见:<http://mp.weixin.qq.com/wiki/0/56d992c605a97245eb7e617854b169fc.html>
132 * Examples:
133 * ```
134 * api.moveUserToGroup(openid, groupId, callback);
135 * ```
136 * Callback:
137 *
138 * - `err`, 调用失败时得到的异常
139 * - `result`, 调用正常时得到的对象
140 *
141 * Result:
142 * ```
143 * {"errcode": 0, "errmsg": "ok"}
144 * ```
145 * @param {String} openid 用户的openid
146 * @param {Number} groupId 分组ID
147 * @param {Function} callback 回调函数
148 */
149make(exports, 'moveUserToGroup', function (openid, groupId, callback) {
150 // http请求方式: POST(请使用https协议)
151 // https://api.weixin.qq.com/cgi-bin/groups/members/update?access_token=ACCESS_TOKEN
152 // POST数据格式:json
153 // POST数据例子:{"openid":"oDF3iYx0ro3_7jD4HFRDfrjdCM58","to_groupid":108}
154 var url = this.endpoint + '/cgi-bin/groups/members/update?access_token=' + this.token.accessToken;
155 var data = {
156 'openid': openid,
157 'to_groupid': groupId
158 };
159 this.request(url, postJSON(data), wrapper(callback));
160});
161
162/**
163 * 删除分组
164 * 详情请见:<http://mp.weixin.qq.com/wiki/0/56d992c605a97245eb7e617854b169fc.html>
165 * Examples:
166 * ```
167 * api.removeGroup(groupId, callback);
168 * ```
169 * Callback:
170 *
171 * - `err`, 调用失败时得到的异常
172 * - `result`, 调用正常时得到的对象
173 *
174 * Result:
175 * ```
176 * {"errcode": 0, "errmsg": "ok"}
177 * ```
178 * @param {Number} groupId 分组ID
179 * @param {Function} callback 回调函数
180 */
181make(exports, 'removeGroup', function (groupId, callback) {
182 var url = this.endpoint + '/cgi-bin/groups/delete?access_token=' + this.token.accessToken;
183 var data = {
184 'group': { id: groupId}
185 };
186 this.request(url, postJSON(data), wrapper(callback));
187});