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

165 lines
3.1 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
{
2022-09-09 16:42:55 +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 = [];
2019-08-21 11:54:20 +08:00
/**
* @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
2022-09-09 16:42:55 +08:00
* @param string $color
2019-08-21 11:54:20 +08:00
*/
2022-09-09 16:42:55 +08:00
public function replaceKeyword($name, $context, string $color = '')
2019-08-21 11:54:20 +08:00
{
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
2022-09-09 16:42:55 +08:00
* @param string $color
* @return void
2019-08-21 11:54:20 +08:00
*/
2022-09-09 16:42:55 +08:00
public function setFirst($context, string $color = '#f00'): void
2019-08-21 11:54:20 +08:00
{
2019-08-21 13:34:03 +08:00
$this->first = [
2019-08-21 11:54:20 +08:00
'value' => $context,
'color' => $color
];
}
/**
* @param $context
2022-09-09 16:42:55 +08:00
* @param string $color
* @return void
2019-08-21 11:54:20 +08:00
*/
2022-09-09 16:42:55 +08:00
public function setRemark($context, string $color = '#000'): void
2019-08-21 11:54:20 +08:00
{
2019-08-21 13:34:03 +08:00
$this->remark = [
2019-08-21 11:54:20 +08:00
'value' => $context,
'color' => $color
];
}
/**
* @param $appid
2022-09-09 16:42:55 +08:00
* @param string $pagepath
* @return void
2019-08-21 11:54:20 +08:00
*/
2022-09-09 16:42:55 +08:00
public function setMiniprogram($appid, string $pagepath): void
2019-08-21 11:54:20 +08:00
{
$this->miniprogram = [
2022-09-09 16:42:55 +08:00
'appid' => $appid,
2019-08-21 11:54:20 +08:00
'pagepath' => $pagepath
];
}
/**
* @return Result
* @throws \Exception
*
* 奴隶交易通知
*/
2022-09-09 16:42:55 +08:00
public function sendTemplate(): Result
2019-08-21 11:54:20 +08:00
{
2022-09-09 16:42:55 +08:00
$url = $this->sendUrl . '?access_token=' . $this->config->getAccessToken();
2019-08-21 11:54:20 +08:00
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 = [
2022-09-09 16:42:55 +08:00
"touser" => $this->openId,
2019-08-21 11:54:20 +08:00
"template_id" => $this->templateId,
2022-09-09 16:42:55 +08:00
"url" => $this->defaultUrl,
"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
2022-09-09 16:42:55 +08:00
$client = new Client('api.weixin.qq.com', 443, true);
$client->withHeader(['Content-Type' => 'application/json']);
$client->post($url, $default);
$client->close();
2019-08-21 11:54:20 +08:00
2022-09-09 16:42:55 +08:00
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
2023-08-18 19:59:46 +08:00
return new Result(code: 505, message: $client->getBody());
2022-09-09 16:42:55 +08:00
}
$body = json_decode($client->getBody(), true);
if (isset($body['errcode']) && $body['errcode'] != 0) {
return new Result(code: $body['errcode'], message: $body['errmsg']);
} else {
return new Result(code: 0, data: $body);
}
2019-08-21 11:54:20 +08:00
}
}