309 lines
9.9 KiB
PHP
309 lines
9.9 KiB
PHP
<?php
|
|
|
|
namespace app\admin\controller\live;
|
|
|
|
use app\common\controller\Backend;
|
|
use think\Db;
|
|
/**
|
|
* 直播互动消息
|
|
*
|
|
* @icon fa fa-circle-o
|
|
*/
|
|
class Message extends Backend
|
|
{
|
|
|
|
/**
|
|
* Message模型对象
|
|
* @var \app\admin\model\live\Message
|
|
*/
|
|
protected $model = null;
|
|
|
|
//中控台消息接口无需权限节点校验,讲师角色无对应权限节点也可访问
|
|
protected $noNeedRight = ['getaliyunauth', 'getmessagelist', 'send', 'status', 'del'];
|
|
|
|
public function _initialize()
|
|
{
|
|
parent::_initialize();
|
|
$this->model = new \app\admin\model\live\Message;
|
|
}
|
|
|
|
/**
|
|
* 查看
|
|
*/
|
|
public function index()
|
|
{
|
|
//当前是否为关联查询
|
|
$this->relationSearch = true;
|
|
|
|
|
|
|
|
//设置过滤方法
|
|
$this->request->filter(['strip_tags', 'trim']);
|
|
//如果发送的来源是Selectpage,则转发到Selectpage
|
|
if ($this->request->request('keyField')) {
|
|
return $this->selectpage();
|
|
}
|
|
list($where, $sort, $order, $offset, $limit) = $this->buildparams();
|
|
|
|
$query = $this->model
|
|
->setUniacid(false)
|
|
->with(['user','liveuser']);
|
|
$list = $query
|
|
->where($where)
|
|
->where('message.uniacid',UNIACID)
|
|
->order($sort, $order)->group('message.id')
|
|
->paginate($limit);
|
|
|
|
foreach ($list as $row) {
|
|
$row->getRelation('user')->visible(\app\admin\model\User::$listShowField);
|
|
$row->getRelation('liveuser')->visible(['id','user_id','black','bantalk']);
|
|
}
|
|
|
|
$result = array("total" => $list->total(), "rows" => $list->items(),'attr'=>[
|
|
'statusList'=>$this->model->getStatusList(),
|
|
'typeList'=>$this->model->getTypeList()
|
|
]);
|
|
|
|
return $this->success("获取成功",$result);
|
|
}
|
|
|
|
|
|
/**
|
|
* 切换状态
|
|
* @param $ids
|
|
* @param $status
|
|
* @return void
|
|
*/
|
|
public function status($ids=null,$status=1){
|
|
if (false === $this->request->isPost()) {
|
|
$this->error(__("Invalid parameters"));
|
|
}
|
|
$ids = $ids ?: $this->request->post("ids");
|
|
if (empty($ids)) {
|
|
$this->error(__('Parameter %s can not be empty', 'ids'));
|
|
}
|
|
|
|
$status = $status ?: $this->request->post("status");
|
|
|
|
$pk = $this->model->getPk();
|
|
$adminIds = $this->getDataLimitAdminIds();
|
|
if (is_array($adminIds)) {
|
|
$this->model->where($this->dataLimitField, 'in', $adminIds);
|
|
}
|
|
$list = $this->model->where($pk, 'in', $ids)->select();
|
|
|
|
$count = 0;
|
|
Db::startTrans();
|
|
try {
|
|
foreach ($list as $item) {
|
|
$row = [
|
|
'status'=>$status
|
|
];
|
|
|
|
if($status == 1){
|
|
if(!$item['is_send']){
|
|
//推送消息
|
|
$row['is_send'] = 1;
|
|
|
|
// 修复硬编码 topic 的 Bug:根据消息记录的 course_id 查询对应直播间的 message_topic
|
|
$roomData = (new \app\common\model\live\Room)->courseGetRoom($item['course_id']);
|
|
if($roomData && isset($roomData['message_topic'])){
|
|
$topic = $roomData['message_topic'];
|
|
// 服务商统一由全局配置决定,历史直播间也随全局切换生效
|
|
$provider = \app\common\library\live\message\Manager::getGlobalProviderName();
|
|
|
|
$msgType = \app\common\library\live\message\Message::mapMsgType($item['type']);
|
|
$senderId = $provider === 'aliyun' ? 'system' : '';
|
|
$message = \app\common\model\live\Message::formatMessage($item['user_id'],$item['type'],$item['content'],$item['id']);
|
|
|
|
$result = \app\common\library\live\Message::sendByProvider($provider,$topic,$message,$msgType,$senderId);
|
|
|
|
// 阿里云模式下,回填 msg_tid 用于后续撤回
|
|
if ($provider === 'aliyun' && is_array($result) && isset($result['msg_tid']) && $result['msg_tid']) {
|
|
$row['msg_tid'] = $result['msg_tid'];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
$this->model->where([
|
|
'id'=>$item['id']
|
|
])->update($row);
|
|
$count++;
|
|
}
|
|
Db::commit();
|
|
} catch (PDOException|Exception $e) {
|
|
Db::rollback();
|
|
$this->error($e->getMessage());
|
|
}
|
|
if ($count) {
|
|
$this->success("操作成功");
|
|
}
|
|
$this->error(__('未操作任何数据'));
|
|
}
|
|
|
|
|
|
/**
|
|
* 发送消息
|
|
* @return void
|
|
*/
|
|
public function send(){
|
|
|
|
$content = input('message');
|
|
$type = input('type');
|
|
$courseId = input('course_id');
|
|
|
|
//判断消息服务是否已开启
|
|
if(!\app\common\library\live\message\Manager::isMessageEnabled()){
|
|
$this->error("暂未开启消息服务");
|
|
}
|
|
|
|
if(!$content){
|
|
$this->error("消息内容不能为空");
|
|
}
|
|
|
|
$data = (new \app\common\model\live\Room)->courseGetRoom($courseId);
|
|
|
|
if(!$data){
|
|
$this->error(__('获取课程信息异常'));
|
|
}
|
|
|
|
// sendMessage 内部已根据 course_id 自动选择对应服务商
|
|
\app\common\model\live\Message::sendMessage(0,$courseId,$data['message_topic'],$type,$content,1);
|
|
|
|
$this->success("操作成功");
|
|
}
|
|
|
|
|
|
/**
|
|
* 获取消息列表
|
|
* @return void
|
|
*/
|
|
public function getMessageList(){
|
|
$courseId = input('course_id');
|
|
$offset = input('offset');
|
|
$limit = input('limit');
|
|
$role = input('role');
|
|
|
|
$data = (new \app\common\model\live\Message)->getMessageList($courseId,$offset,$limit,$role);
|
|
|
|
$this->success("获取成功",$data);
|
|
}
|
|
|
|
|
|
/**
|
|
* 获取阿里云互动消息鉴权信息(管理员入口)
|
|
* 管理员进入中控台聊天面板时调用,使用 admin 角色加入群组,具备消息撤回等管理权限
|
|
* @return void
|
|
*/
|
|
public function getAliyunAuth(){
|
|
$courseId = input('course_id');
|
|
|
|
if(!$courseId){
|
|
$this->error("参数错误");
|
|
}
|
|
|
|
//判断消息服务是否已开启
|
|
if(!\app\common\library\live\message\Manager::isMessageEnabled()){
|
|
$this->error("暂未开启消息服务");
|
|
}
|
|
|
|
$roomData = (new \app\common\model\live\Room)->courseGetRoom($courseId);
|
|
if(!$roomData){
|
|
$this->error("获取直播间信息异常");
|
|
}
|
|
|
|
$provider = \app\common\library\live\message\Manager::getGlobalProviderName();
|
|
|
|
if($provider !== 'aliyun'){
|
|
$this->error("当前直播间未使用阿里云消息服务");
|
|
}
|
|
|
|
// 幂等补建阿里云互动消息群组,兼容历史直播间(创建时未建群或群已被删除)
|
|
$courseTitle = \app\admin\model\course\Course::where(['id' => $courseId])->value('name');
|
|
\app\common\library\live\message\Manager::ensureAliyunGroup($roomData['message_topic'], $courseTitle ?: '');
|
|
|
|
// 管理员入口固定使用 admin 角色,userId 使用管理员ID
|
|
$adminId = $this->auth->id;
|
|
$userId = 'admin-' . $adminId;
|
|
|
|
$result = \app\common\library\live\message\Manager::provider('aliyun')->getLoginAuth($userId, 'admin');
|
|
|
|
if(!$result->isSuccess()){
|
|
$this->error($result->error ? $result->error : "获取鉴权信息失败");
|
|
}
|
|
|
|
$authData = $result->data;
|
|
$authData['group_id'] = $roomData['message_topic'];
|
|
|
|
$this->success("获取成功", $authData);
|
|
}
|
|
|
|
|
|
/**
|
|
* 删除
|
|
* @param $ids
|
|
* @return void
|
|
* @throws DbException
|
|
* @throws DataNotFoundException
|
|
* @throws ModelNotFoundException
|
|
*/
|
|
public function del($ids = null)
|
|
{
|
|
if (false === $this->request->isPost()) {
|
|
$this->error(__("Invalid parameters"));
|
|
}
|
|
$ids = $ids ?: $this->request->post("ids");
|
|
if (empty($ids)) {
|
|
$this->error(__('Parameter %s can not be empty', 'ids'));
|
|
}
|
|
$pk = $this->model->getPk();
|
|
$adminIds = $this->getDataLimitAdminIds();
|
|
if (is_array($adminIds)) {
|
|
$this->model->where($this->dataLimitField, 'in', $adminIds);
|
|
}
|
|
$list = $this->model->where($pk, 'in', $ids)->select();
|
|
|
|
$count = 0;
|
|
Db::startTrans();
|
|
try {
|
|
foreach ($list as $item) {
|
|
$roomData = (new \app\common\model\live\Room)->courseGetRoom($item->course_id);
|
|
|
|
if(!$roomData){
|
|
$this->error(__('获取课程信息异常'));
|
|
}
|
|
|
|
// 服务商统一由全局配置决定,历史直播间也随全局切换生效
|
|
$provider = \app\common\library\live\message\Manager::getGlobalProviderName();
|
|
$topic = $roomData['message_topic'];
|
|
|
|
// 阿里云模式优先使用 msg_tid;奥点云模式使用 uuid
|
|
$msgTid = '';
|
|
if ($provider === 'aliyun') {
|
|
$msgTid = isset($item['msg_tid']) ? $item['msg_tid'] : '';
|
|
}
|
|
if (!$msgTid) {
|
|
$msgTid = isset($item['uuid']) ? $item['uuid'] : '';
|
|
}
|
|
|
|
if ($msgTid) {
|
|
\app\common\library\live\Message::delByProvider($provider, $topic, $msgTid);
|
|
}
|
|
|
|
$count += $item->delete();
|
|
}
|
|
Db::commit();
|
|
} catch (PDOException|Exception $e) {
|
|
Db::rollback();
|
|
$this->error($e->getMessage());
|
|
}
|
|
if ($count) {
|
|
$this->success("操作成功");
|
|
}
|
|
$this->error(__('No rows were deleted'));
|
|
}
|
|
|
|
}
|