KfAccount.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. namespace App\Services\WeChat;
  3. class KfAccount extends Base
  4. {
  5. /**
  6. * 客服接口-发消息
  7. * @param string $to_user
  8. * @param string $msg_type
  9. * @param array $data
  10. * @return mixed
  11. * @throws WeChatException
  12. */
  13. public function send($to_user, $msg_type, $data)
  14. {
  15. $post = [
  16. 'touser' => $to_user,
  17. 'msgtype' => $msg_type,
  18. $msg_type => $data,
  19. ];
  20. $access_token = $this->accessToken;
  21. $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token={$access_token}";
  22. $response = \Curl::to($url)
  23. ->withData(json_encode($post, JSON_UNESCAPED_UNICODE))
  24. ->asJsonResponse(true)
  25. ->post();
  26. if (isset($response['errcode']) && $response['errcode'] != 0) {
  27. throw new WeChatException($response['errmsg'], $response['errcode']);
  28. }
  29. return $response;
  30. }
  31. /**
  32. * 获取所有客服账号
  33. * @return mixed
  34. * @throws WeChatException
  35. */
  36. public function getkflist()
  37. {
  38. $access_token = $this->accessToken;
  39. $url = "https://api.weixin.qq.com/cgi-bin/customservice/getkflist?access_token={$access_token}";
  40. $response = \Curl::to($url)->asJson(true)->get();
  41. if (isset($response['errcode']) && $response['errcode'] != 0) {
  42. throw new WeChatException($response['errmsg'], $response['errcode']);
  43. }
  44. return $response;
  45. }
  46. /**
  47. * 添加客服帐号
  48. * @param string $kf_account
  49. * @param string $nickname
  50. * @param string $password
  51. * @return mixed
  52. * @throws WeChatException
  53. */
  54. public function add($kf_account, $nickname, $password)
  55. {
  56. $post = [
  57. 'kf_account' => $kf_account,
  58. 'nickname' => $nickname,
  59. 'password' => $password,
  60. ];
  61. $access_token = $this->accessToken;
  62. $url = "https://api.weixin.qq.com/customservice/kfaccount/add?access_token={$access_token}";
  63. $response = \Curl::to($url)->withData($post)->asJson(true)->post();
  64. if (isset($response['errcode']) && $response['errcode'] != 0) {
  65. throw new WeChatException($response['errmsg'], $response['errcode']);
  66. }
  67. return $response;
  68. }
  69. /**
  70. * 修改客服帐号
  71. * @param string $kf_account
  72. * @param string $nickname
  73. * @param string $password
  74. * @return mixed
  75. * @throws WeChatException
  76. */
  77. public function update($kf_account, $nickname, $password)
  78. {
  79. $post = [
  80. 'kf_account' => $kf_account,
  81. 'nickname' => $nickname,
  82. 'password' => $password,
  83. ];
  84. $access_token = $this->accessToken;
  85. $url = "https://api.weixin.qq.com/customservice/kfaccount/update?access_token={$access_token}";
  86. $response = \Curl::to($url)->withData($post)->asJson(true)->post();
  87. if (isset($response['errcode']) && $response['errcode'] != 0) {
  88. throw new WeChatException($response['errmsg'], $response['errcode']);
  89. }
  90. return $response;
  91. }
  92. /**
  93. * 删除客服帐号
  94. * @param string $kf_account
  95. * @param string $nickname
  96. * @param string $password
  97. * @return mixed
  98. * @throws WeChatException
  99. */
  100. public function delete($kf_account, $nickname, $password)
  101. {
  102. $post = [
  103. 'kf_account' => $kf_account,
  104. 'nickname' => $nickname,
  105. 'password' => $password,
  106. ];
  107. $access_token = $this->accessToken;
  108. $url = "https://api.weixin.qq.com/customservice/kfaccount/del?access_token={$access_token}";
  109. $response = \Curl::to($url)->withData($post)->asJson(true)->post();
  110. if (isset($response['errcode']) && $response['errcode'] != 0) {
  111. throw new WeChatException($response['errmsg'], $response['errcode']);
  112. }
  113. return $response;
  114. }
  115. /**
  116. * 修改客服帐号头像
  117. * @param string $kf_account
  118. * @param string $image_url
  119. * @return mixed
  120. * @throws WeChatException
  121. */
  122. public function uploadHeadImg($kf_account, $image_url)
  123. {
  124. $filename = storage_path() . '/app/' . uniqid() . '.png';
  125. \File::put($filename, \Curl::to($image_url)->get());
  126. $access_token = $this->accessToken;
  127. $http = "http://api.weixin.qq.com/customservice/kfaccount/uploadheadimg?kf_account={$kf_account}&access_token={$access_token}";
  128. $data = [
  129. 'media' => "@{$filename}",
  130. ];
  131. $response = \Curl::to($http)->withData($data)
  132. ->withFile('chat_' . uniqid(), $filename)
  133. ->asJson()
  134. ->post();
  135. \File::delete($filename);
  136. if (isset($response['errcode']) && $response['errcode'] != 0) {
  137. throw new WeChatException($response['errmsg'], $response['errcode']);
  138. }
  139. return $response;
  140. }
  141. }