Files
kiri-wchat/wchat/wx/PublicTemplate.php
T

158 lines
2.8 KiB
PHP
Raw Normal View History

2019-08-21 11:54:20 +08:00
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/4/8 0008
* Time: 9:49
*/
2020-03-05 12:41:49 +08:00
namespace wchat\wx;
2019-08-21 11:54:20 +08:00
2020-03-05 12:41:49 +08:00
use wchat\common\Result;
2019-11-11 18:14:47 +08:00
class PublicTemplate extends SmallProgram
2019-08-21 11:54:20 +08:00
{
private $keywords = [];
private $templateId = '';
2019-08-21 13:34:03 +08:00
private $first = '';
private $remark = '';
2019-08-21 11:54:20 +08:00
private $openId = '';
private $defaultUrl = 'http://weixin.qq.com/download';
private $sendUrl = 'https://api.weixin.qq.com/cgi-bin/message/template/send';
private $miniprogram = [];
/**
* @param array $keywords
*/
public function setKeywords(array $keywords)
{
$this->keywords = $keywords;
}
/**
2021-04-06 15:20:39 +08:00
* @param $templateId
2019-08-21 11:54:20 +08:00
*/
2021-04-06 15:20:39 +08:00
public function setTemplateId($templateId)
2019-08-21 11:54:20 +08:00
{
$this->templateId = $templateId;
}
/**
2021-04-06 15:20:39 +08:00
* @param $openId
2019-08-21 11:54:20 +08:00
*/
2021-04-06 15:20:39 +08:00
public function setOpenId($openId)
2019-08-21 11:54:20 +08:00
{
$this->openId = $openId;
}
/**
2021-04-06 15:20:39 +08:00
* @param $defaultUrl
2019-08-21 11:54:20 +08:00
*/
2021-04-06 15:20:39 +08:00
public function setDefaultUrl($defaultUrl)
2019-08-21 11:54:20 +08:00
{
$this->defaultUrl = $defaultUrl;
}
/**
* @param $name
* @param $context
2021-04-06 15:20:39 +08:00
* @param $color
2019-08-21 11:54:20 +08:00
*/
public function replaceKeyword($name, $context, $color = '')
{
2019-08-21 13:37:26 +08:00
$this->keywords[$name] = ['value' => $context, 'color' => $color];
2019-08-21 11:54:20 +08:00
}
/**
2019-08-21 13:37:26 +08:00
* @param $name
2019-08-21 11:54:20 +08:00
* @param $context
* @param null $color
*/
2019-08-21 13:37:26 +08:00
public function addKeyword($name, $context, $color = null)
2019-08-21 11:54:20 +08:00
{
if (empty($color)) {
$color = '#000';
}
2019-08-21 13:37:26 +08:00
$this->keywords[$name] = [
2019-08-21 11:54:20 +08:00
'value' => $context,
'color' => $color
];
}
/**
* @param $context
2021-04-06 15:20:39 +08:00
* @param $color
2019-08-21 11:54:20 +08:00
*/
public function setFirst($context, $color = '#f00')
{
2019-08-21 13:34:03 +08:00
$this->first = [
2019-08-21 11:54:20 +08:00
'value' => $context,
'color' => $color
];
}
/**
* @param $context
2021-04-06 15:20:39 +08:00
* @param $color
2019-08-21 11:54:20 +08:00
*/
public function setRemark($context, $color = '#000')
{
2019-08-21 13:34:03 +08:00
$this->remark = [
2019-08-21 11:54:20 +08:00
'value' => $context,
'color' => $color
];
}
/**
* @param $appid
* @param $pagepath
*/
public function setMiniprogram($appid, $pagepath)
{
$this->miniprogram = [
'appid' => $appid,
'pagepath' => $pagepath
];
}
/**
* @return Result
* @throws \Exception
*
* 奴隶交易通知
*/
public function sendTemplate()
{
$url = $this->sendUrl . '?access_token=' . $this->getAccessToken();
2019-08-21 13:34:03 +08:00
$keywords = $this->keywords;
$keywords['first'] = $this->first;
$keywords['remark'] = $this->remark;
2019-08-21 11:54:20 +08:00
$default = [
"touser" => $this->openId,
"template_id" => $this->templateId,
"url" => $this->defaultUrl,
2019-08-21 13:34:03 +08:00
"data" => $keywords,
2019-08-21 11:54:20 +08:00
];
if (!empty($this->miniprogram)) {
$default['miniprogram'] = $this->miniprogram;
}
2020-05-22 15:27:32 +08:00
$params = json_encode($default, JSON_UNESCAPED_UNICODE);
2019-08-21 11:54:20 +08:00
$this->request->setIsSSL(true);
2020-05-22 15:17:24 +08:00
$this->request->addHeader('Content-Type', 'application/json');
2020-04-01 11:53:15 +08:00
$this->request->setErrorField('errcode');
$this->request->setErrorMsgField('errmsg');
2020-05-22 15:27:32 +08:00
$result = $this->request->post($url, $params);
2020-05-22 15:15:08 +08:00
$result->append('postBody', $default);
2019-08-21 11:54:20 +08:00
return $result;
}
}