123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217 |
- <?php
- namespace App\Services\WeChat;
- class Mass extends Base
- {
- /**
- * 发送预览
- * @param string $to_user
- * @param string $msg_type
- * @param array $data
- * @return mixed
- * @throws WeChatException
- */
- public function preview($to_user, $msg_type, $data)
- {
- $access_token = $this->accessToken;
- $url = "https://api.weixin.qq.com/cgi-bin/message/mass/preview?access_token={$access_token}";
- $post = [
- 'touser' => $to_user,
- 'towxname' => $to_user,
- 'msgtype' => $msg_type,
- $msg_type => $data,
- ];
- $response = \Curl::to($url)
- ->withData(json_encode($post, JSON_UNESCAPED_UNICODE))
- ->asJsonResponse(true)
- ->post();
- if (isset($response['errcode']) && $response['errcode'] != 0) {
- throw new WeChatException($response['errmsg'], $response['errcode']);
- }
- return $response;
- }
- /**
- * 查询群发消息发送状态
- * @param int|null $msg_id
- * @return mixed
- * @throws WeChatException
- */
- public function get($msg_id)
- {
- $access_token = $this->accessToken;
- $url = "https://api.weixin.qq.com/cgi-bin/message/mass/get?access_token={$access_token}";
- $response = \Curl::to($url)->withData([
- 'msg_id' => $msg_id,
- ])->asJson(true)->post();
- if (isset($response['errcode']) && $response['errcode'] != 0) {
- throw new WeChatException($response['errmsg'], $response['errcode']);
- }
- return $response;
- }
- /**
- * 根据OpenID列表群发【订阅号不可用,服务号认证后可用】
- * @param string $to_users
- * @param string $msg_type
- * @param array $data
- * @return mixed
- * @throws WeChatException
- */
- public function send($to_users, $msg_type, $data)
- {
- $post = [
- "touser" => $to_users,
- "msgtype" => $msg_type,
- $msg_type => $data,
- 'send_ignore_reprint' => 1,
- ];
- $access_token = $this->accessToken;
- $url = "https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token={$access_token}";
- $response = \Curl::to($url)
- ->withData(json_encode($post, JSON_UNESCAPED_UNICODE))
- ->asJsonResponse(true)
- ->post();
- if (isset($response['errcode']) && $response['errcode'] != 0) {
- throw new WeChatException($response['errmsg'], $response['errcode']);
- }
- return $response;
- }
- /**
- * 根据标签进行群发【订阅号与服务号认证后均可用】
- * @param int $tagId
- * @param string $msg_type
- * @param array $data
- * @return mixed
- * @throws WeChatException
- */
- public function sendTag(int $tagId, $msg_type, array $data)
- {
- $post = [
- 'filter' => [
- 'is_to_all' => false,
- 'tag_id' => $tagId,
- ],
- 'msgtype' => $msg_type,
- $msg_type => $data,
- 'send_ignore_reprint' => 1,
- ];
- $access_token = $this->accessToken;
- $url = "https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token={$access_token}";
- $response = \Curl::to($url)
- ->withData(json_encode($post, JSON_UNESCAPED_UNICODE))
- ->asJsonResponse(true)
- ->post();
- if (isset($response['errcode']) && $response['errcode'] != 0) {
- throw new WeChatException($response['errmsg'], $response['errcode']);
- }
- return $response;
- }
- /**
- * 根据标签进行群发【订阅号与服务号认证后均可用】
- * @param string $msg_type
- * @param array $data
- * @return mixed
- * @throws WeChatException
- */
- public function sendAll($msg_type, $data)
- {
- $post = [
- 'filter' => [
- 'is_to_all' => true,
- ],
- 'msgtype' => $msg_type,
- $msg_type => $data,
- 'send_ignore_reprint' => 1,
- ];
- $access_token = $this->accessToken;
- $url = "https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token={$access_token}";
- $response = \Curl::to($url)
- ->withData(json_encode($post, JSON_UNESCAPED_UNICODE))
- ->asJsonResponse(true)
- ->post();
- if (isset($response['errcode']) && $response['errcode'] != 0) {
- throw new WeChatException($response['errmsg'], $response['errcode']);
- }
- return $response;
- }
- /**
- * 删除群发【订阅号与服务号认证后均可用】
- * @param int $msg_id
- * @param int $article_idx
- * @return bool
- * @throws WeChatException
- */
- public function delete($msg_id, $article_idx = 0)
- {
- $post = [
- 'msg_id' => $msg_id,
- 'article_idx' => $article_idx,
- ];
- $access_token = $this->accessToken;
- $url = "https://api.weixin.qq.com/cgi-bin/message/mass/delete?access_token={$access_token}";
- $response = \Curl::to($url)->withData($post)->asJson()->post();
- if (isset($response->errcode) && $response->errcode != 0) {
- throw new WeChatException($response->errmsg, $response->errcode);
- }
- return true;
- }
- /**
- * 获取群发速度
- * @return mixed
- * @throws WeChatException
- */
- public function getSpeed()
- {
- $access_token = $this->accessToken;
- $url = "https://api.weixin.qq.com/cgi-bin/message/mass/speed/get?access_token={$access_token}";
- $response = \Curl::to($url)->asJson()->post();
- if (isset($response->errcode)) {
- throw new WeChatException($response->errmsg, $response->errcode);
- }
- return $response;
- }
- /**
- * 设置群发速度
- * @param int $speed
- * @return mixed
- * @throws WeChatException
- */
- public function setSpeed($speed)
- {
- $post = [
- 'speed' => $speed,
- ];
- $access_token = $this->accessToken;
- $url = "https://api.weixin.qq.com/cgi-bin/message/mass/speed/set?access_token={$access_token}";
- $response = \Curl::to($url)->withData($post)->asJson()->post();
- if (isset($response->errcode) && $response->errcode != 0) {
- throw new WeChatException($response->errmsg, $response->errcode);
- }
- return $response;
- }
- }
|