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
+42 -30
View File
@@ -4,6 +4,7 @@
namespace wchat\qq;
use Kiri\Client;
use wchat\common\Result;
/**
@@ -13,57 +14,68 @@ use wchat\common\Result;
class SecCheck extends SmallProgram
{
private $_url = 'https://api.q.qq.com/api/json/security/ImgSecCheck?access_token=';
private string $_url = '/api/json/security/ImgSecCheck?access_token=';
private $_msgUrl = '/api/json/security/MsgSecCheck?access_token=';
private string $_msgUrl = '/api/json/security/MsgSecCheck?access_token=';
/**
* @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->setHeader('Content-Type', 'multipart/form-data');
$this->request->addHeader('Host', 'api.q.qq.com');
$this->request->setErrorField('errCode');
$this->request->setErrorMsgField('errMsg');
$this->request->setAgent('');
$real_path = new \CURLFile($path);
return $this->request->upload($this->_url . $this->config->getAccessToken(), [
'appid' => $this->config->getAppid(), 'media' => new \CURLFile($path)
, 'form-data[filename]' => $path, 'form-data[content-type]' => $real_path->getMimeType()
]);
$data = [
'appid' => $this->config->getAppid(),
'media' => $real_path,
'form-data[filename]' => $path,
'form-data[content-type]' => $real_path->getMimeType()
];
$client = new Client('api.q.qq.com', 443, true);
$client->withHeader(['Content-Type' => 'multipart/form-data']);
$client->upload($path, $data);
$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);
}
}
/**
* @param $content
* @return array|Result|mixed
* @param string $content
* @return Result
*/
public function text($content)
public function text(string $content): Result
{
if (empty($content)) {
return $this->sendError('文件不存在', 404);
}
$this->request->setUseSwoole(true);
$this->request->addHeader('Content-Type', 'application/json');
$this->request->setErrorField('errCode');
$this->request->setErrorMsgField('errMsg');
$this->request->setData([
'appid' => $this->config->getAppid(),
'content' => $content
]);
$this->request->setHost('api.q.qq.com');
$this->request->setIsSSL(true);
$url = '/' . ltrim($this->_url, '/') . $this->config->getAccessToken();
$url = '/' . ltrim($this->_msgUrl, '/') . $this->config->getAccessToken();
return $this->request->post($url);
$client = new Client('api.q.qq.com', 443, true);
$client->withHeader(['Content-Type' => 'application/json']);
$client->post($url, ['appid' => $this->config->getAppid(), 'content' => $content]);
$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);
}
}
}