Files
kiri-wchat/wx/Message.php
T

278 lines
7.3 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-11-14 03:04:25 +08:00
use Exception;
2020-03-05 12:41:49 +08:00
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
2023-11-14 00:45:54 +08:00
private array $msgData = [];
/**
2023-11-14 14:10:36 +08:00
* @param string $openid
2023-11-14 00:45:54 +08:00
*/
2023-12-12 15:35:37 +08:00
public function setOpenid(string $openid): void
2023-11-14 00:45:54 +08:00
{
$this->msgData['touser'] = $openid;
}
/**
2023-11-14 14:10:36 +08:00
* @param string $content
2023-11-14 00:45:54 +08:00
* @return Result
2023-12-12 15:35:37 +08:00
* @throws
2023-11-14 00:45:54 +08:00
*/
2023-11-14 14:10:36 +08:00
public function sendTextNews(string $content): Result
2023-11-14 00:45:54 +08:00
{
$this->msgData['msgtype'] = 'text';
$this->msgData['text'] = ['content' => $content];
return $this->sendKefuMsg();
}
/**
2023-11-14 14:10:36 +08:00
* @param string $media_id
2023-11-14 00:45:54 +08:00
* @return Result
2023-12-12 15:35:37 +08:00
* @throws
2023-11-14 00:45:54 +08:00
*/
2023-11-14 14:10:36 +08:00
public function sendImageNews(string $media_id): Result
2023-11-14 00:45:54 +08:00
{
$this->msgData['msgtype'] = 'image';
$this->msgData['image'] = ['media_id' => $media_id];
return $this->sendKefuMsg();
}
/**
2023-11-14 14:10:36 +08:00
* @param string $media_id
2023-11-14 00:45:54 +08:00
* @return Result
2023-12-12 15:35:37 +08:00
* @throws
2023-11-14 00:45:54 +08:00
*/
2023-11-14 14:10:36 +08:00
public function sendVoiceNews(string $media_id): Result
2023-11-14 00:45:54 +08:00
{
$this->msgData['msgtype'] = 'voice';
$this->msgData['voice'] = ['media_id' => $media_id];
return $this->sendKefuMsg();
}
/**
2023-11-14 14:10:36 +08:00
* @param string $media_id
2023-11-14 00:45:54 +08:00
* @return Result
2023-12-12 15:35:37 +08:00
* @throws
2023-11-14 00:45:54 +08:00
*/
2023-11-14 14:10:36 +08:00
public function sendMpNewsNews(string $media_id): Result
2023-11-14 00:45:54 +08:00
{
$this->msgData['msgtype'] = 'mpnews';
$this->msgData['mpnews'] = ['media_id' => $media_id];
return $this->sendKefuMsg();
}
/**
2023-11-14 14:10:36 +08:00
* @param string $title
* @param string $description
* @param string $url
* @param string $picurl
2023-11-14 00:45:54 +08:00
* @return Result
2023-12-12 15:35:37 +08:00
* @throws
2023-11-14 00:45:54 +08:00
*/
2023-11-14 14:10:36 +08:00
public function sendNewsNews(string $title, string $description, string $url, string $picurl): Result
2023-11-14 00:45:54 +08:00
{
$this->msgData['msgtype'] = 'news';
$this->msgData['news'] = [
'articles' => [
[
'title' => $title,
'description' => $description,
'url' => $url,
'picurl' => $picurl
]
]
];
return $this->sendKefuMsg();
}
/**
2023-11-14 14:10:36 +08:00
* @param string $title
2023-11-14 00:45:54 +08:00
* @return Result
2023-12-12 15:35:37 +08:00
* @throws
2023-11-14 00:45:54 +08:00
*/
2023-11-14 14:10:36 +08:00
public function sendCardNews(string $title): Result
2023-11-14 00:45:54 +08:00
{
$this->msgData['msgtype'] = 'wxcard';
$this->msgData['wxcard'] = ['card_id' => $title];
return $this->sendKefuMsg();
}
/**
2023-11-14 14:10:36 +08:00
* @param string $media_id
* @param string $thumb_media_id
* @param string $title
* @param string $description
2023-11-14 00:45:54 +08:00
* @return Result
2023-12-12 15:35:37 +08:00
* @throws
2023-11-14 00:45:54 +08:00
*/
2023-11-14 14:10:36 +08:00
public function sendVideoNews(string $media_id, string $thumb_media_id, string $title, string $description): Result
2023-11-14 00:45:54 +08:00
{
$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();
}
/**
2023-11-14 14:10:36 +08:00
* @param string $musicurl
* @param string $hqmusicurl
* @param string $thumb_media_id
* @param string $title
* @param string $description
2023-11-14 00:45:54 +08:00
* @return Result
2023-12-12 15:35:37 +08:00
* @throws
2023-11-14 00:45:54 +08:00
*/
2023-11-14 14:10:36 +08:00
public function sendMusicNews(string $musicurl, string $hqmusicurl, string $thumb_media_id, string $title, string $description): Result
2023-11-14 00:45:54 +08:00
{
$this->msgData['msgtype'] = 'music';
$this->msgData['music'] = [
'title' => $title,
'description' => $description,
'musicurl' => $musicurl,
'hqmusicurl' => $hqmusicurl,
'thumb_media_id' => $thumb_media_id
];
return $this->sendKefuMsg();
}
/**
2023-11-14 14:10:36 +08:00
* @param string $head_content
* @param string $tail_content
2023-11-14 00:45:54 +08:00
* @param array $menus
* @return Result
2023-12-12 15:35:37 +08:00
* @throws
2023-11-14 00:45:54 +08:00
*/
2023-11-14 14:10:36 +08:00
public function sendMenuNews(string $head_content, string $tail_content, array $menus = []): Result
2023-11-14 00:45:54 +08:00
{
$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个');
2023-11-07 15:13:11 +08:00
}
2023-11-14 00:45:54 +08:00
foreach ($menus as $val) {
$this->addNewsMenu($val['id'], $val['name']);
2023-11-07 15:13:11 +08:00
}
2023-11-14 00:45:54 +08:00
return $this->sendKefuMsg();
}
private int $index = 0;
/**
2023-11-14 14:10:36 +08:00
* @param string $id
* @param string $menuName
2023-11-14 00:45:54 +08:00
* @return $this
*/
2023-11-14 14:10:36 +08:00
public function addNewsMenu(string $id, string $menuName): static
2023-11-14 00:45:54 +08:00
{
$lists['id'] = $id;
$lists['content'] = $menuName;
$this->msgData['msgmenu']['list'][$this->index] = $lists;
++$this->index;
return $this;
}
/**
2023-11-14 14:10:36 +08:00
* @param string $title
* @param string $appid
* @param string $pagepath
* @param string $thumb_media_id
2023-11-14 00:45:54 +08:00
* @return Result
2023-12-12 15:35:37 +08:00
* @throws
2023-11-14 00:45:54 +08:00
*/
2023-11-14 14:10:36 +08:00
public function sendMiniprogrampageNews(string $title, string $appid, string $pagepath, string $thumb_media_id): Result
2023-11-14 00:45:54 +08:00
{
$this->msgData['msgtype'] = 'msgmenu';
$this->msgData['miniprogrampage'] = [
'title' => $title,
'appid' => $appid,
'pagepath' => $pagepath,
'thumb_media_id' => $thumb_media_id,
];
return $this->sendKefuMsg();
}
/**
2023-11-14 12:39:28 +08:00
* @param string $filePath
* @param string $type
2023-11-14 00:45:54 +08:00
* @param bool $isPermanent
* @param string $title
* @param string $introduction
* @return mixed
2023-12-12 15:35:37 +08:00
* @throws
2023-11-14 00:45:54 +08:00
*/
2023-11-14 12:39:28 +08:00
public function uploadFile(string $filePath, string $type, bool $isPermanent = false, string $title = '', string $introduction = ''): Result
2023-11-14 00:45:54 +08:00
{
if (!file_exists($filePath)) {
throw new Exception('文件不存在');
}
if (!in_array($type, ['image', 'voice', 'video', 'thumb'])) {
throw new Exception('暂不支持的文件类型');
}
$token = $this->payConfig->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);
$real_path = new \CURLFile(realpath($filePath));
$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];
}
2023-11-14 12:39:28 +08:00
return $this->post('api.weixin.qq.com', $url, $data);
2023-11-14 00:45:54 +08:00
}
/**
* @return array
*/
public function getContents(): array
{
return $this->msgData;
}
/**
* @return Result
2023-12-12 15:35:37 +08:00
* @throws
2023-11-14 00:45:54 +08:00
*/
2023-11-14 14:10:36 +08:00
private function sendKefuMsg(): Result
2023-11-14 00:45:54 +08:00
{
$url = '/cgi-bin/message/custom/send?access_token=' . $this->payConfig->getAccessToken();
2023-11-14 12:39:28 +08:00
return $this->post('api.weixin.qq.com', $url, $this->msgData);
2023-11-14 00:45:54 +08:00
}
2019-07-17 17:17:37 +08:00
}