122 lines
3.1 KiB
PHP
122 lines
3.1 KiB
PHP
<?php
|
|
|
|
|
|
namespace wchat\wx;
|
|
|
|
|
|
use Kiri\Client;
|
|
use wchat\common\Result;
|
|
|
|
/**
|
|
* Class SecCheck
|
|
* @package wchat
|
|
*/
|
|
class SecCheck extends SmallProgram
|
|
{
|
|
|
|
private string $_url = '/wxa/img_sec_check?access_token=';
|
|
|
|
private string $_msgUrl = '/wxa/msg_sec_check?access_token=';
|
|
|
|
private string $_mediaCheckAsync = '/wxa/media_check_async?access_token=';
|
|
|
|
const MEDIA_VIDEO = 1;
|
|
const MEDIA_IMAGE = 1;
|
|
|
|
/**
|
|
* @param string $path
|
|
* @return Result
|
|
*/
|
|
public function image(string $path = ''): Result
|
|
{
|
|
if (!file_exists($path)) {
|
|
return $this->sendError('文件不存在', 404);
|
|
}
|
|
$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: $client->getBody());
|
|
}
|
|
$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 string $url
|
|
* @param int $type
|
|
* @return mixed
|
|
* @throws
|
|
*/
|
|
public function mediaAsync(string $url, int $type = SecCheck::MEDIA_IMAGE): Result
|
|
{
|
|
if (!in_array($type, [self::MEDIA_IMAGE, self::MEDIA_VIDEO])) {
|
|
throw new \Exception('暂不支持的文件类型');
|
|
}
|
|
$requestUrl = $this->_mediaCheckAsync . $this->config->getAccessToken();
|
|
$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: $client->getBody());
|
|
}
|
|
$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 $params
|
|
* @return ContentAsyncCheck|null
|
|
*/
|
|
public function readByEvent($params): ?ContentAsyncCheck
|
|
{
|
|
return ContentAsyncCheck::instance($params);
|
|
}
|
|
|
|
|
|
/**
|
|
* @param $content
|
|
* @return Result
|
|
*/
|
|
public function text($content): Result
|
|
{
|
|
if (empty($content)) {
|
|
return $this->sendError('文件不存在', 404);
|
|
}
|
|
$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();
|
|
|
|
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
|
|
return new Result(code: 505, message: $client->getBody());
|
|
}
|
|
$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);
|
|
}
|
|
}
|
|
|
|
}
|