<?php

namespace App\Services\WeChat;

use Ixudra\Curl\Facades\Curl;

class Template extends Base
{


    /**
     * 获取模板列表
     * @return mixed
     * @throws WeChatException
     */
    public function getAll()
    {
        $access_token = $this->accessToken;
        $url = "https://api.weixin.qq.com/cgi-bin/template/get_all_private_template?access_token={$access_token}";
        $response = Curl::to($url)->asJson(true)->get();
        if (isset($response->errcode) && $response->errcode != 0) {
            throw new WeChatException($response->errmsg, $response->errcode);
        }

        return $response->template_list;
    }


    /**
     * 发送模板消息
     * @param array $data
     * @return mixed
     * @throws WeChatException
     */
    public function send($data)
    {
        $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKEN";
        $response = \Curl::to($url)->withData($data)->asJson(true)->get();
        if (isset($response['errcode']) && $response['errcode'] != 0) {
            throw new WeChatException($response['errmsg'], $response['errcode']);
        }

        return $response['msgid'];
    }
}
