Files
kiri-wchat/wchat/wx/Message.php
T

315 lines
7.2 KiB
PHP
Raw Normal View History

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-03-05 12:41:49 +08:00
use wchat\common\HttpClient;
use wchat\common\Result;
2019-11-11 18:14:47 +08:00
class Message extends SmallProgram
2019-07-17 17:17:37 +08:00
{
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 $openid = '';
private $msgData = [];
/**
* @param string $openid
*/
public function setOpenid(string $openid)
{
$this->openid = $openid;
$this->msgData['touser'] = $openid;
}
/**
* @param string $content
* @return Result
* @throws \Exception
*/
public function sendTextNews(string $content)
{
$this->msgData['msgtype'] = 'text';
$this->msgData['text[content]'] = $content;
return $this->sendKefuMsg();
}
/**
* @param $media_id
* @return Result
* @throws \Exception
*/
public function sendImageNews(string $media_id)
{
$this->msgData['msgtype'] = 'image';
$this->msgData['image[media_id]'] = $media_id;
return $this->sendKefuMsg();
}
/**
* @param $media_id
* @return Result
* @throws \Exception
*/
public function sendVoiceNews(string $media_id)
{
$this->msgData['msgtype'] = 'voice';
$this->msgData['voice[media_id]'] = $media_id;
return $this->sendKefuMsg();
}
/**
* @param $media_id
* @return Result
* @throws \Exception
*/
public function sendMpNewsNews(string $media_id)
{
$this->msgData['msgtype'] = 'mpnews';
$this->msgData['mpnews[media_id]'] = $media_id;
return $this->sendKefuMsg();
}
/**
* @param string $title
* @param string $description
* @param string $url
* @param string $picurl
* @return Result
* @throws \Exception
*/
public function sendNewsNews(string $title, string $description, string $url, string $picurl)
{
$this->msgData['msgtype'] = 'news';
$this->msgData['news[articles][0][title]'] = $title;
$this->msgData['news[articles][0][description]'] = $description;
$this->msgData['news[articles][0][url]'] = $url;
$this->msgData['news[articles][0][picurl]'] = $picurl;
return $this->sendKefuMsg();
}
/**
* @param string $title
* @return Result
* @throws \Exception
*/
public function sendCardNews(string $title)
{
$this->msgData['msgtype'] = 'wxcard';
$this->msgData['wxcard[card_id]'] = $title;
return $this->sendKefuMsg();
}
2019-07-19 17:50:49 +08:00
/**
* @param string $media_id
* @param string $thumb_media_id
* @param string $title
* @param string $description
* @return Result
* @throws \Exception
*/
2019-07-19 17:53:54 +08:00
public function sendVideoNews(string $media_id, string $thumb_media_id, string $title, string $description)
2019-07-19 17:50:49 +08:00
{
$this->msgData['msgtype'] = 'video';
$this->msgData['video[media_id]'] = $media_id;
$this->msgData['video[thumb_media_id]'] = $thumb_media_id;
$this->msgData['video[title]'] = $title;
$this->msgData['video[description]'] = $description;
return $this->sendKefuMsg();
}
2019-07-19 17:53:54 +08:00
/**
* @param string $musicurl
* @param string $hqmusicurl
* @param string $thumb_media_id
* @param string $title
* @param string $description
* @return Result
* @throws \Exception
*/
public function sendMusicNews(string $musicurl, string $hqmusicurl, string $thumb_media_id, string $title, string $description)
{
$this->msgData['msgtype'] = 'music';
$this->msgData['music[title]'] = $title;
$this->msgData['music[description]'] = $description;
$this->msgData['music[musicurl]'] = $musicurl;
$this->msgData['music[hqmusicurl]'] = $hqmusicurl;
$this->msgData['music[thumb_media_id]'] = $thumb_media_id;
return $this->sendKefuMsg();
}
2019-07-17 17:17:37 +08:00
/**
* @param string $head_content
* @param string $tail_content
* @param array $menus
* @return Result
* @throws \Exception
*/
public function sendMenuNews(string $head_content, string $tail_content, array $menus = [])
{
$this->msgData['msgtype'] = 'msgmenu';
$this->msgData['msgmenu[head_content]'] = $head_content;
$this->msgData['msgmenu[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();
}
private $index = 0;
/**
* @param $id
* @param $menuName
* @return $this
*/
public function addNewsMenu($id, $menuName)
{
$this->msgData['msgmenu[list][' . $this->index . '][id]'] = $id;
$this->msgData['msgmenu[list][' . $this->index . '][content]'] = $menuName;
++$this->index;
return $this;
}
/**
* @param $title
* @param $appid
* @param $pagepath
* @param $thumb_media_id
* @return Result
* @throws \Exception
*/
public function sendMiniprogrampageNews(string $title, string $appid, string $pagepath, string $thumb_media_id)
{
$this->msgData['msgtype'] = 'msgmenu';
$this->msgData['miniprogrampage[title]'] = $title;
$this->msgData['miniprogrampage[appid]'] = $appid;
$this->msgData['miniprogrampage[pagepath]'] = $pagepath;
$this->msgData['miniprogrampage[thumb_media_id]'] = $thumb_media_id;
return $this->sendKefuMsg();
}
/**
* @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('暂不支持的文件类型');
}
2019-07-17 17:27:31 +08:00
$token = $this->getAccessToken();
2019-07-17 17:17:37 +08:00
if ($isPermanent) {
2019-07-17 17:27:31 +08:00
$url = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token={$token}&type={$type}";
2019-07-17 17:17:37 +08:00
} else {
2019-07-17 17:27:31 +08:00
$url = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token={$token}&type={$type}";
2019-07-17 17:17:37 +08:00
}
$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];
}
2019-11-11 18:14:47 +08:00
$this->request->setMethod(HttpClient::POST);
2019-07-17 17:17:37 +08:00
/** @var Result $body */
$data = $this->request->post($url, $data);
if (!$data->isResultsOK()) {
throw new \Exception($data->getMessage());
}
return $data->getData();
}
/**
* @param $mime
* @throws \Exception
*/
private function checkExtinfo($mime)
{
switch (strtolower($mime)) {
case 'image/bmp':
case 'image/png':
case 'image/jpeg':
case 'image/jpg':
case 'image/gif':
break;
case 'mp3/wma/wav/amr':
break;
case 'mp4';
break;
case 'jpg';
break;
default:
throw new \Exception('不支持的文件格式');
}
}
/**
* @param $data
* @return Result
* @throws \Exception
*/
private function sendKefuMsg()
{
$data = json_encode($this->msgData, JSON_UNESCAPED_UNICODE);
2019-07-17 17:27:31 +08:00
$url = '/cgi-bin/message/custom/send?access_token=' . $this->getAccessToken();
2019-11-11 18:14:47 +08:00
$this->request->setMethod(HttpClient::POST);
2020-04-01 11:53:15 +08:00
$this->request->setErrorField('errcode');
$this->request->setErrorMsgField('errmsg');
2019-07-17 17:17:37 +08:00
/** @var Result $body */
$body = $this->request->post($url, $data);
if (!$body->isResultsOK()) {
throw new \Exception($body->getMessage());
}
return $body;
}
}