157 lines
2.7 KiB
PHP
157 lines
2.7 KiB
PHP
<?php
|
|
/**
|
|
* Created by PhpStorm.
|
|
* User: whwyy
|
|
* Date: 2018/4/8 0008
|
|
* Time: 9:49
|
|
*/
|
|
|
|
namespace wchat;
|
|
|
|
use common\Result;
|
|
|
|
class PublicTemplate extends SmallProgram
|
|
{
|
|
|
|
private $keywords = [];
|
|
private $templateId = '';
|
|
private $first = '';
|
|
private $remark = '';
|
|
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;
|
|
}
|
|
|
|
/**
|
|
* @param string $templateId
|
|
*/
|
|
public function setTemplateId(string $templateId)
|
|
{
|
|
$this->templateId = $templateId;
|
|
}
|
|
|
|
/**
|
|
* @param string $openId
|
|
*/
|
|
public function setOpenId(string $openId)
|
|
{
|
|
$this->openId = $openId;
|
|
}
|
|
|
|
/**
|
|
* @param string $defaultUrl
|
|
*/
|
|
public function setDefaultUrl(string $defaultUrl)
|
|
{
|
|
$this->defaultUrl = $defaultUrl;
|
|
}
|
|
|
|
/**
|
|
* @param $name
|
|
* @param $context
|
|
* @param string $color
|
|
*/
|
|
public function replaceKeyword($name, $context, $color = '')
|
|
{
|
|
$this->keywords[$name] = ['value' => $context, 'color' => $color];
|
|
}
|
|
|
|
|
|
/**
|
|
* @param $name
|
|
* @param $context
|
|
* @param null $color
|
|
*/
|
|
public function addKeyword($name, $context, $color = null)
|
|
{
|
|
if (empty($color)) {
|
|
$color = '#000';
|
|
}
|
|
$this->keywords[$name] = [
|
|
'value' => $context,
|
|
'color' => $color
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @param $context
|
|
* @param string $color
|
|
*/
|
|
public function setFirst($context, $color = '#f00')
|
|
{
|
|
$this->first = [
|
|
'value' => $context,
|
|
'color' => $color
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @param $context
|
|
* @param string $color
|
|
*/
|
|
public function setRemark($context, $color = '#000')
|
|
{
|
|
$this->remark = [
|
|
'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();
|
|
|
|
$keywords = $this->keywords;
|
|
$keywords['first'] = $this->first;
|
|
$keywords['remark'] = $this->remark;
|
|
|
|
$default = [
|
|
"touser" => $this->openId,
|
|
"template_id" => $this->templateId,
|
|
"url" => $this->defaultUrl,
|
|
"data" => $keywords,
|
|
];
|
|
|
|
if (!empty($this->miniprogram)) {
|
|
$default['miniprogram'] = $this->miniprogram;
|
|
}
|
|
|
|
$params = json_encode($default, JSON_UNESCAPED_UNICODE);
|
|
|
|
$this->request->setIsSSL(true);
|
|
$this->request->addHeader('content-type', 'application/json');
|
|
|
|
$result = $this->request->post($url, $params);
|
|
$result->append('postBody', $params);
|
|
|
|
return $result;
|
|
}
|
|
}
|