Files
kiri-wchat/wx/PublicTemplate.php
T

149 lines
3.4 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
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;
}
/**
2023-11-14 14:10:36 +08:00
* @param string $templateId
2023-11-14 00:45:54 +08:00
*/
2023-11-14 14:10:36 +08:00
public function setTemplateId(string $templateId)
2023-11-14 00:45:54 +08:00
{
$this->templateId = $templateId;
}
/**
2023-11-14 14:10:36 +08:00
* @param string $openId
2023-11-14 00:45:54 +08:00
*/
2023-11-14 14:10:36 +08:00
public function setOpenId(string $openId)
2023-11-14 00:45:54 +08:00
{
$this->openId = $openId;
}
/**
2023-11-14 14:10:36 +08:00
* @param string $defaultUrl
2023-11-14 00:45:54 +08:00
*/
2023-11-14 14:10:36 +08:00
public function setDefaultUrl(string $defaultUrl)
2023-11-14 00:45:54 +08:00
{
$this->defaultUrl = $defaultUrl;
}
/**
2023-11-14 14:10:36 +08:00
* @param string $name
* @param string $context
2023-11-14 00:45:54 +08:00
* @param string $color
*/
2023-11-14 14:10:36 +08:00
public function replaceKeyword(string $name, string $context, string $color = '')
2023-11-14 00:45:54 +08:00
{
$this->keywords[$name] = ['value' => $context, 'color' => $color];
}
/**
2023-11-14 14:10:36 +08:00
* @param string $name
* @param string $context
2023-11-14 00:45:54 +08:00
* @param null $color
*/
2023-11-14 14:10:36 +08:00
public function addKeyword(string $name, string $context, $color = null)
2023-11-14 00:45:54 +08:00
{
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
];
}
/**
2023-11-14 14:10:36 +08:00
* @param string $context
2023-11-14 00:45:54 +08:00
* @param string $color
* @return void
*/
2023-11-14 14:10:36 +08:00
public function setFirst(string $context, string $color = '#f00'): void
2023-11-14 00:45:54 +08:00
{
$this->first = [
'value' => $context,
'color' => $color
];
}
/**
2023-11-14 14:10:36 +08:00
* @param string $context
2023-11-14 00:45:54 +08:00
* @param string $color
* @return void
*/
2023-11-14 14:10:36 +08:00
public function setRemark(string $context, string $color = '#000'): void
2023-11-14 00:45:54 +08:00
{
$this->remark = [
'value' => $context,
'color' => $color
];
}
/**
2023-11-14 14:10:36 +08:00
* @param string $appid
2023-11-14 00:45:54 +08:00
* @param string $pagepath
* @return void
*/
2023-11-14 14:10:36 +08:00
public function setMiniprogram(string $appid, string $pagepath): void
2023-11-14 00:45:54 +08:00
{
$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
}