Mass.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php
  2. namespace App\Services\WeChat;
  3. class Mass 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 preview($to_user, $msg_type, $data)
  14. {
  15. $access_token = $this->accessToken;
  16. $url = "https://api.weixin.qq.com/cgi-bin/message/mass/preview?access_token={$access_token}";
  17. $post = [
  18. 'touser' => $to_user,
  19. 'towxname' => $to_user,
  20. 'msgtype' => $msg_type,
  21. $msg_type => $data,
  22. ];
  23. $response = \Curl::to($url)
  24. ->withData(json_encode($post, JSON_UNESCAPED_UNICODE))
  25. ->asJsonResponse(true)
  26. ->post();
  27. if (isset($response['errcode']) && $response['errcode'] != 0) {
  28. throw new WeChatException($response['errmsg'], $response['errcode']);
  29. }
  30. return $response;
  31. }
  32. /**
  33. * 查询群发消息发送状态
  34. * @param int|null $msg_id
  35. * @return mixed
  36. * @throws WeChatException
  37. */
  38. public function get($msg_id)
  39. {
  40. $access_token = $this->accessToken;
  41. $url = "https://api.weixin.qq.com/cgi-bin/message/mass/get?access_token={$access_token}";
  42. $response = \Curl::to($url)->withData([
  43. 'msg_id' => $msg_id,
  44. ])->asJson(true)->post();
  45. if (isset($response['errcode']) && $response['errcode'] != 0) {
  46. throw new WeChatException($response['errmsg'], $response['errcode']);
  47. }
  48. return $response;
  49. }
  50. /**
  51. * 根据OpenID列表群发【订阅号不可用,服务号认证后可用】
  52. * @param string $to_users
  53. * @param string $msg_type
  54. * @param array $data
  55. * @return mixed
  56. * @throws WeChatException
  57. */
  58. public function send($to_users, $msg_type, $data)
  59. {
  60. $post = [
  61. "touser" => $to_users,
  62. "msgtype" => $msg_type,
  63. $msg_type => $data,
  64. 'send_ignore_reprint' => 1,
  65. ];
  66. $access_token = $this->accessToken;
  67. $url = "https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token={$access_token}";
  68. $response = \Curl::to($url)
  69. ->withData(json_encode($post, JSON_UNESCAPED_UNICODE))
  70. ->asJsonResponse(true)
  71. ->post();
  72. if (isset($response['errcode']) && $response['errcode'] != 0) {
  73. throw new WeChatException($response['errmsg'], $response['errcode']);
  74. }
  75. return $response;
  76. }
  77. /**
  78. * 根据标签进行群发【订阅号与服务号认证后均可用】
  79. * @param int $tagId
  80. * @param string $msg_type
  81. * @param array $data
  82. * @return mixed
  83. * @throws WeChatException
  84. */
  85. public function sendTag(int $tagId, $msg_type, array $data)
  86. {
  87. $post = [
  88. 'filter' => [
  89. 'is_to_all' => false,
  90. 'tag_id' => $tagId,
  91. ],
  92. 'msgtype' => $msg_type,
  93. $msg_type => $data,
  94. 'send_ignore_reprint' => 1,
  95. ];
  96. $access_token = $this->accessToken;
  97. $url = "https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token={$access_token}";
  98. $response = \Curl::to($url)
  99. ->withData(json_encode($post, JSON_UNESCAPED_UNICODE))
  100. ->asJsonResponse(true)
  101. ->post();
  102. if (isset($response['errcode']) && $response['errcode'] != 0) {
  103. throw new WeChatException($response['errmsg'], $response['errcode']);
  104. }
  105. return $response;
  106. }
  107. /**
  108. * 根据标签进行群发【订阅号与服务号认证后均可用】
  109. * @param string $msg_type
  110. * @param array $data
  111. * @return mixed
  112. * @throws WeChatException
  113. */
  114. public function sendAll($msg_type, $data)
  115. {
  116. $post = [
  117. 'filter' => [
  118. 'is_to_all' => true,
  119. ],
  120. 'msgtype' => $msg_type,
  121. $msg_type => $data,
  122. 'send_ignore_reprint' => 1,
  123. ];
  124. $access_token = $this->accessToken;
  125. $url = "https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token={$access_token}";
  126. $response = \Curl::to($url)
  127. ->withData(json_encode($post, JSON_UNESCAPED_UNICODE))
  128. ->asJsonResponse(true)
  129. ->post();
  130. if (isset($response['errcode']) && $response['errcode'] != 0) {
  131. throw new WeChatException($response['errmsg'], $response['errcode']);
  132. }
  133. return $response;
  134. }
  135. /**
  136. * 删除群发【订阅号与服务号认证后均可用】
  137. * @param int $msg_id
  138. * @param int $article_idx
  139. * @return bool
  140. * @throws WeChatException
  141. */
  142. public function delete($msg_id, $article_idx = 0)
  143. {
  144. $post = [
  145. 'msg_id' => $msg_id,
  146. 'article_idx' => $article_idx,
  147. ];
  148. $access_token = $this->accessToken;
  149. $url = "https://api.weixin.qq.com/cgi-bin/message/mass/delete?access_token={$access_token}";
  150. $response = \Curl::to($url)->withData($post)->asJson()->post();
  151. if (isset($response->errcode) && $response->errcode != 0) {
  152. throw new WeChatException($response->errmsg, $response->errcode);
  153. }
  154. return true;
  155. }
  156. /**
  157. * 获取群发速度
  158. * @return mixed
  159. * @throws WeChatException
  160. */
  161. public function getSpeed()
  162. {
  163. $access_token = $this->accessToken;
  164. $url = "https://api.weixin.qq.com/cgi-bin/message/mass/speed/get?access_token={$access_token}";
  165. $response = \Curl::to($url)->asJson()->post();
  166. if (isset($response->errcode)) {
  167. throw new WeChatException($response->errmsg, $response->errcode);
  168. }
  169. return $response;
  170. }
  171. /**
  172. * 设置群发速度
  173. * @param int $speed
  174. * @return mixed
  175. * @throws WeChatException
  176. */
  177. public function setSpeed($speed)
  178. {
  179. $post = [
  180. 'speed' => $speed,
  181. ];
  182. $access_token = $this->accessToken;
  183. $url = "https://api.weixin.qq.com/cgi-bin/message/mass/speed/set?access_token={$access_token}";
  184. $response = \Curl::to($url)->withData($post)->asJson()->post();
  185. if (isset($response->errcode) && $response->errcode != 0) {
  186. throw new WeChatException($response->errmsg, $response->errcode);
  187. }
  188. return $response;
  189. }
  190. }