Files
amb_wechatapp/application/common/library/live/Message.php
T

158 lines
5.4 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 app\common\library\live;
use app\common\library\live\message\Manager;
use app\common\library\live\message\Result as ProviderResult;
/**
* 直播消息
* 兼容历史调用入口:send / del / getOnlineUser
* 内部统一委托给 message\Manager 按 provider 路由到具体服务商实现
* 新业务建议直接使用 \app\common\library\live\message\Manager
*/
class Message
{
/**
* 发送消息(兼容历史接口)
* 默认使用配置中的 provider;如需指定 provider 请使用 sendByProvider
* @param string $topic 群组ID(奥点云 topic / 阿里云 GroupId
* @param string $message 消息体(JSON 字符串)
* @return array 历史返回结构(兼容奥点云格式 ['Flag'=>200,'uuid'=>xxx]);失败时返回 false
*/
public static function send($topic, $message){
return self::sendByProvider(null, $topic, $message);
}
/**
* 按指定服务商发送消息
* @param string|null $provider 服务商名称(aodianyun/aliyun),null 则用配置默认值
* @param string $topic 群组ID
* @param string $message 消息体
* @param int $msgType 消息类型(阿里云 MsgType,默认 10001 文本)
* @param string $senderId 发送者ID(阿里云必填,奥点云忽略)
* @param string $senderMeta 发送者扩展信息
* @param string $msgTid 消息唯一标识
* @return array 兼容历史返回结构:成功返回 ['Flag'=>200, 'uuid'=>xxx, 'msg_tid'=>xxx],失败返回 ['Flag'=>0, 'FlagString'=>error]
*/
public static function sendByProvider($provider, $topic, $message, $msgType = 10001, $senderId = '', $senderMeta = '', $msgTid = ''){
try {
$result = Manager::provider($provider)->send($topic, $message, $msgType, $senderId, $senderMeta, $msgTid);
} catch (\Exception $e) {
return ['Flag' => 0, 'FlagString' => $e->getMessage()];
}
return self::resultToSendArray($result);
}
/**
* 撤回/删除消息(兼容历史接口)
* @param string $topic 群组ID
* @param string $uuid 消息唯一标识(奥点云 uuid / 阿里云 MsgTid
* @return array|false
*/
public static function del($topic, $uuid){
return self::delByProvider(null, $topic, $uuid);
}
/**
* 按指定服务商撤回/删除消息
* @param string|null $provider
* @param string $topic
* @param string $uuid
* @return array
*/
public static function delByProvider($provider, $topic, $uuid){
try {
$result = Manager::provider($provider)->del($topic, $uuid);
} catch (\Exception $e) {
return ['Flag' => 0, 'FlagString' => $e->getMessage()];
}
return self::resultToDelArray($result);
}
/**
* 获取分组中的在线用户(兼容历史接口)
* @param string $topic
* @param int $skip
* @param int $num
* @return array|false 失败返回 false,成功返回 ['total'=>int, 'list'=>array]
*/
public static function getOnlineUser($topic, $skip=0, $num=0){
return self::getOnlineUserByProvider(null, $topic, $skip, $num);
}
/**
* 按指定服务商获取在线用户
* @param string|null $provider
* @param string $topic
* @param int $skip
* @param int $num
* @return array|false
*/
public static function getOnlineUserByProvider($provider, $topic, $skip=0, $num=0){
try {
$result = Manager::provider($provider)->getOnlineUser($topic, $num, $skip);
} catch (\Exception $e) {
return false;
}
if (!$result->isSuccess()) {
return false;
}
$data = $result->data;
if (!is_array($data)) {
return false;
}
return [
'total' => isset($data['total']) ? (int)$data['total'] : 0,
'list' => isset($data['list']) ? $data['list'] : [],
];
}
/**
* 将服务商 Result 转换为历史 send 接口返回结构
* 兼容奥点云 Dms::sendMsg 的 ['Flag'=>200,'uuid'=>xxx] 格式
* @param ProviderResult $result
* @return array
*/
protected static function resultToSendArray(ProviderResult $result){
if ($result->isSuccess()) {
$data = $result->data;
$uuid = is_array($data) && isset($data['uuid']) ? $data['uuid'] : '';
$msgTid = is_array($data) && isset($data['msg_tid']) ? $data['msg_tid'] : $uuid;
return [
'Flag' => 200,
'FlagString' => '',
'uuid' => $uuid,
'msg_tid' => $msgTid,
];
}
return [
'Flag' => 0,
'FlagString' => $result->error ? $result->error : '发送失败',
];
}
/**
* 将服务商 Result 转换为历史 del 接口返回结构
* @param ProviderResult $result
* @return array
*/
protected static function resultToDelArray(ProviderResult $result){
if ($result->isSuccess()) {
return [
'Flag' => 200,
'FlagString' => '',
];
}
return [
'Flag' => 0,
'FlagString' => $result->error ? $result->error : '删除失败',
];
}
}