<?php
namespace app\api\controller;
use app\common\business\Live as LiveBus;
use app\common\business\User;
use app\common\lib\CodeResponse;
use app\common\lib\im\Tencent\TencentIm;
use app\common\lib\live\Tencent\TencentLive;
use app\common\lib\Show;
use app\common\model\mysql\LogicAdminGift;
use app\common\model\mysql\LogicAdminGuard;
use app\common\model\mysql\LogicAdminInstructions;
use app\common\model\mysql\LogicAdminPic;
use app\common\model\mysql\LogicGiftCategory;
use app\common\model\mysql\LogicLiveAdministrator;
use app\common\model\mysql\LogicLiveCategory;
use app\common\model\mysql\LogicLiveCategoryReport;
use app\common\model\mysql\LogicLiveHouse;
use app\common\model\mysql\LogicLiveRecord;
use app\common\model\mysql\LogicLiveReportAnchor;
use app\common\model\mysql\LogicUser;
use app\common\model\mysql\LogicUserAttention;
use app\common\model\mysql\LogicUserBlacklist;
use app\common\model\mysql\LogicUserGuard;
use app\common\model\mysql\LogicUserLabel;
use app\common\model\mysql\LogicUserWallet;
use app\common\model\mysql\LogicUserWatchRecord;
use think\facade\Db;
use think\facade\Log;
class Live extends BaseController
{
/**
* 获取直播分类
*
* @return \think\response\Json
*/
public function getLiveCategory ()
{
try {
$res = LogicLiveCategory::field('id,name,icon')
->where('status', CodeResponse::STATUS_SUCCESS)
->order('sort desc,id desc')
->select();
} catch (\Exception $e) {
Log::info($e->getMessage());
return Show::error(CodeResponse::SYSTEM_ANOMALY);
}
if ($res->isEmpty()) {
return Show::error();
}
return Show::successData($res);
}
/**
* 获取个板块下的直播间
*
* @return \think\response\Json
* @throws \ReflectionException
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function indexInfo ()
{
//统一参数校验
$request = $this->validateData(input(), 'live', 'indexInfo');
//判断那种板块
switch ($request['type']) {
case 1:
//头条
$res = LiveBus::indexHeadline($request);
break;
case 2:
//推荐
$res = LiveBus::indexRecommend($request);
break;
case 3:
//pk
$res = LiveBus::indexPk($request);
break;
case 4:
//新秀
$res = LiveBus::indexSamsonite($request);
break;
case 5:
//热门
$res = LiveBus::indexHot($request);
break;
case 6:
//附近主播
$res = LiveBus::indexNearby($request);
break;
case 7:
//手机直播
$res = LiveBus::indexIsPhone($request);
break;
case 8:
//关注
$res = LiveBus::indexAttention($request);
break;
default:
Show::error(CodeResponse::SYSTEM_ANOMALY);
}
$res = LiveBus::getAnchorLiveInfo($res);
return Show::successData($res ?: []);
}
/**
* 开始直播
*
* @return bool|\think\response\Json
* @throws \ReflectionException
*/
public function anchorBeginLive ()
{
//统一参数校验
$request = $this->validateData(input(), 'live', 'anchorBeginLive');
try {
//判断用户身份
$user = LogicUser::where('id', $request['uid'])->find();
if (!$user) {
return Show::error(CodeResponse::NOT_USER_MESSAGE);
}
if ($user->anchor !== 1) {
return Show::error(CodeResponse::NOT_ANCHOR);
}
//获取推拉流地址
$live = TencentLive::getPushUrl($user->sole_id);
//判断用户是否第一次开播
$anchor = LogicLiveHouse::where('uid', $request['uid'])->find();
//获得主播的坐标
$longitude
= (new \app\common\business\Live())->getUserCoordinate($request['uid']);
$logicHouse = new LogicLiveHouse();
if (!$anchor) {
Db::startTrans();
$data = [
'uid' => $request['uid'],
'live_sole_id' => $request['sole_id'],
'live_name' => $request['live_name'],
'live_image' => $request['live_image'],
'live_push_site' => $live['push_url'],
'live_pull_site' => $live['flv_play_url'],
'live_category_id' => $request['live_category_id'],
'is_phone' => $request['is_phone'] ?? 0,
'lat' => $longitude['result']['location']['lat'],
'lng' => $longitude['result']['location']['lng'],
];
$logicHouse->save($data);
$recordData = [
'uid' => $request['uid'],
'live_id' => $logicHouse->id,
'live_name' => $request['live_name'],
'live_image' => $request['live_image'],
'live_play_time' => time(),
];
(new LogicLiveRecord())->save($recordData);
Db::commit();
//创建直播群
$createGroup = TencentIm::createGroup($user->sole_id,
$user->sole_id);
$createGroup = json_decode($createGroup, true);
if ($createGroup['ErrorCode'] != 0) {
return Show::error(CodeResponse::OPEN_LIVE_ERROR);
}
$logicHouse->live_group_id = $createGroup['GroupId'];
$logicHouse->save();
} else {
Db::startTrans();
//修改直播表
$anchor->live_name = $request['live_name'];
$anchor->live_image = $request['live_image'];
$anchor->live_push_site = $live['push_url'];
$anchor->live_pull_site = $live['flv_play_url'];
$anchor->live_category_id = $request['live_category_id'];
$anchor->is_phone = $request['is_phone'] ?? 0;
$anchor->lat = $longitude['result']['location']['lat'];
$anchor->lng = $longitude['result']['location']['lng'];
$anchor->save();
$recordData = [
'uid' => $request['uid'],
'live_id' => $anchor->id,
'live_name' => $request['live_name'],
'live_image' => $request['live_image'],
'live_play_time' => time(),
];
(new LogicLiveRecord())->save($recordData);
Db::commit();
}
} catch (\Exception $e) {
Db::rollback();
Log::info($e->getMessage());
return Show::error(CodeResponse::OPEN_LIVE_ERROR);
}
return Show::successData($live, CodeResponse::OPEN_LIVE_SUCCESS);
}
/**
* 获取直播间守护管理信息
*
* @return bool|\think\response\Json
* @throws \ReflectionException
*/
public function getGuardLive ()
{
//统一参数校验
$request = $this->validateData(input(), 'live', 'getGuardLive');
try {
//获取未到期的直播间守护
$res = LogicUserGuard::with('user')
->where('live_id', $request['live_id'])
->where('end_time', '>', time())
->where('status', 1)
->select();
$guard = LogicAdminGuard::column('');
$arr = [];
foreach ($res as $v) {
$arr[] = [
'head_portrait' => $v->user->head_portrait,
'nickname' => $v->user->nickname,
'guard_type' => $v->guard_type,
];
}
} catch (\Exception $e) {
Log::info($e->getMessage());
return Show::error(CodeResponse::SYSTEM_ANOMALY);
}
return Show::successData($arr);
}
/**
* 获取各分类下的直播间
*
* @return bool|\think\response\Json
* @throws \ReflectionException
*/
public function getLiveCategoryHouse ()
{
//统一参数校验
$request = $this->validateData(input(), 'live',
'getLiveCategoryHouse');
try {
$where = [];
if (!empty($request['live_category_id'])) {
$where[] = [
'live_category_id', '=', $request['live_category_id'],
];
}
$res = LogicLiveHouse::where($where)
->where('live_status', 1)
->with('user.address')
->page($request['page'] ?? 1, 20)
->order('id desc')
->select();
$arr = LiveBus::getAnchorLiveInfo($res);
} catch (\Exception $e) {
Log::info($e->getMessage());
return Show::error(CodeResponse::SYSTEM_ANOMALY);
}
if ($arr) {
return Show::successData($arr);
}
return Show::successData();
}
/**
* 获取观看记录
*
* @return bool|\think\response\Json
* @throws \ReflectionException
*/
public function getWatchTheRecord ()
{
//统一参数校验
$request = $this->validateData(input(), 'user',
'get_user_message');
try {
$res = LogicUserWatchRecord::with('live.user.address')
->where('uid', $request['id'])
->page($request['page'] ?? 1, 20)
->order('id desc')
->select();
$arr = LiveBus::getAnchorLiveInfo($res, 2);
} catch (\Exception $e) {
Log::info($e->getMessage());
return Show::error(CodeResponse::SYSTEM_ANOMALY);
}
if ($arr) {
return Show::successData($arr);
}
return Show::successData();
}
/**
* 获取轮播图信息
*
* @return \think\response\Json
*/
public function getCarousel ()
{
//统一参数校验
$request = $this->validateData(input(), 'live', 'getCarousel');
$res = LogicAdminPic::where('status', CodeResponse::STATUS_SUCCESS)
->limit($request['limit'])
->order('sort desc,id desc')
->field('pic_name,pic_image,link')
->select();
if (!$res->isEmpty()) {
return Show::successData($res);
}
return Show::successData();
}
/**
* 关注和取消关注
*
* @return \think\response\Json
*/
public function setFocusOn ()
{
//统一参数校验
$request = $this->validateData(input(), 'live', 'setFocusOn');
//不能是自己
if ($request['uid'] == $request['follower_id']) {
return Show::error(CodeResponse::USER_NOT_ATTENTION);
}
$res = LogicUserAttention::where('uid', $request['uid'])
->where('follower_id', $request['follower_id'])
->find();
//关注
if ($request['type'] == 1) {
if ($res) {
$res->state = 1;
$res = $res->save();
} else {
$res = LogicUserAttention::create([
'uid' => $request['uid'],
'follower_id' => $request['follower_id'],
'state' => 1,
]);
}
if ($res) {
return Show::successMessage(CodeResponse::FOCUS_SUCCESS);
}
return Show::successMessage(CodeResponse::FOCUS_ERROR);
} else {
//取消关注
$res->state = 2;
$res = $res->save();
if ($res) {
return Show::successMessage(CodeResponse::CANCEL_FOCUS_SUCCESS);
}
return Show::successMessage(CodeResponse::CANCEL_FOCUS_ERROR);
}
}
/**
* 搜索直播间
*
* @return \think\response\Json
*/
public function getSearch ()
{
//统一参数校验
$request = $this->validateData(input(), 'live', 'getSearch');
$res = LogicLiveHouse::hasWhere('user',
function ($query) use ($request) {
$preg = preg_match('/^[0-9]{10}$/', $request['search']);
if (!$preg) {
$query->whereLike('nickname',
'%' . $request['search'] . '%');
} else {
$query->where('sole_id', $request['search']);
}
})->with(['user.address', 'userClass.adminClass'])
// ->where('live_status', 1)
->page($request['page'] ?? 1, 20)
->order('look_person desc,id desc')
->select();
if (!$res->isEmpty()) {
$res = LiveBus::getAnchorLiveInfo($res);
//获取主播等级图标
$ids = array_column($res, 'live_id');
$class = LogicLiveHouse::whereIn('a.id', $ids)
->field('a.id,b.class_icon')
->alias('a')
->leftJoin('logic_user_class c', 'a.uid = c.uid')
->leftJoin('logic_admin_class b', 'c.class_id = b.id')
->where('c.type', 2)
->where('b.type', 2)
->select()->toArray();
$classArr = array_column($class, 'class_icon', 'id');
foreach ($res as &$v) {
$v['class_icon'] = $classArr[$v['live_id']];
$atten = LogicUserAttention::where('uid', $request['uid'])
->where('follower_id', $v['uid'])
->where('state', 1)
->find();
$v['is_attention'] = $atten ? 1 : 0;
}
unset($v);
return Show::successData($res ?: []);
}
return Show::successData();
}
/**
* 获取所有守护信息
*
* @return \think\response\Json
*/
public function getAdminGuard ()
{
//获取后台守护管理信息
$res = LogicAdminGuard::where('status', 1)
->order('sort desc,id desc')
->select();
$guard_category = [
'1' => '月守护',
'2' => '季守护',
'3' => '年守护',
];
$data = [];
foreach ($res as $v) {
$data[] = [
'guard_name' => $guard_category[$v['guard_type']],
'guard_icon' => $v['guard_icon'],
'price' => $v['price'],
'discount' => $v['discount'],
'discount_type' => $v['discount_type'],
'discount_price' => bcmul($v['price'],
bcdiv($v['discount_type'], 10, 2), 2),
];
}
if ($res) {
return Show::successData($data);
}
return Show::successData();
}
/**
* 开通和续费守护
*
* @return \think\response\Json
*/
public function setLiveGuard ()
{
//统一参数校验
$request = $this->validateData(input(), 'live', 'setLiveGuard');
//检查是否存在用户
$user = LogicUser::find($request['uid']);
if (!$user) {
return Show::error(CodeResponse::NOT_USER_MESSAGE);
}
//判断用户是否有开通的守护
$userIssetGuard = LogicUserGuard::where('uid', $request['uid'])
->where('live_id', $request['live_id'])
->where('status', 1)
->find();
if ($request['type'] == 1) {
if ($userIssetGuard) {
return Show::error(CodeResponse::USER_OPEN_GUARD);
}
} else {
if (!$userIssetGuard) {
return Show::error(CodeResponse::USER_NOT_LIVE_GUARD);
}
}
//获取该守护类型的价格
$adminGuard = LogicAdminGuard::where('guard_type',
$request['guard_type'])
->find();
if (!$adminGuard) {
return Show::error(CodeResponse::SYSTEM_ANOMALY);
}
//检查是否余额充足
$userWallet = LogicUserWallet::where('uid', $request['uid'])->find();
if ($userWallet->price < $adminGuard->price) {
return Show::error(CodeResponse::USER_WALLET_ERROR);
}
//扣掉余额开通守护
$userWallet->price = bcsub($userWallet->price, $adminGuard->price, 2);
$userWallet->save();
//获取守护时长
$time = LiveBus::getGuardTime($request['guard_type']);
if ($request['type'] == 1) {
//判断以前是否开通过该类型的守护
$userGuard = LogicUserGuard::where('uid', $request['uid'])
->where('live_id', $request['live_id'])
->where('guard_type', $request['guard_type'])
->find();
//开通守护
if ($userGuard) {
$userGuard->start_time = time();
$userGuard->end_time = time() + $time;
$userGuard->status = 1;
$res = $userGuard->save();
} else {
$res = LogicUserGuard::create([
'uid' => $request['uid'],
'live_id' => $request['live_id'],
'guard_id' => $adminGuard->id,
'guard_type' => $request['guard_type'],
'start_time' => time(),
'end_time' => time() + $time,
'status' => 1,
]);
}
if ($res) {
return Show::successMessage(CodeResponse::OPEN_GUARD_SUCCESS);
}
return Show::error(CodeResponse::OPEN_GUARD_ERROR);
} else {
//续费守护
$userIssetGuard->guard_type = $request['guard_type'];
$userIssetGuard->end_time = $userIssetGuard->end_time + $time;
$res = $userIssetGuard->save();
if ($res) {
return Show::successMessage(CodeResponse::USER_RENEWAL_SUCCESS);
}
return Show::error(CodeResponse::USER_RENEWAL_ERROR);
}
}
/**
* 获取用户直播间守护信息
*
* @return \think\response\Json
*/
public function getUserLiveGuard ()
{
//统一参数校验
$request = $this->validateData(input(), 'live', 'getUserLiveGuard');
$guard = LogicUserGuard::where('uid', $request['uid'])
->where('live_id', $request['live_id'])
->field('guard_type,end_time')
->find();
$guard_category = [
'1' => '月守护',
'2' => '季守护',
'3' => '年守护',
];
$guard->end_time = date('Y-m-d H:i:s', $guard->end_time);
$guard->guard_type = $guard_category[$guard->guard_type];
if ($guard) {
return Show::successData($guard);
}
return Show::successMessage();
}
/**
* 获取直播文字说明配置
*
* @return \think\response\Json
*/
public function getInstructions ()
{
$res = LogicAdminInstructions::withoutField('id,create_time')->find(1);
if ($res) {
return Show::successData($res);
}
return Show::successData();
}
/**
* 获取直播间举报类型
*
* @return \think\response\Json
*/
public function getReportCategory ()
{
$res = LogicLiveCategoryReport::where('status', 1)
->field('name,id')
->select();
if ($res) {
return Show::successData($res);
}
return Show::successData();
}
/**
* 直播间举报
*
* @return \think\response\Json
*/
public function setLiveReport ()
{
//统一参数校验
$request = $this->validateData(input(), 'live', 'setLiveReport');
$res = LogicLiveReportAnchor::create([
'uid' => $request['uid'] ?? '',
'live_id' => $request['live_id'] ?? '',
'anchor_id' => $request['anchor_id'] ?? '',
'type' => $request['type'] ?? '',
'content' => $request['content'] ?? '',
'image' => $request['image']
? json_encode($request['image'])
: '',
'contact_information' => $request['contact_information'] ?? '',
]);
if ($res) {
return Show::successMessage(CodeResponse::FEEDBACK_SUCCESS);
}
return Show::error(CodeResponse::FEEDBACK_ERROR);
}
/**
* 设置和取消用户为直播间管理员
*
* @return \think\response\Json
*/
public function setImAdministrator ()
{
//统一参数校验
$request = $this->validateData(input(), 'live', 'setImAdministrator');
//获取用户IM的id
$user = LogicUser::field('sole_id')->find($request['uid']);
// $anchor = LogicUser::field('sole_id')->find($request['anchor_id']);
//在IM设为管理员
(new User())->setImPortraitIdentity($user->sole_id,
$request['type'] == 1 ? CodeResponse::IDENTITY_SUCCESS
: CodeResponse::IDENTITY_ERROR);
// $admin = TencentIm::modifyGroupMemberInfo($anchor->sole_id,
// $user->sole_id);
// $admin = json_decode($admin, true);
// if ($admin['ErrorCode'] != 0) {
// Show::ExitResponse(CodeResponse::SET_IM_USER_Admin_ERROR);
// }
if ($request['type'] == 1) {
//服务端设为管理员
$userAdmin = LogicLiveAdministrator::create([
'uid' => $request['uid'],
'live_id' => $request['live_id'],
]);
if ($userAdmin) {
return Show::successMessage(CodeResponse::SETUP_ADMINISTRATOR_SUCCESS);
}
return Show::error(CodeResponse::SETUP_ADMINISTRATOR_ERROR);
} else {
$userAdmin = LogicLiveAdministrator::where('uid', $request['uid'])
->where('live_id', $request['live_id'])
->find();
if (!$userAdmin) {
return Show::error(CodeResponse::USER_NOT_ADMINISTRATOR);
}
$userAdmin->delete();
if ($userAdmin) {
return Show::successMessage(CodeResponse::CANCEL_ADMINISTRATOR_SUCCESS);
}
return Show::error(CodeResponse::CANCEL_ADMINISTRATOR_ERROR);
}
}
/**
* 用户进入直播间
*
* @return \think\response\Json
*/
public function enterLiveHouse ()
{
//统一参数校验
$request = $this->validateData(input(), 'live', 'enterLiveHouse');
//获取用户最新标签
$userLabel = LogicUserLabel::with(['adminLabel'])
->where('uid', $request['uid'])
->order('sort desc,id desc')
->select();
foreach ($userLabel as $v) {
$arr[] = substr($v->adminLabel->label_icon, -34);
}
//获取用户等级标签
$userBus = (new User());
$userIcon = $userBus->getUserClassIcon($request['uid']);
array_unshift($arr, $userIcon);
$arr = implode(',', $arr);
//重置用户最新的标签到腾讯IM
$user = LogicUser::field('sole_id')->find($request['uid']);
$userBus->setImPortraitLabel($user->sole_id, $arr);
if ($arr) {
return Show::successMessage();
}
return Show::error();
}
/**
* 拉黑用户和取消拉黑用户
*
* @return \think\response\Json
*/
public function setImBlackUser ()
{
//统一参数校验
$request = $this->validateData(input(), 'live', 'setImBlackUser');
//获取用户IM的id
$user = LogicUser::field('sole_id')->find($request['uid']);
$userBlack = LogicUser::field('sole_id')->find($request['black_id']);
if ($request['type'] == 1) {
$userAdmin = LogicUserBlacklist::where('uid', $request['uid'])
->where('black_id', $request['black_id'])
->find();
if ($userAdmin) {
return Show::error(CodeResponse::USER_SET_BLACK);
}
//在IM拉黑
$im = TencentIm::blackListAdd($user->sole_id, [$userBlack->sole_id]);
$im = json_decode($im, true);
if ($im['ErrorCode'] != 0) {
return Show::error(CodeResponse::SET_IM_USER_BLACK_ERROR);
}
//服务端设为管理员
$userAdmin = LogicUserBlacklist::create([
'uid' => $request['uid'],
'black_id' => $request['black_id'],
]);
if ($userAdmin) {
return Show::successMessage(CodeResponse::SET_IM_USER_BLACK_SUCCESS);
}
return Show::error(CodeResponse::SET_IM_USER_BLACK_ERROR);
} else {
$userAdmin = LogicUserBlacklist::where('uid', $request['uid'])
->where('black_id', $request['black_id'])
->find();
if (!$userAdmin) {
return Show::error(CodeResponse::USER_NOT_BLACK);
}
//在IM取消拉黑
$im = TencentIm::blackListDelete($user->sole_id, [$userBlack->sole_id]);
$im = json_decode($im, true);
if ($im['ErrorCode'] != 0) {
return Show::error(CodeResponse::SET_IM_USER_BLACK_ERROR);
}
$userAdmin->delete();
if ($userAdmin) {
return Show::successMessage(CodeResponse::NOT_USER_BLACK_SUCCESS);
}
return Show::error(CodeResponse::NOT_USER_BLACK_ERROR);
}
}
/**
* 获取礼物分类
*
* @return \think\response\Json
*/
public function getLiveGiftCategory ()
{
$res = LogicGiftCategory::where('status', 1)
->field('id,name')
->order('sort desc,id desc')
->select();
if ($res) {
return Show::successData($res);
}
return Show::successData();
}
/**
* 获取分类下的礼物
*
* @return \think\response\Json
*/
public function getLiveGift ()
{
//统一参数校验
$request = $this->validateData(input(), 'live', 'getLiveGift');
//获取所有直播礼物
$gift = LogicAdminGift::with('label')
->where('status', 1)
->where('cid', $request['cid'] ?? 1)
->order('sort desc,id desc')
->select();
foreach ($gift as $v) {
$data[] = [
'id' => $v->id,
'gift_name' => $v->gift_name,
'gift_icon' => $v->gift_icon,
'gift_price' => $v->gift_price,
'gift_label' => $v->label->image ?? '',
];
}
if (!$gift->isEmpty()) {
return Show::successData($data);
}
return Show::successData();
}
}
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交