love_php/app/Models/Server/MerchantLive.php
2026-04-02 09:20:51 +08:00

184 lines
5.5 KiB
PHP

<?php
namespace App\Models\Server;
use App\Models\WangYiYunUser;
use Illuminate\Database\Eloquent\Model;
use App\Services\IMService;
class MerchantLive extends Model
{
protected $fillable = [];
protected $guarded = [];
//商户信息
public function merchant()
{
return $this->belongsTo(MerchantAccount::class,'merchant_id');
}
// 直播流信息
public function channel()
{
return $this->hasOne(MerchantLiveChannels::class, 'live_id', 'id');
}
//直播布局
public function layout()
{
$merchant = $this->merchant;
$accid = 1000000+$merchant->id;
// 查看网易云账户
$exists = WangYiYunUser::where('accid', $accid)->exists();
if(!$exists){
$data['name'] = $merchant->anchorV2->name;
$data['pic'] = $merchant->anchorV2->pic;
$data['mobile'] = $merchant->anchorV2->mobile;
$data['anchor_id'] = $merchant->anchorV2->id;
$data['accid'] = $accid;
MerchantAccount::createIMUser($data);
}
//创建聊天室
// $chat_room = $this->createChatRoom($accid,$this->title);
// if (empty($chat_room)) throw new \Exception("创建IM聊天室失败", 1);
// $this->chat_room_id = $chat_room['roomid'];
$this->save();
}
/**
* 创建聊天室
* @param [type] $creator [description]
* @param [type] $name [description]
* @param [type] $announcement [description]
* @return [type] [description]
*/
public function createChatRoom($creator, $name, $announcement='', $broadcasturl='', $ext=[], $queuelevel=0)
{
try {
$im = new IMService(env('IM_APP_KEY'), env('IM_APP_SECRET'));
$result = $im->chatRoomCreate($creator, $name, $announcement, $broadcasturl, $ext, $queuelevel);
if (empty($result) || $result['code'] != 200) {//失败
throw new \Exception("创建IM聊天室请求失败", 1);
}
return $result['chatroom'];
} catch (\Exception $e) {
$this->getError($e);
return false;
}
}
/**
* 创建直播流地址
* @param [type] $name [description]
* @param [type] $type [description]
* @return [type] [description]
*/
public function createLiveChannel($name)
{
//判断数据是否有对应流
$channel = $this->channel;
if (empty($channel)) {
$im = new IMService();
$result = $im->channelCreate($name,0);
if (empty($result) || $result['code'] != 200) {//失败
}
if(!$result || !isset($result['ret'])){
return false;
}
$ret = $result['ret'];
//创建直播流
$channel = new MerchantLiveChannels();
$channel->name = $name;
$channel->live_id = $this->id;
$channel->push_url = $ret['pushUrl'];
$channel->http_pull_url = $ret['httpPullUrl'];
$channel->hls_pull_url = $ret['hlsPullUrl'];
$channel->rtmp_pull_url = $ret['rtmpPullUrl'];
$channel->cid = $ret['cid'];
$channel->save();
}
return $channel;
}
/**
* [addRtmpTask 增加一个房间推流任务V2]
* @param [type] $channelName [description]
*/
public function addRtmpTask($accid)
{
try {
$live_name = $this->title;
$channel = MerchantLiveChannels::where('live_id', $this->id)->first();
if (empty($channel)) throw new \Exception("直播频道不存在", 1);
//加入直播间 todo
$task = [
"taskId"=>$channel->cid,
"streamUrl"=>$channel->push_url,
"layoutMode"=>"M-0",
"record"=>true,
"accid"=>$accid,
];
//增加一个房间推流任务
// $im = new IMService();
// $result = $im->addRtmpTaskV2($live_name, $this->channel_id, $task);
// if (empty($result) || $result['code'] != 200) {//失败
// throw new \Exception("增加一个房间推流任务失败", 1);
// }
return true;
} catch (\Exception $e) {
$this->getError($e);
return false;
}
}
public function channelRoom()
{
try {
if (empty($this->channel_id)) return false;
$im = new IMService();
$result = $im->liveRoom($this->channel_id);
if (is_array($result) && isset($result['cid'])) {
return $result;
}
return false;
} catch (\Exception $e) {
$this->getError($e);
return false;
}
}
public function addLiveLog()
{
$LiveLog = new MerchantLiveLog();
$LiveLog->merchant_id = $this->merchant_id;
$LiveLog->live_id = $this->id;
$LiveLog->start_time = now();
$LiveLog->save();
}
public function getPosterAttribute($value)
{
if ($this->merchant_id == 491) {
if ($value) {
if (!strpos('local-pictures', $value) && !strpos($value, 'x-oss-process')) {
return $value . Config('image.watermark');
} else {
return $value;
}
} else {
return $value;
}
}else{
return $value;
}
}
}