Files
kiri-wchat/wchat/officialaccount/Subscribe.php
T
2021-04-06 15:20:39 +08:00

119 lines
2.1 KiB
PHP

<?php
namespace wchat\officialaccount;
use wchat\common\HttpClient;
class Subscribe extends AfficialAccount
{
private $openid = '';
private $template_id = '';
private $url = '';
private $miniprogram = [];
private $scene = '';
private $title = '';
private $keywords = [];
private $pushUrl = 'https://api.weixin.qq.com/cgi-bin/message/template/subscribe?access_token=';
/**
* @param $openid
* @return Subscribe
*/
public function setOpenid($openid): Subscribe
{
$this->openid = $openid;
return $this;
}
/**
* @param $template_id
* @return Subscribe
*/
public function setTemplateId($template_id): Subscribe
{
$this->template_id = $template_id;
return $this;
}
/**
* @param $url
* @return Subscribe
*/
public function setUrl($url): Subscribe
{
$this->url = $url;
return $this;
}
/**
* @param $appid
* @param $path
* @return Subscribe
*/
public function setMiniprogram($appid, $path): Subscribe
{
$this->miniprogram['appid'] = $appid;
$this->miniprogram['pagepath'] = $path;
return $this;
}
/**
* @param $scene
* @return Subscribe
*/
public function setScene($scene): Subscribe
{
$this->scene = $scene;
return $this;
}
/**
* @param $title
* @return Subscribe
*/
public function setTitle($title): Subscribe
{
$this->title = $title;
return $this;
}
/**
* @param $keyword
* @param $content
* @param null $color
*/
public function addContent($keyword, $content, $color = null)
{
$param['value'] = $content;
if (!empty($color)) {
$param['color'] = $color;
}
$this->keywords[$keyword] = $param;
}
/**
* @return array|mixed|\wchat\common\Result
*/
public function sendSubscribeMessage()
{
$requestParam['touser'] = $this->openid;
$requestParam['template_id'] = $this->template_id;
$requestParam['url'] = $this->url;
$requestParam['miniprogram'] = $this->miniprogram;
$requestParam['scene'] = $this->scene;
$requestParam['title'] = $this->title;
$requestParam['data'] = $this->keywords;
$requestUrl = $this->pushUrl . $this->config->getAccessToken();
$client = HttpClient::NewRequest();
return $client->post($requestUrl, $requestParam);
}
}