add clear
This commit is contained in:
+39
-32
@@ -8,19 +8,20 @@
|
||||
|
||||
namespace wchat\wx;
|
||||
|
||||
use Kiri\Client;
|
||||
use wchat\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 = [];
|
||||
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 = [];
|
||||
|
||||
|
||||
/**
|
||||
@@ -58,9 +59,9 @@ class PublicTemplate extends SmallProgram
|
||||
/**
|
||||
* @param $name
|
||||
* @param $context
|
||||
* @param $color
|
||||
* @param string $color
|
||||
*/
|
||||
public function replaceKeyword($name, $context, $color = '')
|
||||
public function replaceKeyword($name, $context, string $color = '')
|
||||
{
|
||||
$this->keywords[$name] = ['value' => $context, 'color' => $color];
|
||||
}
|
||||
@@ -84,9 +85,10 @@ class PublicTemplate extends SmallProgram
|
||||
|
||||
/**
|
||||
* @param $context
|
||||
* @param $color
|
||||
* @param string $color
|
||||
* @return void
|
||||
*/
|
||||
public function setFirst($context, $color = '#f00')
|
||||
public function setFirst($context, string $color = '#f00'): void
|
||||
{
|
||||
$this->first = [
|
||||
'value' => $context,
|
||||
@@ -96,9 +98,10 @@ class PublicTemplate extends SmallProgram
|
||||
|
||||
/**
|
||||
* @param $context
|
||||
* @param $color
|
||||
* @param string $color
|
||||
* @return void
|
||||
*/
|
||||
public function setRemark($context, $color = '#000')
|
||||
public function setRemark($context, string $color = '#000'): void
|
||||
{
|
||||
$this->remark = [
|
||||
'value' => $context,
|
||||
@@ -108,12 +111,13 @@ class PublicTemplate extends SmallProgram
|
||||
|
||||
/**
|
||||
* @param $appid
|
||||
* @param $pagepath
|
||||
* @param string $pagepath
|
||||
* @return void
|
||||
*/
|
||||
public function setMiniprogram($appid, $pagepath)
|
||||
public function setMiniprogram($appid, string $pagepath): void
|
||||
{
|
||||
$this->miniprogram = [
|
||||
'appid' => $appid,
|
||||
'appid' => $appid,
|
||||
'pagepath' => $pagepath
|
||||
];
|
||||
}
|
||||
@@ -124,34 +128,37 @@ class PublicTemplate extends SmallProgram
|
||||
*
|
||||
* 奴隶交易通知
|
||||
*/
|
||||
public function sendTemplate()
|
||||
public function sendTemplate(): Result
|
||||
{
|
||||
$url = $this->sendUrl . '?access_token=' . $this->getAccessToken();
|
||||
$url = $this->sendUrl . '?access_token=' . $this->config->getAccessToken();
|
||||
|
||||
$keywords = $this->keywords;
|
||||
$keywords['first'] = $this->first;
|
||||
$keywords['remark'] = $this->remark;
|
||||
|
||||
$default = [
|
||||
"touser" => $this->openId,
|
||||
"touser" => $this->openId,
|
||||
"template_id" => $this->templateId,
|
||||
"url" => $this->defaultUrl,
|
||||
"data" => $keywords,
|
||||
"url" => $this->defaultUrl,
|
||||
"data" => $keywords,
|
||||
];
|
||||
|
||||
if (!empty($this->miniprogram)) {
|
||||
$default['miniprogram'] = $this->miniprogram;
|
||||
}
|
||||
|
||||
$params = json_encode($default, JSON_UNESCAPED_UNICODE);
|
||||
$client = new Client('api.weixin.qq.com', 443, true);
|
||||
$client->withHeader(['Content-Type' => 'application/json']);
|
||||
$client->post($url, $default);
|
||||
$client->close();
|
||||
|
||||
$this->request->setIsSSL(true);
|
||||
$this->request->addHeader('Content-Type', 'application/json');
|
||||
$this->request->setErrorField('errcode');
|
||||
$this->request->setErrorMsgField('errmsg');
|
||||
$result = $this->request->post($url, $params);
|
||||
$result->append('postBody', $default);
|
||||
|
||||
return $result;
|
||||
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
|
||||
return new Result(code: 505, message: 'network error.');
|
||||
}
|
||||
$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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user