2019-07-17 17:17:37 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
2020-03-05 12:41:49 +08:00
|
|
|
namespace wchat\wx;
|
2019-07-17 17:17:37 +08:00
|
|
|
|
2020-11-14 03:04:25 +08:00
|
|
|
use Exception;
|
2020-03-05 12:41:49 +08:00
|
|
|
use wchat\common\HttpClient;
|
|
|
|
|
use wchat\common\Result;
|
2020-11-14 02:17:15 +08:00
|
|
|
|
2020-11-14 03:04:25 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class Message
|
|
|
|
|
* @package wchat\wx
|
|
|
|
|
*/
|
2019-11-11 18:14:47 +08:00
|
|
|
class Message extends SmallProgram
|
2019-07-17 17:17:37 +08:00
|
|
|
{
|
2020-11-14 03:04:25 +08:00
|
|
|
|
2020-11-14 02:15:02 +08:00
|
|
|
private array $msgData = [];
|
2019-07-17 17:17:37 +08:00
|
|
|
|
|
|
|
|
|
2020-11-14 02:17:15 +08:00
|
|
|
/**
|
2021-04-06 15:20:39 +08:00
|
|
|
* @param $openid
|
2020-11-14 02:17:15 +08:00
|
|
|
*/
|
2021-04-06 15:20:39 +08:00
|
|
|
public function setOpenid($openid)
|
2020-11-14 02:17:15 +08:00
|
|
|
{
|
|
|
|
|
$this->msgData['touser'] = $openid;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2021-04-06 15:20:39 +08:00
|
|
|
* @param $content
|
2020-11-14 02:36:05 +08:00
|
|
|
* @return Result
|
2020-11-14 03:04:25 +08:00
|
|
|
* @throws Exception
|
2020-11-14 02:17:15 +08:00
|
|
|
*/
|
2021-04-06 15:20:39 +08:00
|
|
|
public function sendTextNews($content)
|
2020-11-14 02:17:15 +08:00
|
|
|
{
|
|
|
|
|
$this->msgData['msgtype'] = 'text';
|
2020-11-14 02:28:41 +08:00
|
|
|
$this->msgData['text'] = ['content' => $content];
|
2020-11-14 02:17:15 +08:00
|
|
|
|
2020-11-14 02:36:05 +08:00
|
|
|
return $this->sendKefuMsg();
|
2020-11-14 02:17:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2020-11-14 02:36:05 +08:00
|
|
|
* @param $media_id
|
|
|
|
|
* @return Result
|
2020-11-14 03:04:25 +08:00
|
|
|
* @throws Exception
|
2020-11-14 02:17:15 +08:00
|
|
|
*/
|
2021-04-06 15:20:39 +08:00
|
|
|
public function sendImageNews($media_id)
|
2020-11-14 02:17:15 +08:00
|
|
|
{
|
|
|
|
|
$this->msgData['msgtype'] = 'image';
|
2020-11-14 02:28:41 +08:00
|
|
|
$this->msgData['image'] = ['media_id' => $media_id];
|
2020-11-14 02:17:15 +08:00
|
|
|
|
2020-11-14 02:36:05 +08:00
|
|
|
return $this->sendKefuMsg();
|
2020-11-14 02:17:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2020-11-14 02:36:05 +08:00
|
|
|
* @param $media_id
|
|
|
|
|
* @return Result
|
2020-11-14 03:04:25 +08:00
|
|
|
* @throws Exception
|
2020-11-14 02:17:15 +08:00
|
|
|
*/
|
2021-04-06 15:20:39 +08:00
|
|
|
public function sendVoiceNews($media_id)
|
2020-11-14 02:17:15 +08:00
|
|
|
{
|
|
|
|
|
$this->msgData['msgtype'] = 'voice';
|
2020-11-14 02:28:41 +08:00
|
|
|
$this->msgData['voice'] = ['media_id' => $media_id];
|
2020-11-14 02:17:15 +08:00
|
|
|
|
2020-11-14 02:36:05 +08:00
|
|
|
return $this->sendKefuMsg();
|
2020-11-14 02:17:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2020-11-14 02:36:05 +08:00
|
|
|
* @param $media_id
|
|
|
|
|
* @return Result
|
2020-11-14 03:04:25 +08:00
|
|
|
* @throws Exception
|
2020-11-14 02:17:15 +08:00
|
|
|
*/
|
2021-04-06 15:20:39 +08:00
|
|
|
public function sendMpNewsNews($media_id)
|
2020-11-14 02:17:15 +08:00
|
|
|
{
|
|
|
|
|
$this->msgData['msgtype'] = 'mpnews';
|
2020-11-14 02:28:41 +08:00
|
|
|
$this->msgData['mpnews'] = ['media_id' => $media_id];
|
2020-11-14 02:17:15 +08:00
|
|
|
|
2020-11-14 02:36:05 +08:00
|
|
|
return $this->sendKefuMsg();
|
2020-11-14 02:17:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2021-04-06 15:20:39 +08:00
|
|
|
* @param $title
|
|
|
|
|
* @param $description
|
|
|
|
|
* @param $url
|
|
|
|
|
* @param $picurl
|
2020-11-14 02:36:05 +08:00
|
|
|
* @return Result
|
2020-11-14 03:04:25 +08:00
|
|
|
* @throws Exception
|
2020-11-14 02:17:15 +08:00
|
|
|
*/
|
2021-04-06 15:20:39 +08:00
|
|
|
public function sendNewsNews($title, $description, $url, $picurl)
|
2020-11-14 02:17:15 +08:00
|
|
|
{
|
|
|
|
|
$this->msgData['msgtype'] = 'news';
|
2020-11-14 02:28:41 +08:00
|
|
|
$this->msgData['news'] = ['articles' => [[
|
|
|
|
|
'title' => $title,
|
|
|
|
|
'description' => $description,
|
|
|
|
|
'url' => $url,
|
|
|
|
|
'picurl' => $picurl
|
|
|
|
|
]]];
|
2020-11-14 02:36:05 +08:00
|
|
|
return $this->sendKefuMsg();
|
2020-11-14 02:17:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2021-04-06 15:20:39 +08:00
|
|
|
* @param $title
|
2020-11-14 02:36:05 +08:00
|
|
|
* @return Result
|
2020-11-14 03:04:25 +08:00
|
|
|
* @throws Exception
|
2020-11-14 02:17:15 +08:00
|
|
|
*/
|
2021-04-06 15:20:39 +08:00
|
|
|
public function sendCardNews($title)
|
2020-11-14 02:17:15 +08:00
|
|
|
{
|
|
|
|
|
$this->msgData['msgtype'] = 'wxcard';
|
2020-11-14 02:28:41 +08:00
|
|
|
$this->msgData['wxcard'] = ['card_id' => $title];
|
2020-11-14 02:17:15 +08:00
|
|
|
|
2020-11-14 02:36:05 +08:00
|
|
|
return $this->sendKefuMsg();
|
2020-11-14 02:17:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2021-04-06 15:20:39 +08:00
|
|
|
* @param $media_id
|
|
|
|
|
* @param $thumb_media_id
|
|
|
|
|
* @param $title
|
|
|
|
|
* @param $description
|
2020-11-14 02:36:05 +08:00
|
|
|
* @return Result
|
2020-11-14 03:04:25 +08:00
|
|
|
* @throws Exception
|
2020-11-14 02:17:15 +08:00
|
|
|
*/
|
2021-04-06 15:20:39 +08:00
|
|
|
public function sendVideoNews($media_id, $thumb_media_id, $title, $description)
|
2020-11-14 02:17:15 +08:00
|
|
|
{
|
|
|
|
|
$this->msgData['msgtype'] = 'video';
|
2020-11-14 02:28:41 +08:00
|
|
|
$this->msgData['video'] = ['media_id' => [
|
|
|
|
|
'media_id' => $media_id,
|
|
|
|
|
'thumb_media_id' => $thumb_media_id,
|
|
|
|
|
'title' => $title,
|
|
|
|
|
'description' => $description
|
|
|
|
|
]];
|
2020-11-14 02:36:05 +08:00
|
|
|
return $this->sendKefuMsg();
|
2020-11-14 02:17:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2021-04-06 15:20:39 +08:00
|
|
|
* @param $musicurl
|
|
|
|
|
* @param $hqmusicurl
|
|
|
|
|
* @param $thumb_media_id
|
|
|
|
|
* @param $title
|
|
|
|
|
* @param $description
|
2020-11-14 02:36:05 +08:00
|
|
|
* @return Result
|
2020-11-14 03:04:25 +08:00
|
|
|
* @throws Exception
|
2020-11-14 02:17:15 +08:00
|
|
|
*/
|
2021-04-06 15:20:39 +08:00
|
|
|
public function sendMusicNews($musicurl, $hqmusicurl, $thumb_media_id, $title, $description)
|
2020-11-14 02:17:15 +08:00
|
|
|
{
|
|
|
|
|
$this->msgData['msgtype'] = 'music';
|
2020-11-14 02:28:41 +08:00
|
|
|
$this->msgData['music'] = [
|
|
|
|
|
'title' => $title,
|
|
|
|
|
'description' => $description,
|
|
|
|
|
'musicurl' => $musicurl,
|
|
|
|
|
'hqmusicurl' => $hqmusicurl,
|
|
|
|
|
'thumb_media_id' => $thumb_media_id
|
|
|
|
|
];
|
2020-11-14 02:36:05 +08:00
|
|
|
return $this->sendKefuMsg();
|
2020-11-14 02:17:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2021-04-06 15:20:39 +08:00
|
|
|
* @param $head_content
|
|
|
|
|
* @param $tail_content
|
2020-11-14 02:17:15 +08:00
|
|
|
* @param array $menus
|
2020-11-14 02:36:05 +08:00
|
|
|
* @return Result
|
2020-11-14 03:04:25 +08:00
|
|
|
* @throws Exception
|
2020-11-14 02:17:15 +08:00
|
|
|
*/
|
2021-04-06 15:20:39 +08:00
|
|
|
public function sendMenuNews($head_content, $tail_content, array $menus = [])
|
2020-11-14 02:17:15 +08:00
|
|
|
{
|
|
|
|
|
$this->msgData['msgtype'] = 'msgmenu';
|
2020-11-14 02:28:41 +08:00
|
|
|
$this->msgData['msgmenu'] = [
|
|
|
|
|
'head_content' => $head_content,
|
|
|
|
|
'tail_content' => $tail_content,
|
|
|
|
|
];
|
2020-11-14 02:17:15 +08:00
|
|
|
if (empty($menus) || !is_array($menus) || count($menus) < 2) {
|
2020-11-14 03:04:25 +08:00
|
|
|
throw new Exception('菜单选项必须有2个');
|
2020-11-14 02:17:15 +08:00
|
|
|
}
|
|
|
|
|
foreach ($menus as $key => $val) {
|
|
|
|
|
$this->addNewsMenu($val['id'], $val['name']);
|
|
|
|
|
}
|
2020-11-14 02:36:05 +08:00
|
|
|
return $this->sendKefuMsg();
|
2020-11-14 02:17:15 +08:00
|
|
|
}
|
|
|
|
|
|
2020-11-14 02:28:41 +08:00
|
|
|
private int $index = 0;
|
2020-11-14 02:17:15 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $id
|
|
|
|
|
* @param $menuName
|
|
|
|
|
* @return $this
|
|
|
|
|
*/
|
|
|
|
|
public function addNewsMenu($id, $menuName)
|
|
|
|
|
{
|
2020-11-14 02:28:41 +08:00
|
|
|
$lists['id'] = $id;
|
|
|
|
|
$lists['content'] = $menuName;
|
|
|
|
|
$this->msgData['msgmenu']['list'][$this->index] = $lists;
|
2020-11-14 02:17:15 +08:00
|
|
|
++$this->index;
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2020-11-14 02:36:05 +08:00
|
|
|
* @param $title
|
|
|
|
|
* @param $appid
|
|
|
|
|
* @param $pagepath
|
|
|
|
|
* @param $thumb_media_id
|
|
|
|
|
* @return Result
|
2020-11-14 03:04:25 +08:00
|
|
|
* @throws Exception
|
2020-11-14 02:17:15 +08:00
|
|
|
*/
|
2021-04-06 15:20:39 +08:00
|
|
|
public function sendMiniprogrampageNews($title, $appid, $pagepath, $thumb_media_id)
|
2020-11-14 02:17:15 +08:00
|
|
|
{
|
|
|
|
|
$this->msgData['msgtype'] = 'msgmenu';
|
2020-11-14 02:28:41 +08:00
|
|
|
$this->msgData['miniprogrampage'] = [
|
|
|
|
|
'title' => $title,
|
|
|
|
|
'appid' => $appid,
|
|
|
|
|
'pagepath' => $pagepath,
|
|
|
|
|
'thumb_media_id' => $thumb_media_id,
|
|
|
|
|
];
|
2020-11-14 02:36:05 +08:00
|
|
|
return $this->sendKefuMsg();
|
2020-11-14 02:17:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2021-04-06 15:20:39 +08:00
|
|
|
* @param $filePath
|
|
|
|
|
* @param $type
|
2020-11-14 02:17:15 +08:00
|
|
|
* @param bool $isPermanent
|
2021-04-06 15:20:39 +08:00
|
|
|
* @param $title
|
|
|
|
|
* @param $introduction
|
2020-11-14 02:17:15 +08:00
|
|
|
* @return mixed
|
2020-11-14 03:04:25 +08:00
|
|
|
* @throws Exception
|
2020-11-14 02:17:15 +08:00
|
|
|
*/
|
2021-04-06 15:20:39 +08:00
|
|
|
public function upload($filePath, $type, $isPermanent = false, $title = '', $introduction = '')
|
2020-11-14 02:17:15 +08:00
|
|
|
{
|
|
|
|
|
if (!file_exists($filePath)) {
|
2020-11-14 03:04:25 +08:00
|
|
|
throw new Exception('文件不存在');
|
2020-11-14 02:17:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!in_array($type, ['image', 'voice', 'video', 'thumb'])) {
|
2020-11-14 03:04:25 +08:00
|
|
|
throw new Exception('暂不支持的文件类型');
|
2020-11-14 02:17:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$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}";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$mime = mime_content_type($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];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->request->setMethod(HttpClient::POST);
|
|
|
|
|
|
|
|
|
|
/** @var Result $body */
|
|
|
|
|
$data = $this->request->post($url, $data);
|
|
|
|
|
if (!$data->isResultsOK()) {
|
2020-11-14 03:04:25 +08:00
|
|
|
throw new Exception($data->getMessage());
|
2020-11-14 02:17:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $data->getData();
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-14 02:33:13 +08:00
|
|
|
|
2020-11-14 02:17:15 +08:00
|
|
|
/**
|
2020-11-14 02:33:13 +08:00
|
|
|
* @return array
|
2020-11-14 02:17:15 +08:00
|
|
|
*/
|
2020-11-14 02:30:35 +08:00
|
|
|
public function getContents()
|
|
|
|
|
{
|
|
|
|
|
return $this->msgData;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2020-11-14 02:17:15 +08:00
|
|
|
/**
|
|
|
|
|
* @return Result
|
2020-11-14 03:04:25 +08:00
|
|
|
* @throws Exception
|
2020-11-14 02:17:15 +08:00
|
|
|
*/
|
|
|
|
|
private function sendKefuMsg()
|
|
|
|
|
{
|
|
|
|
|
$data = json_encode($this->msgData, JSON_UNESCAPED_UNICODE);
|
|
|
|
|
|
|
|
|
|
$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');
|
|
|
|
|
|
|
|
|
|
$this->request->addHeader('Content-Type', 'application/json');
|
|
|
|
|
|
|
|
|
|
/** @var Result $body */
|
|
|
|
|
$body = $this->request->post($url, $data);
|
|
|
|
|
|
|
|
|
|
if (!$body->isResultsOK()) {
|
2020-11-14 03:04:25 +08:00
|
|
|
throw new Exception($body->getMessage());
|
2020-11-14 02:17:15 +08:00
|
|
|
}
|
|
|
|
|
return $body;
|
|
|
|
|
}
|
2019-07-17 17:17:37 +08:00
|
|
|
}
|