文件
xionggeshop/addons/shopro/controller/Live.php

163 行
4.1 KiB
PHP

<?php
namespace addons\shopro\controller;
use addons\shopro\library\Wechat;
use addons\shopro\exception\Exception;
use think\Cache;
use think\Db;
class Live extends Base
{
protected $noNeedLogin = ['*'];
protected $noNeedRight = ['*'];
/**
* 获取直播地址
* @return \think\response\Json
* @throws \think\Exception
* @throws \think\exception\PDOException
*/
public function getLiveAddr()
{
$user = $this->auth->getUser();
if (empty($user)) {
return json([
'code' => 401,
'msg' => '请先登录'
]);
}
if ($user->is_live == 2) {
return json([
'code' => 402,
'msg' => '您没有直播权限'
]);
}
$liveUrl = getPushUrl(config('live.live_domain'), 'shop', config('live.live_key'), date('Y-m-d H:i:s', time() + 86400));
// 更新成直播状态
Db::name('user')
->update([
'id' => $user->id,
'live_status' => 1
]);
return json([
'code' => 200,
'msg' => '获取直播地址成功',
'data' => [
'liveUrl' => $liveUrl
]
]);
}
/**
* 获取播放地址
* @return \think\response\Json
*/
public function getVideoUrl()
{
$check = input('param.check/d', 0);
$id = Db::name('user')->where(['live_status' => 1])->value('id');
if (!empty($check)) {
return json([
'code' => 200,
'msg' => '获取成功',
'data' => [
'live' => !empty($id) ? 1 : 2,
]
]);
}
if (empty($id)) {
return json([
'code' => 200,
'msg' => '获取成功',
'data' => [
'live' => 2,
'liveUrl' => ''
]
]);
}
$url = getVideoUrl(config('live.video_domain'), 'shop', config('live.video_key'), date('Y-m-d H:i:s', time() + 86400));
return json([
'code' => 200,
'msg' => '获取成功',
'data' => [
'live' => 1,
'liveUrl' => $url
]
]);
}
/**
* 直播结束
* @return \think\response\Json
* @throws \think\Exception
* @throws \think\exception\PDOException
*/
public function endLive()
{
$user = $this->auth->getUser();
if (empty($user)) {
return json([
'code' => 401,
'msg' => '请先登录'
]);
}
// 更新成直播状态
Db::name('user')
->update([
'id' => $user->id,
'live_status' => 2
]);
return json([
'code' => 200,
'msg' => '直播结束成功',
]);
}
public function index()
{
$params = $this->request->get();
$type = $params['type'] ?? 'all';
$ids = array_filter(isset($params['ids']) ? explode(',', $params['ids']) : []);
if (!in_array($type, ['all', 'notice', 'living', 'lived'])) {
$this->error('参数错误');
}
// 同步直播
\addons\shopro\model\Live::autoSyncLive();
if ($type != 'all') {
$lives = \addons\shopro\model\Live::{$type}()
->with('goods')
->order('id', 'desc')
->paginate(10);
} else {
$lives = \addons\shopro\model\Live::order('live_status', 'asc')
->with('goods')
->order('id', 'desc');
if (isset($params['ids'])) {
// 首页根据 id 获取
$lives = $lives->where('id', 'in', $ids)->select();
} else {
// 直播列表
$lives = $lives->paginate(10);
}
}
$this->success('获取成功', $lives);
}
}