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
+56 -37
View File
@@ -4,6 +4,7 @@
namespace wchat\wx;
use Kiri\Client;
use wchat\common\Result;
/**
@@ -13,59 +14,70 @@ use wchat\common\Result;
class SecCheck extends SmallProgram
{
private $_url = 'https://api.weixin.qq.com/wxa/img_sec_check?access_token=';
private string $_url = '/wxa/img_sec_check?access_token=';
private $_msgUrl = '/wxa/msg_sec_check?access_token=';
private string $_msgUrl = '/wxa/msg_sec_check?access_token=';
private $_mediaCheckAsync = '/wxa/media_check_async?access_token=';
private string $_mediaCheckAsync = '/wxa/media_check_async?access_token=';
const MEDIA_VIDEO = 1;
const MEDIA_IMAGE = 1;
/**
* @param $path
* @return array|Result|mixed
* @param string $path
* @return Result
*/
public function image($path = '')
public function image(string $path = ''): Result
{
if (!file_exists($path)) {
return $this->sendError('文件不存在', 404);
}
$this->request->setUseSwoole(false);
$this->request->setIsSSL(true);
$this->request->setHeader('Content-Type', 'multipart/form-data');
$this->request->setErrorField('errcode');
$this->request->setErrorMsgField('errmsg');
return $this->request->upload($this->_url . $this->config->getAccessToken(), ['media' => new \CURLFile($path)]);
$access_token = $this->config->getAccessToken();
$client = new Client('api.weixin.qq.com', 443, true);
$client->withHeader(['Content-Type' => 'multipart/form-data']);
$client->upload($this->_url . '?access_token=' . $access_token, [
'media' => new \CURLFile($path)
]);
$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 $url
* @param string $url
* @param int $type
* @return mixed
* @throws
*/
public function mediaAsync($url, $type = SecCheck::MEDIA_IMAGE)
public function mediaAsync(string $url, int $type = SecCheck::MEDIA_IMAGE): Result
{
if (!in_array($type, [self::MEDIA_IMAGE, self::MEDIA_VIDEO])) {
throw new \Exception('暂不支持的文件类型');
}
$this->request->setIsSSL(true);
$this->request->setHost('api.weixin.qq.com');
$this->request->setHeader('Content-Type', 'application/json');
$this->request->setErrorField('errcode');
$this->request->setErrorMsgField('errmsg');
$requestUrl = $this->_mediaCheckAsync . $this->config->getAccessToken();
$response = $this->request->post($requestUrl, [
'media_url' => $url,
'media_type' => $type
]);
if (!$response->isResultsOK()) {
throw new \Exception($response->getMessage());
$client = new Client('api.weixin.qq.com', 443, true);
$client->withHeader(['Content-Type' => 'application/json']);
$client->post($requestUrl, ['media_url' => $url, 'media_type' => $type]);
$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']);
} else {
return new Result(code: 0, data: $body);
}
return $response->getData('trace_id');
}
@@ -73,7 +85,7 @@ class SecCheck extends SmallProgram
* @param $params
* @return ContentAsyncCheck|null
*/
public function readByEvent($params)
public function readByEvent($params): ?ContentAsyncCheck
{
return ContentAsyncCheck::instance($params);
}
@@ -81,22 +93,29 @@ class SecCheck extends SmallProgram
/**
* @param $content
* @return array|Result|mixed
* @return Result
*/
public function text($content)
public function text($content): Result
{
if (empty($content)) {
return $this->sendError('文件不存在', 404);
}
$this->request->setIsSSL(true);
$this->request->setHost('api.weixin.qq.com');
$this->request->setHeader('Content-Type', 'application/json');
$this->request->setErrorField('errcode');
$this->request->setErrorMsgField('errmsg');
$requestUrl = $this->_msgUrl . $this->config->getAccessToken();
$requestUrl = $this->_msgUrl . $this->config->getAccessToken();
$client = new Client('api.weixin.qq.com', 443, true);
$client->withHeader(['Content-Type' => 'application/json']);
$client->post($requestUrl, ['content' => $content]);
$client->close();
return $this->request->post($requestUrl, ['content' => $content]);
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);
}
}
}