177 lines
5.3 KiB
PHP
177 lines
5.3 KiB
PHP
<?php
|
|
|
|
namespace app\api\controller\live;
|
|
|
|
use app\common\controller\Api;
|
|
|
|
/**
|
|
* 直播消息
|
|
*/
|
|
class Message extends Api
|
|
{
|
|
// 无需登录的接口,*表示全部
|
|
protected $noNeedLogin = [];
|
|
protected $noNeedRight = '*';
|
|
|
|
|
|
protected $modal;
|
|
|
|
public function _initialize()
|
|
{
|
|
parent::_initialize();
|
|
$this->modal = new \app\api\model\live\Message;
|
|
}
|
|
|
|
/**
|
|
* 发送消息
|
|
* @return void
|
|
*/
|
|
public function send(){
|
|
$content = input('message');
|
|
$type = input('type','text');
|
|
$courseId = input('course_id');
|
|
|
|
//判断消息服务是否已开启
|
|
if(!\app\common\library\live\message\Manager::isMessageEnabled()){
|
|
$this->error("暂未开启消息服务");
|
|
}
|
|
|
|
//判断是否已订阅
|
|
if(!\app\common\model\user\Subscription::getSubscriptionAuth($this->auth->id,$courseId)){
|
|
$this->error("暂未订阅该课程");
|
|
}
|
|
|
|
if(empty($content)){
|
|
$this->error("请完善消息后在再发送");
|
|
}
|
|
|
|
if(mb_strlen($content) > 255){
|
|
$this->error("消息过长,请重新编辑后发送");
|
|
}
|
|
|
|
$roomData = (new \app\common\model\live\Room)->courseGetRoom($courseId);
|
|
|
|
if(!$roomData){
|
|
$this->error(__('获取课程信息异常'));
|
|
}
|
|
|
|
//判断禁用词
|
|
if($roomData['config']['ban_words']){
|
|
$banWords = explode(";",$roomData['config']['ban_words']);
|
|
if($banWords && \app\common\model\live\Message::check($content,$banWords)!==true){
|
|
$this->error("消息包含敏感内容,请重新编辑");
|
|
}
|
|
}
|
|
|
|
if(isset($roomData['config']['bantalk']) && $roomData['config']['bantalk'] == 1){
|
|
$this->error("全员禁言中");
|
|
}
|
|
|
|
$userInfo = (new \app\admin\model\live\User)->joinLive($this->auth->id,$courseId);
|
|
|
|
if($userInfo['black']){
|
|
return $this->error("你已被管理员拉黑,无法发送消息");
|
|
}
|
|
|
|
if($userInfo['bantalk']){
|
|
return $this->error("您已被管理员禁言,无法发送消息");
|
|
}
|
|
|
|
$status = 1;
|
|
//判断是否需要审核
|
|
if($roomData['config']['comment_audit']){
|
|
$status = 0;
|
|
}
|
|
|
|
\app\common\model\live\Message::sendMessage($this->auth->id,$courseId,$roomData['message_topic'],$type,$content,$status);
|
|
|
|
if($roomData['config']['comment_audit']){
|
|
$this->error("消息将在审核通过后展示");
|
|
}
|
|
|
|
$this->success("操作成功");
|
|
}
|
|
|
|
|
|
/**
|
|
* 获取消息列表 历史消息
|
|
* @return void
|
|
*/
|
|
public function getMessageList(){
|
|
$courseId = input('course_id');
|
|
|
|
//判断是否已订阅
|
|
if(!\app\common\model\user\Subscription::getSubscriptionAuth($this->auth->id,$courseId)){
|
|
$this->error("暂未订阅该课程");
|
|
}
|
|
|
|
$limit = input('limit',10);
|
|
$page = input('page',1);
|
|
|
|
|
|
$data = (new \app\common\model\live\Message)->getMessageList($courseId,(($page - 1) * $limit),$limit);
|
|
|
|
if($data){
|
|
foreach ($data as &$item){
|
|
$item['is_my'] = $item['user_id'] == $this->auth->id;
|
|
}
|
|
}
|
|
|
|
$this->success("获取成功",$data);
|
|
}
|
|
|
|
|
|
/**
|
|
* 获取阿里云互动消息鉴权信息
|
|
* 客户端进入阿里云直播间时调用,返回 SDK 登录所需的 appId/appSign/token 等
|
|
* @return void
|
|
*/
|
|
public function getAliyunAuth(){
|
|
$courseId = input('course_id');
|
|
|
|
if(!$courseId){
|
|
$this->error("参数错误");
|
|
}
|
|
|
|
//判断消息服务是否已开启
|
|
if(!\app\common\library\live\message\Manager::isMessageEnabled()){
|
|
$this->error("暂未开启消息服务");
|
|
}
|
|
|
|
//判断是否已订阅
|
|
if(!\app\common\model\user\Subscription::getSubscriptionAuth($this->auth->id,$courseId)){
|
|
$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 则需校验管理员身份
|
|
$role = input('role', '');
|
|
$role = ($role === 'admin') ? 'admin' : '';
|
|
|
|
$result = \app\common\library\live\message\Manager::provider('aliyun')->getLoginAuth($this->auth->id, $role);
|
|
|
|
if(!$result->isSuccess()){
|
|
$this->error($result->error ? $result->error : "获取鉴权信息失败");
|
|
}
|
|
|
|
$authData = $result->data;
|
|
// 附带群组ID,便于客户端直接加入群组
|
|
$authData['group_id'] = $roomData['message_topic'];
|
|
|
|
$this->success("获取成功", $authData);
|
|
}
|
|
} |