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
|
|
|
|
2022-09-09 16:42:55 +08:00
|
|
|
use Kiri\Client;
|
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
|
|
|
{
|
|
|
|
|
|
2023-11-14 00:45:54 +08:00
|
|
|
private array $keywords = [];
|
|
|
|
|
private string $templateId = '';
|
|
|
|
|
private array $first = [];
|
|
|
|
|
private array $remark = [];
|
|
|
|
|
private string $openId = '';
|
|
|
|
|
private string $defaultUrl = 'http://weixin.qq.com/download';
|
|
|
|
|
private string $sendUrl = '/cgi-bin/message/template/send';
|
|
|
|
|
private array $miniprogram = [];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param array $keywords
|
|
|
|
|
*/
|
|
|
|
|
public function setKeywords(array $keywords)
|
|
|
|
|
{
|
|
|
|
|
$this->keywords = $keywords;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $templateId
|
|
|
|
|
*/
|
|
|
|
|
public function setTemplateId($templateId)
|
|
|
|
|
{
|
|
|
|
|
$this->templateId = $templateId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $openId
|
|
|
|
|
*/
|
|
|
|
|
public function setOpenId($openId)
|
|
|
|
|
{
|
|
|
|
|
$this->openId = $openId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $defaultUrl
|
|
|
|
|
*/
|
|
|
|
|
public function setDefaultUrl($defaultUrl)
|
|
|
|
|
{
|
|
|
|
|
$this->defaultUrl = $defaultUrl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $name
|
|
|
|
|
* @param $context
|
|
|
|
|
* @param string $color
|
|
|
|
|
*/
|
|
|
|
|
public function replaceKeyword($name, $context, string $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';
|
2023-11-07 15:13:11 +08:00
|
|
|
}
|
2023-11-14 00:45:54 +08:00
|
|
|
$this->keywords[$name] = [
|
|
|
|
|
'value' => $context,
|
|
|
|
|
'color' => $color
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $context
|
|
|
|
|
* @param string $color
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function setFirst($context, string $color = '#f00'): void
|
|
|
|
|
{
|
|
|
|
|
$this->first = [
|
|
|
|
|
'value' => $context,
|
|
|
|
|
'color' => $color
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $context
|
|
|
|
|
* @param string $color
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function setRemark($context, string $color = '#000'): void
|
|
|
|
|
{
|
|
|
|
|
$this->remark = [
|
|
|
|
|
'value' => $context,
|
|
|
|
|
'color' => $color
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $appid
|
|
|
|
|
* @param string $pagepath
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function setMiniprogram($appid, string $pagepath): void
|
|
|
|
|
{
|
|
|
|
|
$this->miniprogram = [
|
|
|
|
|
'appid' => $appid,
|
|
|
|
|
'pagepath' => $pagepath
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return Result
|
|
|
|
|
* @throws \Exception
|
|
|
|
|
*
|
|
|
|
|
* 奴隶交易通知
|
|
|
|
|
*/
|
|
|
|
|
public function sendTemplate(): Result
|
|
|
|
|
{
|
|
|
|
|
$url = $this->sendUrl . '?access_token=' . $this->payConfig->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;
|
|
|
|
|
}
|
2023-11-14 12:39:28 +08:00
|
|
|
return $this->post('api.weixin.qq.com', $url, $default);
|
2023-11-14 00:45:54 +08:00
|
|
|
}
|
2019-08-21 11:54:20 +08:00
|
|
|
}
|