326 lines
7.4 KiB
PHP
326 lines
7.4 KiB
PHP
<?php
|
|
|
|
|
|
namespace wchat\wx;
|
|
|
|
use Exception;
|
|
use Kiri\Client;
|
|
use wchat\common\Result;
|
|
|
|
|
|
/**
|
|
* Class Message
|
|
* @package wchat\wx
|
|
*/
|
|
class Message extends SmallProgram
|
|
{
|
|
|
|
private array $msgData = [];
|
|
|
|
|
|
/**
|
|
* @param $openid
|
|
*/
|
|
public function setOpenid($openid)
|
|
{
|
|
$this->msgData['touser'] = $openid;
|
|
}
|
|
|
|
/**
|
|
* @param $content
|
|
* @return Result
|
|
* @throws Exception
|
|
*/
|
|
public function sendTextNews($content): Result
|
|
{
|
|
$this->msgData['msgtype'] = 'text';
|
|
$this->msgData['text'] = ['content' => $content];
|
|
|
|
return $this->sendKefuMsg();
|
|
}
|
|
|
|
/**
|
|
* @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();
|
|
}
|
|
|
|
|
|
/**
|
|
* @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();
|
|
}
|
|
|
|
/**
|
|
* @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();
|
|
}
|
|
|
|
|
|
/**
|
|
* @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): Result
|
|
{
|
|
$this->msgData['msgtype'] = 'wxcard';
|
|
$this->msgData['wxcard'] = ['card_id' => $title];
|
|
|
|
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): 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 = []): 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;
|
|
|
|
/**
|
|
* @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): 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 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('暂不支持的文件类型');
|
|
}
|
|
|
|
$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);
|
|
|
|
$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];
|
|
}
|
|
|
|
$client = new Client('api.weixin.qq.com', 443, true);
|
|
$client->withHeader(['Content-Type' => 'multipart/form-data']);
|
|
$proxyHost = $this->getConfig()->getProxyHost();
|
|
$proxyPort = $this->getConfig()->getProxyPort();
|
|
if (!empty($proxyHost) && $proxyPort > 0) {
|
|
$client->withProxyHost($proxyHost)->withProxyPort($proxyPort);
|
|
}
|
|
$client->post($url, $data);
|
|
$client->close();
|
|
|
|
$this->msgData = [];
|
|
|
|
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
|
|
return new Result(code: 505, message: $client->getBody());
|
|
}
|
|
$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(): array
|
|
{
|
|
return $this->msgData;
|
|
}
|
|
|
|
|
|
/**
|
|
* @return Result
|
|
* @throws Exception
|
|
*/
|
|
private function sendKefuMsg()
|
|
{
|
|
$url = '/cgi-bin/message/custom/send?access_token=' . $this->config->getAccessToken();
|
|
|
|
$client = new Client('api.weixin.qq.com', 443, true);
|
|
$client->withHeader(['Content-Type' => 'application/json']);
|
|
$proxyHost = $this->getConfig()->getProxyHost();
|
|
$proxyPort = $this->getConfig()->getProxyPort();
|
|
if (!empty($proxyHost) && $proxyPort > 0) {
|
|
$client->withProxyHost($proxyHost)->withProxyPort($proxyPort);
|
|
}
|
|
$client->post($url, $this->msgData);
|
|
$client->close();
|
|
|
|
$this->msgData = [];
|
|
|
|
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
|
|
return new Result(code: 505, message: $client->getBody());
|
|
}
|
|
$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);
|
|
}
|
|
}
|
|
}
|