add clear
This commit is contained in:
@@ -1,20 +0,0 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace wchat\base;
|
||||
|
||||
|
||||
/**
|
||||
* Interface Access_Token
|
||||
* @package wchat\base
|
||||
*/
|
||||
interface Access_Token
|
||||
{
|
||||
|
||||
/**
|
||||
* @param false $get_token
|
||||
* @return mixed
|
||||
*/
|
||||
public function generateAccess_token($get_token = false);
|
||||
|
||||
}
|
||||
+72
-22
@@ -4,22 +4,25 @@
|
||||
namespace wchat\base;
|
||||
|
||||
|
||||
use wchat\common\Miniprogarampage;
|
||||
use Kiri\Client;
|
||||
use wchat\common\Multiprogramming;
|
||||
use wchat\common\Result;
|
||||
|
||||
/**
|
||||
* Class Subject
|
||||
* @package wchat\base
|
||||
*/
|
||||
abstract class Subject extends Miniprogarampage
|
||||
abstract class Subject extends Multiprogramming
|
||||
{
|
||||
|
||||
|
||||
private $keywords = [];
|
||||
private $templateId = '';
|
||||
private $openId = '';
|
||||
private $page = 'pages/index/index';
|
||||
private $emphasis_keyword = '';
|
||||
private array $keywords = [];
|
||||
private string $templateId = '';
|
||||
private string $openId = '';
|
||||
private string $page = 'pages/index/index';
|
||||
private string $emphasis_keyword = '';
|
||||
private string $oac_appid = '';
|
||||
private string $use_robot = '';
|
||||
|
||||
/**
|
||||
* @param array $keywords
|
||||
@@ -64,9 +67,9 @@ abstract class Subject extends Miniprogarampage
|
||||
/**
|
||||
* @param $index
|
||||
* @param $context
|
||||
* @param $color
|
||||
* @param string $color
|
||||
*/
|
||||
public function replaceKeyword($index, $context, $color = '')
|
||||
public function replaceKeyword($index, $context, string $color = '')
|
||||
{
|
||||
if (empty($color)) {
|
||||
$color = '#000';
|
||||
@@ -95,35 +98,82 @@ abstract class Subject extends Miniprogarampage
|
||||
|
||||
abstract public function getUrl();
|
||||
|
||||
abstract public function getHost();
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getOacAppid(): string
|
||||
{
|
||||
return $this->oac_appid;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $oac_appid
|
||||
*/
|
||||
public function setOacAppid(string $oac_appid): void
|
||||
{
|
||||
$this->oac_appid = $oac_appid;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getUseRobot(): string
|
||||
{
|
||||
return $this->use_robot;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $use_robot
|
||||
*/
|
||||
public function setUseRobot(string $use_robot): void
|
||||
{
|
||||
$this->use_robot = $use_robot;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return Result
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function sendTemplate()
|
||||
public function sendTemplate(): Result
|
||||
{
|
||||
$url = $this->getUrl() . '?access_token=' . $this->config->getAccessToken();
|
||||
$access_token = $this->config->getAccessToken();
|
||||
if (empty($access_token)) {
|
||||
throw new \Exception('request access_token con\'t null.');
|
||||
}
|
||||
$params = [
|
||||
"touser" => $this->openId,
|
||||
"template_id" => $this->templateId,
|
||||
"page" => $this->page,
|
||||
"data" => $this->keywords,
|
||||
];
|
||||
|
||||
if (!empty($this->emphasis_keyword)) {
|
||||
$params['emphasis_keyword'] = $this->emphasis_keyword;
|
||||
}
|
||||
if (!empty($this->oac_appid)) {
|
||||
$params['oac_appid'] = $this->oac_appid;
|
||||
}
|
||||
if (!empty($this->use_robot)) {
|
||||
$params['use_robot'] = $this->use_robot;
|
||||
}
|
||||
$this->reset($result);
|
||||
|
||||
$params = json_encode($params, JSON_UNESCAPED_UNICODE);
|
||||
$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();
|
||||
|
||||
$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', $params);
|
||||
|
||||
return $this->reset($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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+30
-23
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user