<?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;
    }
}
