165 lines
5.6 KiB
PHP
165 lines
5.6 KiB
PHP
<?php
|
||
|
||
namespace app\common\library\live\message\provider;
|
||
|
||
use app\common\library\live\message\ProviderInterface;
|
||
use app\common\library\live\message\Result;
|
||
use app\common\model\config\System;
|
||
use aodianyun\Dms;
|
||
|
||
/**
|
||
* 奥点云消息服务商适配器
|
||
* 封装 \aodianyun\Dms,保持与原 library/live/Message.php 一致的行为
|
||
*/
|
||
class Aodianyun implements ProviderInterface
|
||
{
|
||
/**
|
||
* @var Dms
|
||
*/
|
||
protected $dms;
|
||
|
||
/**
|
||
* 构造函数,从配置读取 s_key 并实例化 Dms
|
||
*/
|
||
public function __construct()
|
||
{
|
||
$config = System::getConfig('aodianyun');
|
||
$sKey = is_array($config) && isset($config['s_key']) ? $config['s_key'] : '';
|
||
$this->dms = new Dms($sKey);
|
||
}
|
||
|
||
/**
|
||
* 发送消息到群组
|
||
* 奥点云 DMS 成功响应:HTTP 201 + {"uuid":"xxx"}(无 Flag 字段)
|
||
* 失败响应:{"error":"xxx"} 或 Dms::curl 兜底构造的 ['Flag'=>httpCode, 'FlagString'=>body]
|
||
* @param string $groupId 群组ID(奥点云 topic)
|
||
* @param string $body 消息内容
|
||
* @param int $msgType 消息类型(奥点云不使用)
|
||
* @param string $senderId 发送者ID(奥点云不使用)
|
||
* @param string $senderMeta 发送者扩展信息(奥点云不使用)
|
||
* @param string $msgTid 消息唯一标识(奥点云由服务端生成 uuid)
|
||
* @return Result data 含 uuid
|
||
*/
|
||
public function send($groupId, $body, $msgType = 10001, $senderId = '', $senderMeta = '', $msgTid = '')
|
||
{
|
||
$response = $this->dms->sendMsg($groupId, $body);
|
||
|
||
// 成功:响应中含 uuid 字段
|
||
if (is_array($response) && isset($response['uuid'])) {
|
||
$uuid = $response['uuid'];
|
||
return Result::success(['uuid' => $uuid, 'msg_tid' => $uuid], $response);
|
||
}
|
||
|
||
$error = $this->extractError($response, '发送失败');
|
||
return Result::fail($error, $response);
|
||
}
|
||
|
||
/**
|
||
* 撤回/删除消息
|
||
* 奥点云 DMS 成功响应:HTTP 204 No Content,body 为空,Dms::delMsg 返回 null
|
||
* 失败响应:{"error":"xxx"} 或 ['Flag'=>httpCode, 'FlagString'=>body]
|
||
* @param string $groupId 群组ID
|
||
* @param string $msgTid 消息唯一标识(奥点云为 uuid)
|
||
* @return Result
|
||
*/
|
||
public function del($groupId, $msgTid)
|
||
{
|
||
$response = $this->dms->delMsg($groupId, $msgTid);
|
||
|
||
// 成功:HTTP 204 空内容,json_decode 返回 null;curl 失败时 Dms 会构造 ['Flag'=>0, ...] 数组
|
||
if ($response === null || (is_array($response) && empty($response))) {
|
||
return Result::success([], $response);
|
||
}
|
||
|
||
$error = $this->extractError($response, '删除失败');
|
||
return Result::fail($error, $response);
|
||
}
|
||
|
||
/**
|
||
* 获取群组在线用户
|
||
* 奥点云 DMS 成功响应:{"list":[...], "total":N}
|
||
* 失败响应:{"error":"xxx"} 或 ['Flag'=>httpCode, 'FlagString'=>body]
|
||
* @param string $groupId 群组ID
|
||
* @param int $pageSize 每页数量(奥点云作为 num)
|
||
* @param int $nextPageToken 起始偏移量(奥点云作为 skip)
|
||
* @return Result data 含 total、list、next_page_token、has_more
|
||
*/
|
||
public function getOnlineUser($groupId, $pageSize = 50, $nextPageToken = 0)
|
||
{
|
||
$skip = (int)$nextPageToken;
|
||
$num = (int)$pageSize;
|
||
|
||
$response = $this->dms->getOnlineUser($groupId, $skip, $num);
|
||
|
||
// 成功:响应中含 total 字段
|
||
if (is_array($response) && isset($response['total'])) {
|
||
$total = (int)$response['total'];
|
||
$list = isset($response['list']) ? $response['list'] : [];
|
||
return Result::success([
|
||
'total' => $total,
|
||
'list' => $list,
|
||
'next_page_token' => 0,
|
||
'has_more' => false,
|
||
], $response);
|
||
}
|
||
|
||
$error = $this->extractError($response, '获取在线用户失败');
|
||
return Result::fail($error, $response);
|
||
}
|
||
|
||
/**
|
||
* 从奥点云 Dms 响应中提取错误信息
|
||
* 优先取官方文档的 error 字段,其次取 Dms::curl 兜底的 FlagString 字段
|
||
* @param mixed $response
|
||
* @param string $default 默认错误文案
|
||
* @return string
|
||
*/
|
||
protected function extractError($response, $default = '操作失败')
|
||
{
|
||
if (!is_array($response)) {
|
||
return $default;
|
||
}
|
||
if (isset($response['error']) && $response['error'] !== '') {
|
||
return $response['error'];
|
||
}
|
||
if (isset($response['FlagString']) && $response['FlagString'] !== '') {
|
||
return $response['FlagString'];
|
||
}
|
||
return $default;
|
||
}
|
||
|
||
/**
|
||
* 获取客户端登录鉴权信息(奥点云不需要 token 鉴权,返回空数据)
|
||
* @param string $userId 用户ID
|
||
* @param string $role 角色
|
||
* @return Result
|
||
*/
|
||
public function getLoginAuth($userId, $role = '')
|
||
{
|
||
return Result::success([], null);
|
||
}
|
||
|
||
/**
|
||
* 创建群组(奥点云无需创建群组,直接返回成功)
|
||
* @param string $groupId 群组ID
|
||
* @param string $groupName 群组名称
|
||
* @param string $groupMeta 群组扩展信息
|
||
* @return Result
|
||
*/
|
||
public function createGroup($groupId, $groupName = '', $groupMeta = '')
|
||
{
|
||
return Result::success([], null);
|
||
}
|
||
|
||
/**
|
||
* 删除群组(奥点云无需删除群组,直接返回成功)
|
||
* @param string $groupId 群组ID
|
||
* @param string $operatorId 操作者ID
|
||
* @return Result
|
||
*/
|
||
public function deleteGroup($groupId, $operatorId = '')
|
||
{
|
||
return Result::success([], null);
|
||
}
|
||
}
|