Code coverage report for wechat-enterprise/lib/api_tag.js

Statements: 100% (40 / 40)      Branches: 100% (0 / 0)      Functions: 100% (12 / 12)      Lines: 100% (40 / 40)      Ignored: none     

All files » wechat-enterprise/lib/ » api_tag.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 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 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 2851 1 1 1                                                     1 1           1   1 1     1                                                     1 1           1   1 1       1                                                   1 1           1   1 1     1                                                               1 1           1   1 1 1                                                                                       1 1           1   1 1       1                                                                                       1 1           1   1 1       1    
var urllib = require('urllib');
var util = require('./util');
var postJSON = util.postJSON;
var wrapper = util.wrapper;
 
/**
 * 创建标签
 *
 * 详细请看:http://qydev.weixin.qq.com/wiki/index.php?title=管理标签
 * Examples:
 * ```
 * api.createTag(name, callback);
 * ```
 *
 * Callback:
 *
 * - `err`, 调用失败时得到的异常
 * - `result`, 调用正常时得到的对象
 *
 * Result:
 * ```
 * {
 *  "errcode": 0,
 *  "errmsg": "created",
 *  "tagid": "1"
 * }
 * ```
 * @param {String} name 标签名字
 * @param {Function} callback 回调函数
 */
exports.createTag = function (name, callback) {
  this.preRequest(this._createTag, arguments);
};
 
/*!
 * 创建标签的未封装版本
 */
exports._createTag = function (name, callback) {
  // https://qyapi.weixin.qq.com/cgi-bin/tag/create?access_token=ACCESS_TOKEN
  var url = this.prefix + 'tag/create?access_token=' + this.token.accessToken;
  var data = {
    tagname: name
  };
  urllib.request(url, postJSON(data), wrapper(callback));
};
 
/**
 * 更新标签名字
 *
 * Examples:
 * ```
 * api.updateTagName(id, name, callback);
 * ```
 *
 * Callback:
 *
 * - `err`, 调用失败时得到的异常
 * - `result`, 调用正常时得到的对象
 *
 * Result:
 * ```
 * {
 *   "errcode": 0,
 *   "errmsg": "updated"
 * }
 * ```
 * @param {String} id 标签ID
 * @param {String} name 标签名称。最长64个字符
 * @param {Function} callback 回调函数
 */
exports.updateTagName = function (id, name, callback) {
  this.preRequest(this._updateTagName, arguments);
};
 
/*!
 * 更新标签名字的未封装版本
 */
exports._updateTagName = function (id, name, callback) {
  // https://qyapi.weixin.qq.com/cgi-bin/tag/update?access_token=ACCESS_TOKEN
  var url = this.prefix + 'tag/update?access_token=' + this.token.accessToken;
  var data = {
    tagid: id,
    tagname: name
  };
  urllib.request(url, postJSON(data), wrapper(callback));
};
 
/**
 * 删除标签
 *
 * Examples:
 * ```
 * api.deleteTag(id, callback);
 * ```
 *
 * Callback:
 *
 * - `err`, 调用失败时得到的异常
 * - `result`, 调用正常时得到的对象
 *
 * Result:
 * ```
 * {
 *  "errcode": 0,
 *  "errmsg": "deleted"
 * }
 * ```
 * @param {Number} id 标签ID
 * @param {Function} callback 回调函数
 */
exports.deleteTag = function (id, callback) {
  this.preRequest(this._deleteTag, arguments);
};
 
/*!
 * 删除标签的未封装版本
 */
exports._deleteTag = function (id, callback) {
  // https://qyapi.weixin.qq.com/cgi-bin/tag/delete?access_token=ACCESS_TOKEN&tagid=1
  var url = this.prefix + 'tag/delete?access_token=' + this.token.accessToken;
  var opts = {dataType: 'json', data: {
    tagid: id
  }};
  urllib.request(url, opts, wrapper(callback));
};
 
