2023-03-16 20:04:53 +08:00

106 lines
3.3 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace Extend\TencentIm;
use App\Constants\HttpEnumCode;
use App\Exception\BusinessException;
use App\Utils\Log;
use GuzzleHttp\Exception\GuzzleException;
use Hyperf\Snowflake\IdGeneratorInterface;
use Hyperf\Utils\ApplicationContext;
class MyStruct{
}
/**
* 消息
*/
class Message extends Base
{
private string $version = 'v4';
// 发送消息参数
private array $arg;
public function __construct()
{
parent::__construct();
$this->arg = [
// 把消息同步到 From_Account 在线终端和漫游上;消息不同步至 From_Account
'SyncOtherMachine' => 1,
//消息随机数32位无符号整数
'MsgRandom' => random_int(1, 4292967295),
/**
* 消息回调禁止开关,只对本条消息有效
* ForbidBeforeSendMsgCallback 表示禁止发消息前回调
* ForbidAfterSendMsgCallback 表示禁止发消息后回调
*/
'ForbidCallbackControl' => ['ForbidBeforeSendMsgCallback','ForbidAfterSendMsgCallback'],
/**
* 消息发送控制选项,是一个 String 数组,只对本条消息有效。
* "NoUnread"表示该条消息不计入未读数。
* "NoLastMsg"表示该条消息不更新会话列表。
* "WithMuteNotifications"表示该条消息的接收方对发送方设置的免打扰选项生效(默认不生效)
*/
'SendMsgControl' => ['NoUnread','NoLastMsg','WithMuteNotifications'],
'MsgBody' => [
/**
* 消息对象类型
* TIMTextElem文本消息
* TIMLocationElem位置消息
* TIMFaceElem表情消息
* TIMCustomElem自定义消息
* TIMSoundElem语音消息
* TIMImageElem图像消息
* TIMFileElem文件消息
* TIMVideoFileElem视频消息
*/
'MsgType' => "TIMTextElem",
// 消息内容,对于每种 MsgType 用不同的 MsgContent 格式
'MsgContent' => null,
],
/**
* 消息自定义数据(云端保存,会发送到对端,程序卸载重装后还能拉取到)
* 如聊天类型。json格式
* order_inquiry_id
* inquiry_type
* message_type 消息类型1系统发送 其余:用户发送)
*/
'CloudCustomData' => "",
];
}
/**
* 发送消息
* @param array $arg
* @return bool
* @throws GuzzleException
*/
public function sendMessage(array $arg): bool
{
try {
// 合并发送参数
$arg = array_merge($this->arg,$arg);
Log::getInstance()->info(json_encode($arg,JSON_UNESCAPED_UNICODE));
$options = [
"json"=> $arg
];
$path = $this->config['base_url'] . $this->version . "/openim/sendmsg?" . $this->buildRequestParams();
$this->postRequest($path,$options);
return true;
}catch (\Exception $e) {
throw new BusinessException($e->getMessage(), HttpEnumCode::SERVER_ERROR);
}
}
}