初始化项目:添加后端代码、ThinkPHP框架、前端资源
This commit is contained in:
@@ -0,0 +1,103 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\model\live;
|
||||
|
||||
use think\Model;
|
||||
use fast\Random;
|
||||
|
||||
class Room extends Model
|
||||
{
|
||||
|
||||
|
||||
// 表名
|
||||
protected $name = 'live_room';
|
||||
|
||||
// 自动写入时间戳字段
|
||||
protected $autoWriteTimestamp = 'integer';
|
||||
|
||||
// 定义时间戳字段名
|
||||
protected $createTime = 'createtime';
|
||||
protected $updateTime = false;
|
||||
protected $deleteTime = false;
|
||||
|
||||
// 追加属性
|
||||
protected $append = [
|
||||
|
||||
];
|
||||
|
||||
public function getLiveStatusList()
|
||||
{
|
||||
return ['offline' => __('Offline'),'forbidden' => __('Forbidden'),'online' => __('Online'),'onlinefaild'=>__('Onlinefaild'),'end'=>__('end'),'not_start'=>__('not_start'),'run_playback'=>__('run_playback'),'wait_playback'=>__('wait_playback')];
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建直播间
|
||||
* @param $courseId
|
||||
* @param int $liveTime
|
||||
* @return bool
|
||||
*/
|
||||
public static function buildRoom($courseId,$liveTime=0)
|
||||
{
|
||||
|
||||
$streamName = Random::alnum(10);
|
||||
$messageTopic = Random::alnum(10);
|
||||
|
||||
// 直播间不再存储服务商字段,运行时统一使用全局配置 live_basic.live_message_type
|
||||
$provider = \app\common\library\live\message\Manager::getGlobalProviderName();
|
||||
|
||||
$data = [
|
||||
'uniacid'=>UNIACID,
|
||||
'course_id'=>$courseId,
|
||||
'app_name'=>'live',
|
||||
'stream_name'=>$streamName,
|
||||
'message_topic'=>$messageTopic,
|
||||
'live_record_key'=>Random::alnum(10),
|
||||
'createtime'=>time()
|
||||
];
|
||||
|
||||
self::insert($data);
|
||||
|
||||
// 阿里云模式下,同步创建互动消息群组(失败仅记录日志,不阻断直播间创建流程)
|
||||
if ($provider === 'aliyun') {
|
||||
try {
|
||||
$courseName = '';
|
||||
$course = \app\admin\model\course\Course::where(['id' => $courseId])->value('name');
|
||||
if ($course) {
|
||||
$courseName = $course;
|
||||
}
|
||||
|
||||
$result = \app\common\library\live\message\Manager::provider('aliyun')
|
||||
->createGroup($messageTopic, $courseName, '');
|
||||
|
||||
if (!$result->isSuccess()) {
|
||||
\think\Log::error('[live Room buildRoom] aliyun createGroup failed: ' . $result->error . ' course_id=' . $courseId . ' topic=' . $messageTopic);
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
\think\Log::error('[live Room buildRoom] aliyun createGroup exception: ' . $e->getMessage() . ' course_id=' . $courseId);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public function course()
|
||||
{
|
||||
return $this->belongsTo('app\admin\model\Course', 'course_id', 'id', [], 'LEFT')->setEagerlyType(0);
|
||||
}
|
||||
|
||||
|
||||
public function getLiveStatus($courseId){
|
||||
|
||||
$roomData = Room::with(['course'])->where([
|
||||
'course_id'=>$courseId
|
||||
])->find();
|
||||
if(!$roomData){
|
||||
return false;
|
||||
}
|
||||
|
||||
return \app\common\library\live\Room::checkLiveStatus("live",$roomData['stream_name']);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user