add clear

This commit is contained in:
2022-09-09 16:42:55 +08:00
parent ad76b45b45
commit 7273fe1ce5
63 changed files with 2625 additions and 5287 deletions
+30 -23
View File
@@ -2,22 +2,23 @@
namespace wchat\base;
use wchat\common\Miniprogarampage;
use Kiri\Client;
use wchat\common\Multiprogramming;
use wchat\common\Result;
/**
* Class Template
*/
abstract class Template extends Miniprogarampage
abstract class Template extends Multiprogramming
{
private $keywords = [];
private $templateId = '';
private $formId = '';
private $openId = '';
private $page = 'pages/index/index';
private $emphasis_keyword = '';
private array $keywords = [];
private string $templateId = '';
private string $formId = '';
private string $openId = '';
private string $page = 'pages/index/index';
private string $emphasis_keyword = '';
/**
* @param array $keywords
@@ -72,7 +73,7 @@ abstract class Template extends Miniprogarampage
* @param $context
* @param $color
*/
public function replaceKeyword($index, $context, $color = '')
public function replaceKeyword($index, $context, string $color = '')
{
if (empty($color)) {
$color = '#000';
@@ -101,20 +102,21 @@ abstract class Template extends Miniprogarampage
abstract public function getUrl();
abstract public function getHost();
/**
* @return Result
* @throws \Exception
*
* 奴隶交易通知
*/
public function sendTemplate()
public function sendTemplate(): Result
{
$access_token = $this->config->getAccessToken();
if (empty($access_token)) {
throw new \Exception('request access_token con\'t null.');
}
$url = $this->getUrl() . '?access_token=' . $access_token;
$params = [
"touser" => $this->openId,
"template_id" => $this->templateId,
@@ -126,30 +128,35 @@ abstract class Template extends Miniprogarampage
if (!empty($this->emphasis_keyword)) {
$params['emphasis_keyword'] = $this->emphasis_keyword;
}
$params = json_encode($params, JSON_UNESCAPED_UNICODE);
$this->request->setIsSSL(true);
$this->request->addHeader('Content-Type', 'application/json; charset=utf-8');
$this->request->setErrorField('errcode');
$this->request->setErrorMsgField('errmsg');
$result = $this->request->post($url, $params);
$result->append('postBody', $params);
return $this->reset($result);
$this->reset($result);
$client = new Client($this->getHost(), 443, true);
$client->withHeader(['Content-Type' => 'application/json; charset=utf-8']);
$client->post($this->getUrl() . '?access_token=' . $access_token, $params);
$client->close();
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']);
}
return new Result(code: 0, data: $body);
}
/**
* @param $result
* @return mixed
* @return void
*/
private function reset($result)
private function reset($result): void
{
$this->openId = '';
$this->keywords = [];
$this->formId = '';
$this->templateId = '';
$this->page = '';
return $result;
}
}