Staff.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. * Staff.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\Staff;
  20. use EasyWeChat\Core\AbstractAPI;
  21. use EasyWeChat\Support\Collection;
  22. /**
  23. * Class Staff.
  24. */
  25. class Staff extends AbstractAPI
  26. {
  27. const API_LISTS = 'https://api.weixin.qq.com/cgi-bin/customservice/getkflist';
  28. const API_ONLINE = 'https://api.weixin.qq.com/cgi-bin/customservice/getonlinekflist';
  29. const API_DELETE = 'https://api.weixin.qq.com/customservice/kfaccount/del';
  30. const API_UPDATE = 'https://api.weixin.qq.com/customservice/kfaccount/update';
  31. const API_CREATE = 'https://api.weixin.qq.com/customservice/kfaccount/add';
  32. const API_INVITE_BIND = 'https://api.weixin.qq.com/customservice/kfaccount/inviteworker';
  33. const API_MESSAGE_SEND = 'https://api.weixin.qq.com/cgi-bin/message/custom/send';
  34. const API_AVATAR_UPLOAD = 'https://api.weixin.qq.com/customservice/kfaccount/uploadheadimg';
  35. const API_RECORDS = 'https://api.weixin.qq.com/customservice/msgrecord/getrecord';
  36. const API_MSG_LIST = 'https://api.weixin.qq.com/customservice/msgrecord/getmsglist';
  37. /**
  38. * List all staffs.
  39. *
  40. * @return \EasyWeChat\Support\Collection
  41. */
  42. public function lists()
  43. {
  44. return $this->parseJSON('get', [self::API_LISTS]);
  45. }
  46. /**
  47. * List all online staffs.
  48. *
  49. * @return \EasyWeChat\Support\Collection
  50. */
  51. public function onlines()
  52. {
  53. return $this->parseJSON('get', [self::API_ONLINE]);
  54. }
  55. /**
  56. * Create a staff.
  57. *
  58. * @param string $account
  59. * @param string $nickname
  60. *
  61. * @return \EasyWeChat\Support\Collection
  62. */
  63. public function create($account, $nickname)
  64. {
  65. $params = [
  66. 'kf_account' => $account,
  67. 'nickname' => $nickname,
  68. ];
  69. return $this->parseJSON('json', [self::API_CREATE, $params]);
  70. }
  71. /**
  72. * Update a staff.
  73. *
  74. * @param string $account
  75. * @param string $nickname
  76. *
  77. * @return \EasyWeChat\Support\Collection
  78. */
  79. public function update($account, $nickname)
  80. {
  81. $params = [
  82. 'kf_account' => $account,
  83. 'nickname' => $nickname,
  84. ];
  85. return $this->parseJSON('json', [self::API_UPDATE, $params]);
  86. }
  87. /**
  88. * Delete a staff.
  89. *
  90. * @param string $account
  91. *
  92. * @return \EasyWeChat\Support\Collection
  93. */
  94. public function delete($account)
  95. {
  96. // XXX: 微信那帮搞技术的都 TM 是 SB,url上的文本居然不 TM urlencode,
  97. // 这里客服账号因为有 @ 符,而微信不接收urlencode的账号。。
  98. // 简直是日了...
  99. // #222
  100. // PS: 如果你是微信做接口的,奉劝你们,尊重技术,不会别乱搞,笨不是你们的错,你们出来坑人就是大错特错。
  101. $accessTokenField = sprintf('%s=%s', $this->accessToken->getQueryName(), $this->accessToken->getToken());
  102. $url = sprintf(self::API_DELETE.'?%s&kf_account=%s', $accessTokenField, $account);
  103. $contents = $this->getHttp()->parseJSON(file_get_contents($url));
  104. $this->checkAndThrow($contents);
  105. return new Collection($contents);
  106. }
  107. /**
  108. * Invite a staff.
  109. *
  110. * @param string $account
  111. * @param string $wechatId
  112. *
  113. * @return \EasyWeChat\Support\Collection
  114. */
  115. public function invite($account, $wechatId)
  116. {
  117. $params = [
  118. 'kf_account' => $account,
  119. 'invite_wx' => $wechatId,
  120. ];
  121. return $this->parseJSON('json', [self::API_INVITE_BIND, $params]);
  122. }
  123. /**
  124. * Set staff avatar.
  125. *
  126. * @param string $account
  127. * @param string $path
  128. *
  129. * @return \EasyWeChat\Support\Collection
  130. */
  131. public function avatar($account, $path)
  132. {
  133. return $this->parseJSON('upload', [self::API_AVATAR_UPLOAD, ['media' => $path], [], ['kf_account' => $account]]);
  134. }
  135. /**
  136. * Get message builder.
  137. *
  138. * @param \EasyWeChat\Message\AbstractMessage|string $message
  139. *
  140. * @return \EasyWeChat\Staff\MessageBuilder
  141. *
  142. * @throws \EasyWeChat\Core\Exceptions\InvalidArgumentException
  143. */
  144. public function message($message)
  145. {
  146. $messageBuilder = new MessageBuilder($this);
  147. return $messageBuilder->message($message);
  148. }
  149. /**
  150. * Send a message.
  151. *
  152. * @param string|array $message
  153. *
  154. * @return \EasyWeChat\Support\Collection
  155. */
  156. public function send($message)
  157. {
  158. return $this->parseJSON('json', [self::API_MESSAGE_SEND, $message]);
  159. }
  160. /**
  161. * Get staff session history.
  162. *
  163. * @param int $startTime
  164. * @param int $endTime
  165. * @param int $page
  166. * @param int $pageSize
  167. *
  168. * @return \EasyWeChat\Support\Collection
  169. */
  170. public function records($startTime, $endTime, $page = 1, $pageSize = 10)
  171. {
  172. $params = [
  173. 'starttime' => is_numeric($startTime) ? $startTime : strtotime($startTime),
  174. 'endtime' => is_numeric($endTime) ? $endTime : strtotime($endTime),
  175. 'pageindex' => $page,
  176. 'pagesize' => $pageSize,
  177. ];
  178. return $this->parseJSON('json', [self::API_RECORDS, $params]);
  179. }
  180. /**
  181. * 获取聊天记录.
  182. *
  183. * @param int $startTime
  184. * @param int $endTime
  185. * @param int $msgId
  186. * @param int $number
  187. *
  188. * @return \EasyWeChat\Support\Collection
  189. */
  190. public function messages($startTime, $endTime, $msgId = 1, $number = 10000)
  191. {
  192. $params = [
  193. 'starttime' => is_numeric($startTime) ? $startTime : strtotime($startTime),
  194. 'endtime' => is_numeric($endTime) ? $endTime : strtotime($endTime),
  195. 'msgid' => $msgId,
  196. 'number' => $number,
  197. ];
  198. return $this->parseJSON('json', [self::API_MSG_LIST, $params]);
  199. }
  200. }