初始化项目:添加后端代码、ThinkPHP框架、前端资源
This commit is contained in:
@@ -0,0 +1,277 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\controller\live;
|
||||
|
||||
use app\common\controller\Backend;
|
||||
use think\Db;
|
||||
|
||||
/**
|
||||
* 直播间
|
||||
* @icon fa fa-circle-o
|
||||
*/
|
||||
class Room extends Backend
|
||||
{
|
||||
|
||||
/**
|
||||
* Room模型对象
|
||||
* @var \app\admin\model\live\Room
|
||||
*/
|
||||
protected $model = null;
|
||||
|
||||
//中控台直播接口无需权限节点校验,讲师角色无对应权限节点也可访问
|
||||
protected $noNeedRight = ['getroomdetail', 'getplayurl', 'getlivestatus'];
|
||||
|
||||
public function _initialize()
|
||||
{
|
||||
parent::_initialize();
|
||||
|
||||
\app\common\model\fill\Handle::check('app_config','live');
|
||||
|
||||
$this->model = new \app\admin\model\live\Room;
|
||||
$this->roomConfigModel = new \app\admin\model\live\Config;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 查看
|
||||
*/
|
||||
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();
|
||||
|
||||
$list = $this->model
|
||||
->with(['course'])
|
||||
->where($where)
|
||||
->order($sort, $order)
|
||||
->paginate($limit);
|
||||
|
||||
foreach ($list as $row) {
|
||||
|
||||
|
||||
}
|
||||
|
||||
$result = array("total" => $list->total(), "rows" => $list->items());
|
||||
|
||||
return $this->success("获取成功",$result);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取直播状态
|
||||
* @return void
|
||||
*/
|
||||
public function getLiveStatus(){
|
||||
$courseId = input("course_id");
|
||||
|
||||
|
||||
$roomStatus = (new \app\common\model\live\Room)->getRoomStatus($courseId);
|
||||
|
||||
return $this->success("获取成功",[
|
||||
'status'=>$roomStatus,
|
||||
'status_text'=>__($roomStatus)
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取在线用户数
|
||||
* @return mixed
|
||||
*/
|
||||
public function getOnlineUserNum(){
|
||||
$courseId = input("course_id");
|
||||
|
||||
$data = $this->model->where([
|
||||
'course_id'=>$courseId
|
||||
])->find();
|
||||
|
||||
if(!$data){
|
||||
return $this->error("未找到相关记录");
|
||||
}
|
||||
|
||||
$provider = \app\common\library\live\message\Manager::getGlobalProviderName();
|
||||
|
||||
$onlineData = \app\common\library\live\Message::getOnlineUserByProvider($provider, $data['message_topic']);
|
||||
|
||||
if($onlineData && isset($onlineData['total'])){
|
||||
return $this->success("获取成功",$onlineData['total']);
|
||||
}
|
||||
|
||||
// 阿里云超级大群等场景无法获取具体数量时返回 0,保持接口可用
|
||||
if ($provider === 'aliyun') {
|
||||
return $this->success("获取成功", 0);
|
||||
}
|
||||
|
||||
return $this->error("获取失败");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取推流地址
|
||||
* @return void
|
||||
*/
|
||||
public function getPushUrl(){
|
||||
$courseId = input("course_id");
|
||||
|
||||
$data = $this->model->where([
|
||||
'course_id'=>$courseId
|
||||
])->find();
|
||||
|
||||
if(!$data){
|
||||
return $this->error("未找到相关记录");
|
||||
}
|
||||
|
||||
$course = \app\admin\model\course\Course::where([
|
||||
'id'=>$courseId
|
||||
])->field('live_type')->find();
|
||||
|
||||
$liveType = $course ? $course['live_type'] : 1;
|
||||
|
||||
$urls = \app\common\library\live\Url::pushUrl($data['app_name'],$data['stream_name'],[
|
||||
'callback_record_key'=>$data['live_record_key'],
|
||||
'callback_uniacid'=>UNIACID
|
||||
]);
|
||||
|
||||
return $this->success("获取成功",[
|
||||
'urls'=>$urls,
|
||||
'live_type'=>$liveType
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取播流地址
|
||||
* @return void
|
||||
*/
|
||||
public function getPlayUrl(){
|
||||
$courseId = input("course_id");
|
||||
|
||||
$data = $this->model->where([
|
||||
'course_id'=>$courseId
|
||||
])->find();
|
||||
|
||||
if(!$data){
|
||||
return $this->error("未找到相关记录");
|
||||
}
|
||||
|
||||
$course = \app\admin\model\course\Course::where([
|
||||
'id'=>$courseId
|
||||
])->field('live_type,live_custom_url')->find();
|
||||
|
||||
if($course && $course['live_type'] == 3){
|
||||
$urls = [
|
||||
'hls'=>$course['live_custom_url']
|
||||
];
|
||||
}else{
|
||||
$urls = \app\common\library\live\Url::playUrl($data['app_name'],$data['stream_name']);
|
||||
}
|
||||
|
||||
return $this->success("获取成功",$urls);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取课程配置
|
||||
* @return mixed
|
||||
*/
|
||||
public function getConfig(){
|
||||
$courseId = input("course_id");
|
||||
$type = input("type");
|
||||
|
||||
|
||||
$data = $this->roomConfigModel->getCourseConfig($courseId,$type);
|
||||
|
||||
return $this->success("获取成功",$data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置课程配置
|
||||
* @return mixed
|
||||
*/
|
||||
public function setConfig(){
|
||||
$courseId = input("course_id");
|
||||
$row = $this->request->post("row/a", [], 'trim');
|
||||
|
||||
$this->roomConfigModel->adjustConfig($courseId,$row);
|
||||
|
||||
return $this->success("操作成功");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取直播间详情
|
||||
* @return void
|
||||
*/
|
||||
public function getRoomDetail(){
|
||||
$courseId = input("course_id");
|
||||
//讲师只能访问自己关联的直播
|
||||
if ($this->auth->isLecturer()) {
|
||||
$exists = Db::name('live_lecturer')->where([
|
||||
'course_id' => $courseId,
|
||||
'admin_id' => $this->auth->id
|
||||
])->count();
|
||||
if (!$exists) {
|
||||
$this->error("无权限访问该直播");
|
||||
}
|
||||
}
|
||||
$data = (new \app\common\model\live\Room)->courseGetRoom($courseId);
|
||||
|
||||
$data['message_key'] = \app\common\model\config\System::getConfig('aodianyun')['pubkey'];
|
||||
|
||||
// 直播间使用的消息服务商统一由全局配置决定,历史直播间也随全局切换生效
|
||||
$data['message_provider'] = \app\common\library\live\message\Manager::getGlobalProviderName();
|
||||
|
||||
// 消息服务是否已开启
|
||||
$data['message_enabled'] = \app\common\library\live\message\Manager::isMessageEnabled() ? 1 : 0;
|
||||
|
||||
// 阿里云模式下,额外返回客户端 SDK 所需的 AppId 与 AppSign
|
||||
if ($data['message_provider'] === 'aliyun') {
|
||||
$aliyunMessageConfig = \app\common\model\config\System::getConfig('aliyun_message');
|
||||
$data['aliyun_app_id'] = (is_array($aliyunMessageConfig) && isset($aliyunMessageConfig['app_id'])) ? $aliyunMessageConfig['app_id'] : '';
|
||||
$data['aliyun_app_sign'] = (is_array($aliyunMessageConfig) && isset($aliyunMessageConfig['app_sign'])) ? $aliyunMessageConfig['app_sign'] : '';
|
||||
}
|
||||
|
||||
return $this->success("操作成功",$data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 延长直播
|
||||
* @return void
|
||||
*/
|
||||
public function lengthenLive(){
|
||||
$courseModel = new \app\admin\model\course\Course;
|
||||
$courseId = input("ids");
|
||||
|
||||
$row = $this->request->post("row/a", [], 'trim');
|
||||
|
||||
$row['live_start_time'] = strtotime($row['live_start_time']);
|
||||
$row['live_end_time'] = strtotime($row['live_end_time']);
|
||||
|
||||
$courseModel->where([
|
||||
'id'=>$courseId
|
||||
])->update($row);
|
||||
|
||||
return $this->success("操作成功");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 结束直播
|
||||
*/
|
||||
public function endLive(){
|
||||
$courseModel = new \app\admin\model\course\Course;
|
||||
$courseId = input("ids");
|
||||
|
||||
$courseModel->where([
|
||||
'id'=>$courseId
|
||||
])->update([
|
||||
'live_end_time'=>time()
|
||||
]);
|
||||
|
||||
return $this->success("操作成功");
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user