/**
 * 获取标签成员
 *
 * Examples:
 * ```
 * api.getTagUsers(id, callback);
 * ```
 *
 * Callback:
 *
 * - `err`, 调用失败时得到的异常
 * - `result`, 调用正常时得到的对象
 *
 * Result:
 * ```
 * {
 *   "errcode": 0,
 *   "errmsg": "ok",
 *   "userlist": [
 *     {
 *         "userid": "zhangsan",
 *         "name": "李四"
 *     }
 *   ]
 * }
 * ```
 * @param {Number} id 标签ID
 * @param {Function} callback 回调函数
 */
exports.getTagUsers = function (id, callback) {
  this.preRequest(this._getTagUsers, arguments);
};
 
/*!
 * 获取标签成员的未封装版本
 */
exports._getTagUsers = function (id, callback) {
  // https://qyapi.weixin.qq.com/cgi-bin/tag/get?access_token=ACCESS_TOKEN&tagid=1
  var url = this.prefix + 'tag/get?access_token=' + this.token.accessToken;
  var opts = {dataType: 'json', data: {tagid: id}};
  urllib.request(url, opts, wrapper(callback));
};
 
/**
 * 增加标签成员
 *
 * Examples:
 * ```
 * var userIdList = ['id1', 'id2'];
 * api.addTagUsers(id, userIdList, callback);
 * ```
 *
 * Callback:
 *
 * - `err`, 调用失败时得到的异常
 * - `result`, 调用正常时得到的对象
 *
 * Result:
 * a)正确时返回
 * ```
 * {
 *   "errcode": 0,
 *   "errmsg": "deleted"
 * }
 * ```
 * b)若部分userid非法,则返回
 * ```
 * {
 *   "errcode": 0,
 *   "errmsg": "invalid userlist failed"
 *   "invalidlist":"usr1|usr2|usr"
 * }
 * ```
 * c)当包含的userid全部非法时返回
 * ```
 * {
 *   "errcode": 40031,
 *   "errmsg": "all list invalid"
 * }
 * ```
 * @param {Number} id 标签ID
 * @param {Array} userIdList 用户ID列表
 * @param {Function} callback 回调函数
 */
exports.addTagUsers = function (id, userIdList, callback) {
  this.preRequest(this._addTagUsers, arguments);
};
 
/*!
 * 增加标签成员的未封装版本
 */
exports._addTagUsers = function (id, userIdList, callback) {
  // https://qyapi.weixin.qq.com/cgi-bin/tag/addtagusers?access_token=ACCESS_TOKEN
  var url = this.prefix + 'tag/addtagusers?access_token=' + this.token.accessToken;
  var data = {
    tagid: id,
    userlist: userIdList
  };
  urllib.request(url, postJSON(data), wrapper(callback));
};
 
/**
 * 删除标签成员
 *
 * Examples:
 * ```
 * var userIdList = ['id1', 'id2'];
 * api.deleteTagUsers(id, userIdList, callback);
 * ```
 *
 * Callback:
 *
 * - `err`, 调用失败时得到的异常
 * - `result`, 调用正常时得到的对象
 *
 * Result:
 * a)正确时返回
 * ```
 * {
 *   "errcode": 0,
 *   "errmsg": "deleted"
 * }
 * ```
 * b)若部分userid非法,则返回
 * ```
 * {
 *   "errcode": 0,
 *   "errmsg": "invalid userlist failed"
 *   "invalidlist":"usr1|usr2|usr"
 * }
 * ```
 * c)当包含的userid全部非法时返回
 * ```
 * {
 *   "errcode": 40031,
 *   "errmsg": "all list invalid"
 * }
 * ```
 * @param {Number} id 标签ID
 * @param {Array} userIdList 用户ID数组
 * @param {Function} callback 回调函数
 */
exports.deleteTagUsers = function (id, userIdList, callback) {
  this.preRequest(this._deleteTagUsers, arguments);
};
 
/*!
 * 删除标签成员的未封装版本
 */
exports._deleteTagUsers = function (id, userIdList, callback) {
  // https://qyapi.weixin.qq.com/cgi-bin/tag/deltagusers?access_token=ACCESS_TOKEN
  var url = this.prefix + 'tag/deltagusers?access_token=' + this.token.accessToken;
  var data = {
    tagid: id,
    userlist: userIdList
  };
  urllib.request(url, postJSON(data), wrapper(callback));
};