User.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. /*
  3. * This file is part of the overtrue/wechat.
  4. *
  5. * (c) overtrue <i@overtrue.me>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. /**
  11. * User.php.
  12. *
  13. * @author overtrue <i@overtrue.me>
  14. * @copyright 2015 overtrue <i@overtrue.me>
  15. *
  16. * @see https://github.com/overtrue
  17. * @see http://overtrue.me
  18. */
  19. namespace EasyWeChat\User;
  20. use EasyWeChat\Core\AbstractAPI;
  21. /**
  22. * Class User.
  23. */
  24. class User extends AbstractAPI
  25. {
  26. const API_GET = 'https://api.weixin.qq.com/cgi-bin/user/info';
  27. const API_BATCH_GET = 'https://api.weixin.qq.com/cgi-bin/user/info/batchget';
  28. const API_LIST = 'https://api.weixin.qq.com/cgi-bin/user/get';
  29. const API_GROUP = 'https://api.weixin.qq.com/cgi-bin/groups/getid';
  30. const API_REMARK = 'https://api.weixin.qq.com/cgi-bin/user/info/updateremark';
  31. const API_OAUTH_GET = 'https://api.weixin.qq.com/sns/userinfo';
  32. const API_GET_BLACK_LIST = 'https://api.weixin.qq.com/cgi-bin/tags/members/getblacklist';
  33. const API_BATCH_BLACK_LIST = 'https://api.weixin.qq.com/cgi-bin/tags/members/batchblacklist';
  34. const API_BATCH_UNBLACK_LIST = 'https://api.weixin.qq.com/cgi-bin/tags/members/batchunblacklist';
  35. /**
  36. * Fetch a user by open id.
  37. *
  38. * @param string $openId
  39. * @param string $lang
  40. *
  41. * @return \EasyWeChat\Support\Collection
  42. */
  43. public function get($openId, $lang = 'zh_CN')
  44. {
  45. $params = [
  46. 'openid' => $openId,
  47. 'lang' => $lang,
  48. ];
  49. return $this->parseJSON('get', [self::API_GET, $params]);
  50. }
  51. /**
  52. * Batch get users.
  53. *
  54. * @param array $openIds
  55. * @param string $lang
  56. *
  57. * @return \EasyWeChat\Support\Collection
  58. */
  59. public function batchGet(array $openIds, $lang = 'zh_CN')
  60. {
  61. $params = [];
  62. $params['user_list'] = array_map(function ($openId) use ($lang) {
  63. return [
  64. 'openid' => $openId,
  65. 'lang' => $lang,
  66. ];
  67. }, $openIds);
  68. return $this->parseJSON('json', [self::API_BATCH_GET, $params]);
  69. }
  70. /**
  71. * List users.
  72. *
  73. * @param string $nextOpenId
  74. *
  75. * @return \EasyWeChat\Support\Collection
  76. */
  77. public function lists($nextOpenId = null)
  78. {
  79. $params = ['next_openid' => $nextOpenId];
  80. return $this->parseJSON('get', [self::API_LIST, $params]);
  81. }
  82. /**
  83. * Set user remark.
  84. *
  85. * @param string $openId
  86. * @param string $remark
  87. *
  88. * @return \EasyWeChat\Support\Collection
  89. */
  90. public function remark($openId, $remark)
  91. {
  92. $params = [
  93. 'openid' => $openId,
  94. 'remark' => $remark,
  95. ];
  96. return $this->parseJSON('json', [self::API_REMARK, $params]);
  97. }
  98. /**
  99. * Get user's group id.
  100. *
  101. * @param string $openId
  102. *
  103. * @return \EasyWeChat\Support\Collection
  104. */
  105. public function group($openId)
  106. {
  107. return $this->getGroup($openId);
  108. }
  109. /**
  110. * Get user's group.
  111. *
  112. * @param string $openId
  113. *
  114. * @return \EasyWeChat\Support\Collection
  115. */
  116. public function getGroup($openId)
  117. {
  118. $params = ['openid' => $openId];
  119. return $this->parseJSON('json', [self::API_GROUP, $params]);
  120. }
  121. /**
  122. * Get black list.
  123. *
  124. * @param string|null $beginOpenid
  125. *
  126. * @return \EasyWeChat\Support\Collection
  127. */
  128. public function blacklist($beginOpenid = null)
  129. {
  130. $params = ['begin_openid' => $beginOpenid];
  131. return $this->parseJSON('json', [self::API_GET_BLACK_LIST, $params]);
  132. }
  133. /**
  134. * Batch block user.
  135. *
  136. * @param array $openidList
  137. *
  138. * @return \EasyWeChat\Support\Collection
  139. */
  140. public function batchBlock(array $openidList)
  141. {
  142. $params = ['openid_list' => $openidList];
  143. return $this->parseJSON('json', [self::API_BATCH_BLACK_LIST, $params]);
  144. }
  145. /**
  146. * Batch unblock user.
  147. *
  148. * @param array $openidList
  149. *
  150. * @return \EasyWeChat\Support\Collection
  151. */
  152. public function batchUnblock(array $openidList)
  153. {
  154. $params = ['openid_list' => $openidList];
  155. return $this->parseJSON('json', [self::API_BATCH_UNBLACK_LIST, $params]);
  156. }
  157. }