Files
kiri-wchat/wx/Template.php
T

158 lines
2.7 KiB
PHP
Raw Normal View History

2018-07-19 12:10:22 +08:00
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/4/8 0008
* Time: 9:49
*/
namespace wchat;
class Template extends Base
{
2019-07-12 19:25:47 +08:00
private $keywords = [];
private $templateId = '';
private $formId = '';
private $openId = '';
private $defaultUrl = '';
private $page = 'pages/index/index';
private $emphasis_keyword = '';
2019-07-15 15:20:32 +08:00
private $sendUrl = 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send';
2019-07-12 19:25:47 +08:00
/** @var Template $instance */
private static $instance = null;
2019-07-12 19:26:59 +08:00
private function __construct()
{
}
2019-07-12 19:25:47 +08:00
/**
* @return Template
*/
public static function getInstance()
{
if (static::$instance === null) {
static::$instance = new Template();
}
return static::$instance;
}
/**
* @param array $keywords
*/
public function setKeywords(array $keywords)
{
$this->keywords = $keywords;
}
/**
* @param string $templateId
*/
public function setTemplateId(string $templateId)
{
$this->templateId = $templateId;
}
/**
* @param string $formId
*/
public function setFormId(string $formId)
{
$this->formId = $formId;
}
/**
* @param string $openId
*/
public function setOpenId(string $openId)
{
$this->openId = $openId;
}
/**
* @param string $defaultUrl
*/
public function setDefaultUrl(string $defaultUrl)
{
$this->defaultUrl = $defaultUrl;
}
/**
* @param string $page
*/
public function setPage(string $page)
{
$this->page = $page;
}
/**
* @param string $emphasis_keyword
*/
public function setEmphasisKeyword(string $emphasis_keyword)
{
$this->emphasis_keyword = $emphasis_keyword;
}
/**
* @param $index
* @param $context
2019-07-15 15:19:07 +08:00
* @param $color
2019-07-12 19:25:47 +08:00
*/
2019-07-15 15:19:07 +08:00
public function replaceKeyword($index, $context, $color = '')
2019-07-12 19:25:47 +08:00
{
2019-07-15 15:19:07 +08:00
if (empty($color)) {
$color = '#000';
}
2019-07-12 19:25:47 +08:00
$this->keywords['keyword' . $index] = [
2019-07-15 15:19:07 +08:00
'value' => $context,
'color' => $color
2019-07-12 19:25:47 +08:00
];
}
/**
2019-07-15 15:19:07 +08:00
* @param $color
2019-07-12 19:25:47 +08:00
* @param $context
*/
2019-07-15 15:19:07 +08:00
public function addKeyword($context, $color = null)
2019-07-12 19:25:47 +08:00
{
2019-07-15 15:19:07 +08:00
if (empty($color)) {
$color = '#000';
}
2019-07-12 19:25:47 +08:00
$this->keywords['keyword' . count($this->keywords)] = [
2019-07-15 15:19:07 +08:00
'value' => $context,
'color' => $color
2019-07-12 19:25:47 +08:00
];
}
/**
* @param string $access
* @return Result
* @throws \Exception
*
* 奴隶交易通知
*/
public function sendTemplate(string $access)
{
2019-07-15 15:20:32 +08:00
$url = $this->sendUrl . '?access_token=' . $access;
2019-07-12 19:25:47 +08:00
$params = [
"touser" => $this->openId,
"template_id" => $this->templateId,
"page" => $this->page,
"form_id" => $this->formId,
"data" => $this->keywords,
];
if (!empty($this->emphasis_keyword)) {
$params['emphasis_keyword'] = $this->emphasis_keyword;
}
$header = ['content-type' => 'application/json'];
return WxClient::post($url, $params, NULL, $header)
->append('postBody', $params);
}
2019-07-12 18:51:12 +08:00
}