2019-12-19 15:00:25 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
2020-03-05 12:41:49 +08:00
|
|
|
namespace wchat\wx;
|
2019-12-19 15:00:25 +08:00
|
|
|
|
|
|
|
|
|
2020-03-05 12:41:49 +08:00
|
|
|
use wchat\common\Result;
|
2019-12-19 15:00:25 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class SecCheck
|
|
|
|
|
* @package wchat
|
|
|
|
|
*/
|
|
|
|
|
class SecCheck extends SmallProgram
|
|
|
|
|
{
|
|
|
|
|
|
2021-02-05 12:41:44 +08:00
|
|
|
private $_url = '/wxa/img_sec_check?access_token=';
|
2019-12-19 15:00:25 +08:00
|
|
|
|
2021-02-05 12:41:44 +08:00
|
|
|
private $_msgUrl = '/wxa/msg_sec_check?access_token=';
|
2019-12-19 15:00:25 +08:00
|
|
|
|
2021-02-05 12:41:44 +08:00
|
|
|
private $_mediaCheckAsync = '/wxa/media_check_async?access_token=';
|
2019-12-19 15:00:25 +08:00
|
|
|
|
|
|
|
|
const MEDIA_VIDEO = 1;
|
|
|
|
|
const MEDIA_IMAGE = 1;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $path
|
|
|
|
|
* @return array|Result|mixed
|
|
|
|
|
*/
|
|
|
|
|
public function image($path = '')
|
|
|
|
|
{
|
|
|
|
|
if (!file_exists($path)) {
|
|
|
|
|
return $this->sendError('文件不存在', 404);
|
|
|
|
|
}
|
|
|
|
|
$this->request->setUseSwoole(false);
|
2020-11-14 03:04:25 +08:00
|
|
|
$this->request->setIsSSL(true);
|
|
|
|
|
$this->request->setHost('api.weixin.qq.com');
|
2020-04-01 11:53:15 +08:00
|
|
|
$this->request->setHeader('Content-Type', 'multipart/form-data');
|
|
|
|
|
$this->request->setErrorField('errcode');
|
|
|
|
|
$this->request->setErrorMsgField('errmsg');
|
2020-08-12 17:29:51 +08:00
|
|
|
return $this->request->upload($this->_url . $this->config->getAccessToken(), ['media' => new \CURLFile($path)]);
|
2019-12-19 15:00:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $url
|
|
|
|
|
* @param int $type
|
|
|
|
|
* @return mixed
|
|
|
|
|
* @throws
|
|
|
|
|
*/
|
|
|
|
|
public function mediaAsync(string $url, $type = SecCheck::MEDIA_IMAGE)
|
|
|
|
|
{
|
|
|
|
|
if (!in_array($type, [self::MEDIA_IMAGE, self::MEDIA_VIDEO])) {
|
|
|
|
|
throw new \Exception('暂不支持的文件类型');
|
|
|
|
|
}
|
2020-11-14 03:04:25 +08:00
|
|
|
$this->request->setIsSSL(true);
|
|
|
|
|
$this->request->setHost('api.weixin.qq.com');
|
|
|
|
|
$this->request->setHeader('Content-Type', 'application/json');
|
2020-04-01 11:53:15 +08:00
|
|
|
$this->request->setErrorField('errcode');
|
|
|
|
|
$this->request->setErrorMsgField('errmsg');
|
2020-11-14 03:04:25 +08:00
|
|
|
|
|
|
|
|
$requestUrl = $this->_mediaCheckAsync . $this->config->getAccessToken();
|
|
|
|
|
$response = $this->request->post($requestUrl, [
|
2019-12-19 15:00:25 +08:00
|
|
|
'media_url' => $url,
|
|
|
|
|
'media_type' => $type
|
|
|
|
|
]);
|
|
|
|
|
if (!$response->isResultsOK()) {
|
|
|
|
|
throw new \Exception($response->getMessage());
|
|
|
|
|
}
|
|
|
|
|
return $response->getData('trace_id');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $params
|
|
|
|
|
* @return ContentAsyncCheck|null
|
|
|
|
|
*/
|
|
|
|
|
public function readByEvent($params)
|
|
|
|
|
{
|
|
|
|
|
return ContentAsyncCheck::instance($params);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $content
|
|
|
|
|
* @return array|Result|mixed
|
|
|
|
|
*/
|
|
|
|
|
public function text($content)
|
|
|
|
|
{
|
|
|
|
|
if (empty($content)) {
|
|
|
|
|
return $this->sendError('文件不存在', 404);
|
|
|
|
|
}
|
2020-11-14 03:04:25 +08:00
|
|
|
$this->request->setIsSSL(true);
|
|
|
|
|
$this->request->setHost('api.weixin.qq.com');
|
|
|
|
|
$this->request->setHeader('Content-Type', 'application/json');
|
2020-04-01 11:53:15 +08:00
|
|
|
$this->request->setErrorField('errcode');
|
|
|
|
|
$this->request->setErrorMsgField('errmsg');
|
2020-11-14 03:04:25 +08:00
|
|
|
|
|
|
|
|
$requestUrl = $this->_msgUrl . $this->config->getAccessToken();
|
|
|
|
|
|
|
|
|
|
return $this->request->post($requestUrl, ['content' => $content]);
|
2019-12-19 15:00:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|