UNPKG

5.56 kBJavaScriptView Raw
1'use strict';
2
3var util = require('./util');
4var wrapper = util.wrapper;
5var postJSON = util.postJSON;
6
7/**
8 * 获取用户基本信息。可以设置lang,其中zh_CN 简体,zh_TW 繁体,en 英语。默认为en
9 * 详情请见:<http://mp.weixin.qq.com/wiki/14/bb5031008f1494a59c6f71fa0f319c66.html>
10 * Examples:
11 * ```
12 * api.getUser(openid, callback);
13 * api.getUser({openid: 'openid', lang: 'en'}, callback);
14 * ```
15 * Callback:
16 *
17 * - `err`, 调用失败时得到的异常
18 * - `result`, 调用正常时得到的对象
19 *
20 * Result:
21 * ```
22 * {
23 * "subscribe": 1,
24 * "openid": "o6_bmjrPTlm6_2sgVt7hMZOPfL2M",
25 * "nickname": "Band",
26 * "sex": 1,
27 * "language": "zh_CN",
28 * "city": "广州",
29 * "province": "广东",
30 * "country": "中国",
31 * "headimgurl": "http://wx.qlogo.cn/mmopen/g3MonUZtNHkdmzicIlibx6iaFqAc56vxLSUfpb6n5WKSYVY0ChQKkiaJSgQ1dZuTOgvLLrhJbERQQ4eMsv84eavHiaiceqxibJxCfHe/0",
32 * "subscribe_time": 1382694957
33 * }
34 * ```
35 * @param {String|Object} options 用户的openid。或者配置选项,包含openid和lang两个属性。
36 * @param {Function} callback 回调函数
37 */
38exports.getUser = function (options, callback) {
39 this.preRequest(this._getUser, arguments);
40};
41
42/*!
43 * 获取用户基本信息的未封装版本
44 */
45exports._getUser = function (options, callback) {
46 if (typeof options !== 'object') {
47 options = {
48 openid: options,
49 lang: 'en'
50 };
51 }
52 // https://api.weixin.qq.com/cgi-bin/user/info?access_token=ACCESS_TOKEN&openid=OPENID
53 var url = this.endpoint + '/cgi-bin/user/info?openid=' + options.openid + '&lang=' + options.lang + '&access_token=' + this.token.accessToken;
54 this.request(url, {dataType: 'json'}, wrapper(callback));
55};
56
57/**
58 * 批量获取用户基本信息
59 * Example:
60 * ```
61 * api.batchGetUser(['openid1', 'openid2'], callback)
62 * ```
63 * Callback:
64 *
65 * - `err`, 调用失败时得到的异常
66 * - `result`, 调用正常时得到的对象
67 *
68 * Result:
69 * ```
70 * {
71 * "user_info_list": [{
72 * "subscribe": 1,
73 * "openid": "otvxTs4dckWG7imySrJd6jSi0CWE",
74 * "nickname": "iWithery",
75 * "sex": 1,
76 * "language": "zh_CN",
77 * "city": "Jieyang",
78 * "province": "Guangdong",
79 * "country": "China",
80 * "headimgurl": "http://wx.qlogo.cn/mmopen/xbIQx1GRqdvyqkMMhEaGOX802l1CyqMJNgUzKP8MeAeHFicRDSnZH7FY4XB7p8XHXIf6uJA2SCunTPicGKezDC4saKISzRj3nz/0",
81 * "subscribe_time": 1434093047,
82 * "unionid": "oR5GjjgEhCMJFyzaVZdrxZ2zRRF4",
83 * "remark": "",
84 * "groupid": 0
85 * }, {
86 * "subscribe": 0,
87 * "openid": "otvxTs_JZ6SEiP0imdhpi50fuSZg",
88 * "unionid": "oR5GjjjrbqBZbrnPwwmSxFukE41U",
89 * }]
90 * }
91 * ```
92 * @param {Array} openids 用户的openid数组。
93 * @param {Function} callback 回调函数
94 */
95exports.batchGetUsers = function (openids, callback) {
96 this.preRequest(this._batchGetUsers, arguments);
97};
98
99/*!
100 * 批量获取用户基本信息的未封装版本
101 */
102exports._batchGetUsers = function (openids, callback) {
103 var url = this.endpoint + '/cgi-bin/user/info/batchget?access_token=' + this.token.accessToken;
104 var data = {};
105 data.user_list = openids.map(function (openid) {
106 return {openid: openid, language: 'zh-CN'};
107 });
108 this.request(url, postJSON(data), wrapper(callback));
109};
110
111/**
112 * 获取关注者列表
113 * 详细细节 http://mp.weixin.qq.com/wiki/0/d0e07720fc711c02a3eab6ec33054804.html
114 * Examples:
115 * ```
116 * api.getFollowers(callback);
117 * // or
118 * api.getFollowers(nextOpenid, callback);
119 * ```
120 * Callback:
121 *
122 * - `err`, 调用失败时得到的异常
123 * - `result`, 调用正常时得到的对象
124 *
125 * Result:
126 * ```
127 * {
128 * "total":2,
129 * "count":2,
130 * "data":{
131 * "openid":["","OPENID1","OPENID2"]
132 * },
133 * "next_openid":"NEXT_OPENID"
134 * }
135 * ```
136 * @param {String} nextOpenid 调用一次之后,传递回来的nextOpenid。第一次获取时可不填
137 * @param {Function} callback 回调函数
138 */
139exports.getFollowers = function (nextOpenid, callback) {
140 this.preRequest(this._getFollowers, arguments);
141};
142
143/*!
144 * 获取关注者列表的未封装版本
145 */
146exports._getFollowers = function (nextOpenid, callback) {
147 // https://api.weixin.qq.com/cgi-bin/user/get?access_token=ACCESS_TOKEN&next_openid=NEXT_OPENID
148 if (typeof nextOpenid === 'function') {
149 callback = nextOpenid;
150 nextOpenid = '';
151 }
152 var url = this.endpoint + '/cgi-bin/user/get?next_openid=' + nextOpenid + '&access_token=' + this.token.accessToken;
153 this.request(url, {dataType: 'json'}, wrapper(callback));
154};
155
156/**
157 * 设置用户备注名
158 * 详细细节 http://mp.weixin.qq.com/wiki/1/4a566d20d67def0b3c1afc55121d2419.html
159 * Examples:
160 * ```
161 * api.updateRemark(openid, remark, callback);
162 * ```
163 * Callback:
164 *
165 * - `err`, 调用失败时得到的异常
166 * - `result`, 调用正常时得到的对象
167 *
168 * Result:
169 * ```
170 * {
171 * "errcode":0,
172 * "errmsg":"ok"
173 * }
174 * ```
175 * @param {String} openid 用户的openid
176 * @param {String} remark 新的备注名,长度必须小于30字符
177 * @param {Function} callback 回调函数
178 */
179exports.updateRemark = function (openid, remark, callback) {
180 this.preRequest(this._updateRemark, arguments);
181};
182
183/*!
184 * 设置用户备注名的未封装版本
185 */
186exports._updateRemark = function (openid, remark, callback) {
187 // https://api.weixin.qq.com/cgi-bin/user/info/updateremark?access_token=ACCESS_TOKEN
188 var url = this.endpoint + '/cgi-bin/user/info/updateremark?access_token=' + this.token.accessToken;
189 var data = {
190 openid: openid,
191 remark: remark
192 };
193 this.request(url, postJSON(data), wrapper(callback));
194};