297 lines
7.6 KiB
PHP
297 lines
7.6 KiB
PHP
<?php
|
|
|
|
|
|
namespace wchat\wx;
|
|
|
|
use wchat\common\HttpClient;
|
|
use wchat\common\Result;
|
|
|
|
class Message extends SmallProgram
|
|
{
|
|
const TEXT = 0;
|
|
const IMAGE = 1;
|
|
const VOICE = 2;
|
|
const NEWS = 3;
|
|
const VIDEO = 4;
|
|
const MUSIC = 5;
|
|
const MINIPROGRAMPAGE = 6;
|
|
const WXCARD = 7;
|
|
|
|
private string $openid = '';
|
|
private array $msgData = [];
|
|
|
|
|
|
/**
|
|
* @param string $openid
|
|
*/
|
|
public function setOpenid(string $openid)
|
|
{
|
|
$this->openid = $openid;
|
|
$this->msgData['touser'] = $openid;
|
|
}
|
|
|
|
/**
|
|
* @param string $content
|
|
* @return Message
|
|
*/
|
|
public function sendTextNews(string $content)
|
|
{
|
|
$this->msgData['msgtype'] = 'text';
|
|
$this->msgData['text'] = ['content' => $content];
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* @param string $media_id
|
|
* @return Message
|
|
*/
|
|
public function sendImageNews(string $media_id)
|
|
{
|
|
$this->msgData['msgtype'] = 'image';
|
|
$this->msgData['image'] = ['media_id' => $media_id];
|
|
|
|
return $this;
|
|
}
|
|
|
|
|
|
/**
|
|
* @param string $media_id
|
|
* @return Message
|
|
*/
|
|
public function sendVoiceNews(string $media_id)
|
|
{
|
|
$this->msgData['msgtype'] = 'voice';
|
|
$this->msgData['voice'] = ['media_id' => $media_id];
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* @param string $media_id
|
|
* @return Message
|
|
*/
|
|
public function sendMpNewsNews(string $media_id)
|
|
{
|
|
$this->msgData['msgtype'] = 'mpnews';
|
|
$this->msgData['mpnews'] = ['media_id' => $media_id];
|
|
|
|
return $this;
|
|
}
|
|
|
|
|
|
/**
|
|
* @param string $title
|
|
* @param string $description
|
|
* @param string $url
|
|
* @param string $picurl
|
|
* @return Message
|
|
*/
|
|
public function sendNewsNews(string $title, string $description, string $url, string $picurl)
|
|
{
|
|
$this->msgData['msgtype'] = 'news';
|
|
$this->msgData['news'] = ['articles' => [[
|
|
'title' => $title,
|
|
'description' => $description,
|
|
'url' => $url,
|
|
'picurl' => $picurl
|
|
]]];
|
|
return $this;
|
|
}
|
|
|
|
|
|
/**
|
|
* @param string $title
|
|
* @return Message
|
|
*/
|
|
public function sendCardNews(string $title)
|
|
{
|
|
$this->msgData['msgtype'] = 'wxcard';
|
|
$this->msgData['wxcard'] = ['card_id' => $title];
|
|
|
|
return $this;
|
|
}
|
|
|
|
|
|
/**
|
|
* @param string $media_id
|
|
* @param string $thumb_media_id
|
|
* @param string $title
|
|
* @param string $description
|
|
* @return Message
|
|
*/
|
|
public function sendVideoNews(string $media_id, string $thumb_media_id, string $title, string $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;
|
|
}
|
|
|
|
|
|
/**
|
|
* @param string $musicurl
|
|
* @param string $hqmusicurl
|
|
* @param string $thumb_media_id
|
|
* @param string $title
|
|
* @param string $description
|
|
* @return Message
|
|
*/
|
|
public function sendMusicNews(string $musicurl, string $hqmusicurl, string $thumb_media_id, string $title, string $description)
|
|
{
|
|
$this->msgData['msgtype'] = 'music';
|
|
$this->msgData['music'] = [
|
|
'title' => $title,
|
|
'description' => $description,
|
|
'musicurl' => $musicurl,
|
|
'hqmusicurl' => $hqmusicurl,
|
|
'thumb_media_id' => $thumb_media_id
|
|
];
|
|
return $this;
|
|
}
|
|
|
|
|
|
/**
|
|
* @param string $head_content
|
|
* @param string $tail_content
|
|
* @param array $menus
|
|
* @return Message
|
|
* @throws \Exception
|
|
*/
|
|
public function sendMenuNews(string $head_content, string $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;
|
|
}
|
|
|
|
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 string $title
|
|
* @param string $appid
|
|
* @param string $pagepath
|
|
* @param string $thumb_media_id
|
|
* @return Message
|
|
*/
|
|
public function sendMiniprogrampageNews(string $title, string $appid, string $pagepath, string $thumb_media_id)
|
|
{
|
|
$this->msgData['msgtype'] = 'msgmenu';
|
|
$this->msgData['miniprogrampage'] = [
|
|
'title' => $title,
|
|
'appid' => $appid,
|
|
'pagepath' => $pagepath,
|
|
'thumb_media_id' => $thumb_media_id,
|
|
];
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* @param string $filePath
|
|
* @param string $type
|
|
* @param bool $isPermanent
|
|
* @param string $title
|
|
* @param string $introduction
|
|
* @return mixed
|
|
* @throws \Exception
|
|
*/
|
|
public function upload(string $filePath, string $type, $isPermanent = false, string $title = '', string $introduction = '')
|
|
{
|
|
if (!file_exists($filePath)) {
|
|
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}";
|
|
}
|
|
|
|
$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()) {
|
|
throw new \Exception($data->getMessage());
|
|
}
|
|
|
|
return $data->getData();
|
|
}
|
|
|
|
|
|
/**
|
|
* @return array
|
|
*/
|
|
public function getContents()
|
|
{
|
|
return $this->msgData;
|
|
}
|
|
|
|
|
|
/**
|
|
* @return Result
|
|
* @throws \Exception
|
|
*/
|
|
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()) {
|
|
throw new \Exception($body->getMessage());
|
|
}
|
|
return $body;
|
|
}
|
|
}
|