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
+257 -243
View File
@@ -4,7 +4,7 @@
namespace wchat\wx;
use Exception;
use wchat\common\HttpClient;
use Kiri\Client;
use wchat\common\Result;
@@ -15,287 +15,301 @@ use wchat\common\Result;
class Message extends SmallProgram
{
private array $msgData = [];
private array $msgData = [];
/**
* @param $openid
*/
public function setOpenid($openid)
{
$this->msgData['touser'] = $openid;
}
/**
* @param $openid
*/
public function setOpenid($openid)
{
$this->msgData['touser'] = $openid;
}
/**
* @param $content
* @return Result
* @throws Exception
*/
public function sendTextNews($content)
{
$this->msgData['msgtype'] = 'text';
$this->msgData['text'] = ['content' => $content];
/**
* @param $content
* @return Result
* @throws Exception
*/
public function sendTextNews($content): Result
{
$this->msgData['msgtype'] = 'text';
$this->msgData['text'] = ['content' => $content];
return $this->sendKefuMsg();
}
return $this->sendKefuMsg();
}
/**
* @param $media_id
* @return Result
* @throws Exception
*/
public function sendImageNews($media_id)
{
$this->msgData['msgtype'] = 'image';
$this->msgData['image'] = ['media_id' => $media_id];
/**
* @param $media_id
* @return Result
* @throws Exception
*/
public function sendImageNews($media_id): Result
{
$this->msgData['msgtype'] = 'image';
$this->msgData['image'] = ['media_id' => $media_id];
return $this->sendKefuMsg();
}
return $this->sendKefuMsg();
}
/**
* @param $media_id
* @return Result
* @throws Exception
*/
public function sendVoiceNews($media_id)
{
$this->msgData['msgtype'] = 'voice';
$this->msgData['voice'] = ['media_id' => $media_id];
/**
* @param $media_id
* @return Result
* @throws Exception
*/
public function sendVoiceNews($media_id): Result
{
$this->msgData['msgtype'] = 'voice';
$this->msgData['voice'] = ['media_id' => $media_id];
return $this->sendKefuMsg();
}
return $this->sendKefuMsg();
}
/**
* @param $media_id
* @return Result
* @throws Exception
*/
public function sendMpNewsNews($media_id)
{
$this->msgData['msgtype'] = 'mpnews';
$this->msgData['mpnews'] = ['media_id' => $media_id];
/**
* @param $media_id
* @return Result
* @throws Exception
*/
public function sendMpNewsNews($media_id): Result
{
$this->msgData['msgtype'] = 'mpnews';
$this->msgData['mpnews'] = ['media_id' => $media_id];
return $this->sendKefuMsg();
}
return $this->sendKefuMsg();
}
/**
* @param $title
* @param $description
* @param $url
* @param $picurl
* @return Result
* @throws Exception
*/
public function sendNewsNews($title, $description, $url, $picurl)
{
$this->msgData['msgtype'] = 'news';
$this->msgData['news'] = ['articles' => [[
'title' => $title,
'description' => $description,
'url' => $url,
'picurl' => $picurl
]]];
return $this->sendKefuMsg();
}
/**
* @param $title
* @param $description
* @param $url
* @param $picurl
* @return Result
* @throws Exception
*/
public function sendNewsNews($title, $description, $url, $picurl): Result
{
$this->msgData['msgtype'] = 'news';
$this->msgData['news'] = [
'articles' => [
[
'title' => $title,
'description' => $description,
'url' => $url,
'picurl' => $picurl
]
]
];
return $this->sendKefuMsg();
}
/**
* @param $title
* @return Result
* @throws Exception
*/
public function sendCardNews($title)
{
$this->msgData['msgtype'] = 'wxcard';
$this->msgData['wxcard'] = ['card_id' => $title];
/**
* @param $title
* @return Result
* @throws Exception
*/
public function sendCardNews($title): Result
{
$this->msgData['msgtype'] = 'wxcard';
$this->msgData['wxcard'] = ['card_id' => $title];
return $this->sendKefuMsg();
}
return $this->sendKefuMsg();
}
/**
* @param $media_id
* @param $thumb_media_id
* @param $title
* @param $description
* @return Result
* @throws Exception
*/
public function sendVideoNews($media_id, $thumb_media_id, $title, $description)
{
$this->msgData['msgtype'] = 'video';
$this->msgData['video'] = ['media_id' => [
'media_id' => $media_id,
'thumb_media_id' => $thumb_media_id,
'title' => $title,
'description' => $description
]];
return $this->sendKefuMsg();
}
/**
* @param $media_id
* @param $thumb_media_id
* @param $title
* @param $description
* @return Result
* @throws Exception
*/
public function sendVideoNews($media_id, $thumb_media_id, $title, $description): Result
{
$this->msgData['msgtype'] = 'video';
$this->msgData['video'] = [
'media_id' => [
'media_id' => $media_id,
'thumb_media_id' => $thumb_media_id,
'title' => $title,
'description' => $description
]
];
return $this->sendKefuMsg();
}
/**
* @param $musicurl
* @param $hqmusicurl
* @param $thumb_media_id
* @param $title
* @param $description
* @return Result
* @throws Exception
*/
public function sendMusicNews($musicurl, $hqmusicurl, $thumb_media_id, $title, $description)
{
$this->msgData['msgtype'] = 'music';
$this->msgData['music'] = [
'title' => $title,
'description' => $description,
'musicurl' => $musicurl,
'hqmusicurl' => $hqmusicurl,
'thumb_media_id' => $thumb_media_id
];
return $this->sendKefuMsg();
}
/**
* @param $musicurl
* @param $hqmusicurl
* @param $thumb_media_id
* @param $title
* @param $description
* @return Result
* @throws Exception
*/
public function sendMusicNews($musicurl, $hqmusicurl, $thumb_media_id, $title, $description): Result
{
$this->msgData['msgtype'] = 'music';
$this->msgData['music'] = [
'title' => $title,
'description' => $description,
'musicurl' => $musicurl,
'hqmusicurl' => $hqmusicurl,
'thumb_media_id' => $thumb_media_id
];
return $this->sendKefuMsg();
}
/**
* @param $head_content
* @param $tail_content
* @param array $menus
* @return Result
* @throws Exception
*/
public function sendMenuNews($head_content, $tail_content, array $menus = [])
{
$this->msgData['msgtype'] = 'msgmenu';
$this->msgData['msgmenu'] = [
'head_content' => $head_content,
'tail_content' => $tail_content,
];
if (empty($menus) || !is_array($menus) || count($menus) < 2) {
throw new Exception('菜单选项必须有2个');
}
foreach ($menus as $key => $val) {
$this->addNewsMenu($val['id'], $val['name']);
}
return $this->sendKefuMsg();
}
/**
* @param $head_content
* @param $tail_content
* @param array $menus
* @return Result
* @throws Exception
*/
public function sendMenuNews($head_content, $tail_content, array $menus = []): Result
{
$this->msgData['msgtype'] = 'msgmenu';
$this->msgData['msgmenu'] = [
'head_content' => $head_content,
'tail_content' => $tail_content,
];
if (empty($menus) || !is_array($menus) || count($menus) < 2) {
throw new Exception('菜单选项必须有2个');
}
foreach ($menus as $val) {
$this->addNewsMenu($val['id'], $val['name']);
}
return $this->sendKefuMsg();
}
private int $index = 0;
private int $index = 0;
/**
* @param $id
* @param $menuName
* @return $this
*/
public function addNewsMenu($id, $menuName)
{
$lists['id'] = $id;
$lists['content'] = $menuName;
$this->msgData['msgmenu']['list'][$this->index] = $lists;
++$this->index;
return $this;
}
/**
* @param $id
* @param $menuName
* @return $this
*/
public function addNewsMenu($id, $menuName): static
{
$lists['id'] = $id;
$lists['content'] = $menuName;
$this->msgData['msgmenu']['list'][$this->index] = $lists;
++$this->index;
return $this;
}
/**
* @param $title
* @param $appid
* @param $pagepath
* @param $thumb_media_id
* @return Result
* @throws Exception
*/
public function sendMiniprogrampageNews($title, $appid, $pagepath, $thumb_media_id)
{
$this->msgData['msgtype'] = 'msgmenu';
$this->msgData['miniprogrampage'] = [
'title' => $title,
'appid' => $appid,
'pagepath' => $pagepath,
'thumb_media_id' => $thumb_media_id,
];
return $this->sendKefuMsg();
}
/**
* @param $title
* @param $appid
* @param $pagepath
* @param $thumb_media_id
* @return Result
* @throws Exception
*/
public function sendMiniprogrampageNews($title, $appid, $pagepath, $thumb_media_id): Result
{
$this->msgData['msgtype'] = 'msgmenu';
$this->msgData['miniprogrampage'] = [
'title' => $title,
'appid' => $appid,
'pagepath' => $pagepath,
'thumb_media_id' => $thumb_media_id,
];
return $this->sendKefuMsg();
}
/**
* @param $filePath
* @param $type
* @param bool $isPermanent
* @param $title
* @param $introduction
* @return mixed
* @throws Exception
*/
public function upload($filePath, $type, $isPermanent = false, $title = '', $introduction = '')
{
if (!file_exists($filePath)) {
throw new Exception('文件不存在');
}
/**
* @param $filePath
* @param $type
* @param bool $isPermanent
* @param string $title
* @param string $introduction
* @return mixed
* @throws Exception
*/
public function upload($filePath, $type, bool $isPermanent = false, string $title = '', string $introduction = ''): Result
{
if (!file_exists($filePath)) {
throw new Exception('文件不存在');
}
if (!in_array($type, ['image', 'voice', 'video', 'thumb'])) {
throw new Exception('暂不支持的文件类型');
}
if (!in_array($type, ['image', 'voice', 'video', 'thumb'])) {
throw new Exception('暂不支持的文件类型');
}
$token = $this->getAccessToken();
if ($isPermanent) {
$url = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token={$token}&type={$type}";
} else {
$url = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token={$token}&type={$type}";
}
$token = $this->config->getAccessToken();
if ($isPermanent) {
$url = "/cgi-bin/material/add_material?access_token={$token}&type={$type}";
} else {
$url = "/cgi-bin/media/upload?access_token={$token}&type={$type}";
}
$mime = mime_content_type($filePath);
$mime = mime_content_type($filePath);
$real_path = new \CURLFile(realpath($filePath));
$real_path = new \CURLFile(realpath($filePath));
$data = array("media" => $real_path, 'form-data[filename]' => $filePath, 'form-data[Content-Type]' => $mime);
if ($isPermanent && $mime == 'video/mp3') {
$data = ['media' => $real_path, 'description[title]' => $title, 'description[introduction]' => $introduction];
}
$data = ["media" => $real_path, 'form-data[filename]' => $filePath, 'form-data[Content-Type]' => $mime];
if ($isPermanent && $mime == 'video/mp3') {
$data = ['media' => $real_path, 'description[title]' => $title, 'description[introduction]' => $introduction];
}
$this->request->setMethod(HttpClient::POST);
$client = new Client('api.weixin.qq.com', 443, true);
$client->withHeader(['Content-Type' => 'multipart/form-data']);
$client->post($url, $data);
$client->close();
/** @var Result $body */
$data = $this->request->post($url, $data);
if (!$data->isResultsOK()) {
throw new Exception($data->getMessage());
}
$this->msgData = [];
return $data->getData();
}
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 array
*/
public function getContents()
{
return $this->msgData;
}
/**
* @return array
*/
public function getContents(): array
{
return $this->msgData;
}
/**
* @return Result
* @throws Exception
*/
private function sendKefuMsg()
{
$data = json_encode($this->msgData, JSON_UNESCAPED_UNICODE);
/**
* @return Result
* @throws Exception
*/
private function sendKefuMsg()
{
$url = '/cgi-bin/message/custom/send?access_token=' . $this->config->getAccessToken();
$url = '/cgi-bin/message/custom/send?access_token=' . $this->getAccessToken();
$this->request->setMethod(HttpClient::POST);
$this->request->setErrorField('errcode');
$this->request->setErrorMsgField('errmsg');
$this->request->setHost('api.weixin.qq.com');
$client = new Client('api.weixin.qq.com', 443, true);
$client->withHeader(['Content-Type' => 'application/json']);
$client->post($url, $this->msgData);
$client->close();
$this->request->addHeader('Content-Type', 'application/json');
$this->msgData = [];
/** @var Result $body */
$body = $this->request->post($url, $data);
if (!$body->isResultsOK()) {
throw new Exception($body->getMessage());
}
return $body;
}
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);
}
}
}