<?php
namespace app\common\library\wechat;
class WxSubMsg extends WxBase
{
public function sendTemplateMessage($param)
{
$accessToken = $this->getAccessToken();
$url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token={$accessToken}";
$params = [
'touser' => $param['touser'],
'template_id' => $param['template_id'],
'page' => $param['page'],
'data' => $param['data'],
];
$result = $this->post($url, $this->jsonEncode($params));
$describe = '发送订阅消息';
$this->doLogs(compact('describe', 'url', 'params', 'result'));
$response = $this->jsonDecode($result);
if (!isset($response['errcode'])) {
$this->error = 'not found errcode';
return false;
}
if ($response['errcode'] != 0) {
$this->error = $response['errmsg'];
return false;
}
return true;
}
public function getTemplateList()
{
$accessToken = $this->getAccessToken();
$url = "https://api.weixin.qq.com/wxaapi/newtmpl/gettemplate?access_token={$accessToken}";
$result = $this->get($url);
$this->doLogs(['describe' => '获取当前帐号下的订阅消息模板列表', 'url' => $url, 'result' => $result]);
$response = $this->jsonDecode($result);
if (!isset($response['errcode'])) {
$this->error = 'not found errcode';
return false;
}
if ($response['errcode'] != 0) {
$this->error = $response['errmsg'];
return false;
}
return $response;
}
public function addTemplate($tid, $kidList, $sceneDesc)
{
$accessToken = $this->getAccessToken();
$url = "https://api.weixin.qq.com/wxaapi/newtmpl/addtemplate?access_token={$accessToken}";
$params = [
'tid' => $tid,
'kidList' => $kidList,
'sceneDesc' => $sceneDesc,
];
$result = $this->post2($url, $params);
$this->doLogs(['describe' => '添加订阅消息模板', 'url' => $url, 'params' => $params, 'result' => $result]);
$response = $this->jsonDecode($result);
if (!isset($response['errcode'])) {
$this->error = 'not found errcode';
return false;
}
if ($response['errcode'] != 0) {
$this->error = $response['errmsg'];
return false;
}
return $response;
}
}