love_php/app/Http/Controllers/App/TeamController.php
2026-04-02 09:20:51 +08:00

859 lines
34 KiB
PHP

<?php
namespace App\Http\Controllers\App;
use App\Models\TeamLiveChannel;
use App\Models\Video;
use App\Jobs\App\SendAttachMsg;
use App\Models\App\UserTeam;
use App\Models\User;
use App\Models\App\Team;
use App\Models\TeamLive;
use App\Models\InteractLiveGift;
use App\Models\InteractLiveGiftLog;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use App\Http\Controllers\Controller;
use App\Http\Response\ResponseJson;
use App\Services\IMService;
use phpDocumentor\Reflection\Types\True_;
use AlibabaCloud\Client\json;
use App\Contracts\LiveContract;
use App\Models\InteractLive;
use App\Models\Course\Course;
use App\Models\Course\UserCourses;
use function Clue\StreamFilter\fun;
class TeamController extends Controller
{
use ResponseJson;
protected $liveCon;
public function __construct(LiveContract $liveCon)
{
$this->liveCon = $liveCon;
}
//我的群聊
public function myTeams(Request $request){
$user = auth()->user();
$teams = $user->team()->withCount('members');
if($request->keyword){
$teams = $teams->where('tname', 'like', '%'.$request->keyword.'%');
}
$teams = $teams->paginate();
return $this->success('ok', $teams);
}
//群聊详情
public function team(Request $request, $team_id){
$team = Team::withCount('members', 'video')->with('video')->where('id', $team_id)->first();
if(empty($team)){
return $this->failure('群聊不存在');
}
$team->is_manager = UserTeam::where('team_id', $team_id)->where('user_id', auth()->id())->value('type') == 2 ? 1 : 0;
$team->is_owner = $team->owner == auth()->id() ? 1 : 0;
$team->members = $team->membersAsc()->select('users.id', 'name', 'nickname', 'app_avatar', 'circle_avatar', 'photo')->limit(20)->get();
if(!empty($team->members)){
foreach ($team->members as $val){
$val->mute = $val->pivot->mute;
$val->nick = $val->pivot->nick;
$val->is_owner = $team->owner == $val->id ? 1 : 0;
$val->custom = json_decode($val->pivot->custom);
}
}
if(!empty($team->video)){
foreach ($team->video as $video){
$video->duration = gmstrftime('%H:%M:%S',$video->duration);
//补封面
if(empty($video->cover_url) && !empty($video->aliyun_video_id)){
try{
$result = \AliyunService::getPlayInfo($video->aliyun_video_id);
}catch (\Exception $e){
return $this->failure("获取播放地址失败");
}
$video->aliyun_video_id = $request->aliyun_video_id;
if(!empty($result['VideoBase']['CoverURL'])){
Video::where('id', $video->id)->update(['cover_url' => $result['VideoBase']['CoverURL']]);
}
}
}
}
return $this->success('ok', $team);
}
//课程群聊信息
public function teamV2(Request $request, $team_id){
$user_id = auth()->id();
$key = 'course-team'.$user_id.'team_id'.$team_id;
$result = cache::remember($key,5,function()use($user_id,$team_id){
$team = Team::withCount('members', 'video')->where('id', $team_id)->first();
if(empty($team)){
return $this->failure('群聊不存在');
}
$team->is_manager = UserTeam::where('team_id', $team_id)->where('user_id', $user_id)->value('type') == 2 ? 1 : 0;
$team->is_owner = $team->owner == $user_id ? 1 : 0;
//课程数量
$Courses = Course::where('is_show',1)->where('team_id',$team_id)->pluck('id');
$team->course_count = count($Courses);
$team->members = UserCourses::where('status',1)->wherein('course_id', $Courses)->groupby('user_id')->get();
$data = [];
foreach ( $team->members as $key => $value) {
if($value->user){
$user['id']= $value->user->id??0;
$user['nickname'] = $value->user->nickname??'未知';
$user['avatar'] = $value->user->avatar??'';
array_push($data,$user);
}
unset($value->user);
}
$team->members = $data;
$team->user_count =count($data);
$team->course = Course::select('id','title','thumb','charge','user_count')->withCount('videos')->where('is_show',1)->where('team_id',$team_id)->orderBy('user_count','desc')->get();
foreach ($team->course as $key => $value) {
$value['videos_count'] = $value->videos_count;
}
return $team;
});
return $this->success('ok', $result);
}
//修改群聊
public function updateTeam(Request $request, $team_id){
$team = Team::find($team_id);
if(empty($team)){
return $this->failure('群聊不存在');
}
$manger_id = UserTeam::where('team_id', $team_id)->whereIn('type', [1,2])->pluck('user_id')->toArray();
if(!in_array(auth()->id() , $manger_id)){
return $this->failure('没有权限');
}
try {
//同步资料
$im_service= new IMService(env('IM_APP_KEY'), env('IM_APP_SECRET'));
if($request->tname || $request->intro || $request->icon){
$url = 'https://api.netease.im/nimserver/team/update.action';
$data= array(
'tid' => $team_id,
'owner' => $team->owner,
);
if($request->tname && $request->tname != $team->tname){
$data['tname'] = $request->tname;
$team->tname = $request->tname;
}
if($request->intro && $request->intro != $team->intro){
$data['intro'] = $request->intro;
$team->intro = $request->intro;
}
if($request->icon && $request->icon != $team->icon){
$data['icon'] = $request->icon;
$team->icon = $request->icon;
}
}
$result = $im_service->postDataCurl($url,$data);
if($result['code'] == 200){
$team->save();
}
return $this->success('ok');
} catch (\Exception $e) {
$this->getError($e);
return $this->failure('修改群聊失败,请稍后再试');
}
}
//群成员
public function teamMembers(Request $request, $team_id){
$team = Team::find($team_id);
if(empty($team)){
return $this->failure('群聊不存在');
}
$columns = ['users.id', 'name', 'nickname', 'app_avatar', 'circle_avatar', 'photo'];
$members = $team->membersAsc();
if($request->keyword){
$keyword = $request->keyword;
$members= $members->where(function ($query) use ($keyword){
$query->where('nickname', 'like', '%'.$keyword.'%')->orWhere('mobile', 'like', '%'.$keyword.'%');
});
}
$members = $members->paginate(50, $columns);
foreach ($members as $val){
$val->nick = $val->pivot->nick;
$val->mute = $val->pivot->mute;
$val->is_owner = $team->owner == $val->id ? 1 : 0;
$val->custom = json_decode($val->pivot->custom);
}
return $this->success('ok', $members);
}
//添加成员-搜索用户
public function searchUser(Request $request, $team_id){
$members = UserTeam::where('team_id', $team_id)->pluck('user_id')->toarray();
$users = User::select('id', 'name', 'nickname', 'circle_avatar', 'app_avatar', 'photo', 'rank_id')->whereNotIn('id', $members);
if($request->keyword){
$keyword = $request->keyword;
$users = $users->where(function ($query) use ($keyword){
$query->where('nickname', 'like', '%'.$keyword.'%')->orWhere('mobile', 'like', '%'.$keyword.'%');
});
}
$users = $users->paginate();
foreach ($users as $user){
$user->isSuperRank = $user->rank_id ? 1 : 0;
}
return $this->success('ok', $users);
}
//添加成员
public function addMember(Request $request, $team_id){
$team = Team::find($team_id);
if(empty($team)) {
return $this->failure('群聊不存在');
}
$manger_id = UserTeam::where('team_id', $team_id)->whereIn('type', [1,2])->pluck('user_id')->toArray();
if(!in_array(auth()->id() , $manger_id)){
return $this->failure('没有权限');
}
//增删群成员
$user_ids = UserTeam::where('team_id', $team_id)->pluck('user_id')->toArray();
if($request->has('members') && !empty($request->members) && array_diff($request->members, $user_ids)) {
//网易端拉人
$im_service = new IMService(env('IM_APP_KEY'), env('IM_APP_SECRET'));
$result = $im_service->addIntoGroup($team->tid, auth()->id(), $request->members);
if ($result['code'] == 200) {
// SendAttachMsg::dispatch($request->members, 'team', $team->id, '你已被邀请加入'.$team->tname.'群聊')->onQueue('love');
$this->send($request->members, 'team', $team->id, '你已被邀请加入'.$team->tname.'群聊');
$insert = [];
$members = array_unique($request->members);
foreach ($members as $val) {
if (!in_array($val, $user_ids)) {
$arr = [
'user_id' => $val,
'team_id' => (string)$team->id,
'created_at' => date('Y-m-d H:i:s'),
'updated_at' => date('Y-m-d H:i:s'),
];
$insert[] = $arr;
}
}
UserTeam::insert($insert);
}
}
return $this->success('ok');
}
//删除成员
public function delMember(Request $request, $team_id){
if(empty($request->members)){
return $this->failure('请输入用户id');
}
$team = Team::find($team_id);
if(empty($team)){
return $this->failure('群聊不存在');
}
$manger_id = UserTeam::where('team_id', $team_id)->whereIn('type', [1,2])->pluck('user_id')->toArray();
if(!in_array(auth()->id() , $manger_id)){
return $this->failure('没有权限');
}
if($team->owner == $request->user_id){
return $this->failure('不能踢群主');
}
$im_service= new IMService(env('IM_APP_KEY'), env('IM_APP_SECRET'));
$result = $im_service->kickFromGroup($team->tid, auth()->id(), '', $request->members);
if ($result['code'] == 200) {
UserTeam::where('team_id', $team_id)->whereIn('user_id', $request->members)->delete();
}
return $this->success('ok');
}
//发送群聊消息
public function sendChatMessageToTeam(Request $request, User $user)
{
$data['status'] = 1;
return $this->success('ok',$data);
}
public function teamMsg(Request $request){
$user = auth()->user();
$begintime = $request->input('start_time', time()*1000 - 24 * 3600000);
$endtime = $request->input('start_time', time()*1000);
$limit = 100;
$im_service = new IMService(env('IM_APP_KEY'), env('IM_APP_SECRET'));
$user->createIMUser();
$messages = $im_service->queryGroupMsg('3813569048',10051306,$begintime,$endtime, $limit,$reverse='1');
$teams = $im_service->queryGroup([3813743980]);
return $this->success('ok', $teams);
}
//退群/解散
public function dropTeam(Request $request, $team_id){
$team = Team::find($team_id);
if(empty($team)) return $this->failure('群聊不存在');
$user_id = auth()->id();
$im_service = new IMService(env('IM_APP_KEY'), env('IM_APP_SECRET'));
if($team->owner != $user_id){
//退群
$in_team = UserTeam::where('user_id', $user_id)->where('team_id', $team_id)->count();
if(!$in_team){
return $this->failure('不在该群聊');
}
$result = $im_service->leaveTeam($team_id, $user_id);
if($result['code'] == 200){
UserTeam::where('user_id', $user_id)->where('team_id', $team_id)->delete();
}
}else{
//解散群聊
$result = $im_service->removeGroup($team_id, $team->owner);
if($result['code'] == 200){
UserTeam::where('team_id', $team_id)->delete();
Team::where('id', $team_id)->delete();
}
}
return $this->success('ok');
}
public function send($user_id, $type, $type_id, $content)
{
switch ($type){
case 'team':
//\Log::info('SendAttachMsg.team');
$team = Team::with('owner:id,nickname')->where('id', $type_id)->first();
break;
default:
//\Log::info('SendAttachMsg.default');
break;
}
foreach ($user_id as $user_id){
$result = $this->sendAttachMsg($type_id, 'team', $content, $team->owner, $user_id, $team->icon);
}
return true;
}
//发送系统通知
public function sendAttachMsg($type_id, $type, $content, $from, $to, $image)
{
$body = "点击查看>>";
//发送评论自定义系统消息
$im_service = new IMService();
$attach = ["myattach"=>$content, $type.'_id'=>$type_id];
$payload = [
"apsField"=>[
"alert"=>[
"title"=>$content,
"body"=> $body,
],
"mutable-content"=>1
],
"hwField"=>[
"click_action"=>[
"type"=>1,
"intent"=>"intent://com.huawei.codelabpush/deeplink?id=".$type_id."&customType=".$type."#Intent;scheme=pushscheme;launchFlags=0x4000000;end",
],
"title"=>$content,
"body"=>$body,
],
"oppoField"=>[
"click_action_type"=>1,
"click_action_activity"=>"com.oppo.codelabpush.intent.action.test",
"action_parameters"=>[
"id"=>$type_id,
"customType"=>"moment",
],
],
"apnsText"=>$body,
"pushTitle"=>$content,
"customType"=>$type,
"content"=>$body,
"media_image"=>$image,
"media_type"=>'image',
"id"=>$type_id,
"type"=>$type,
'passThrough'=>1,
];
$result = $im_service->sendAttachMsg($from,0,$to,json_encode($attach),$content,$payload);
return true;
}
//修改群成员昵称
public function updateTeamNick(Request $request, $team_id){
$user_id = auth()->id();
$user_team = UserTeam::where('team_id', $team_id)->where('user_id', $user_id)->first();
if(!$user_team){
return $this->failure('该用户不在群聊');
}
$team = Team::find($team_id);
if(empty($team)){
return $this->failure('群聊不存在');
}
//扩展字段
$custom = [];
if($request->class){
$custom['class'] = $request->class;
}
$im_service= new IMService(env('IM_APP_KEY'), env('IM_APP_SECRET'));
$url = 'https://api.netease.im/nimserver/team/updateTeamNick.action';
$data['tid'] = $team_id;
$data['owner'] = $team->owner;
$data['accid'] = $user_id;
if($request->nick && $user_team->nick != $request->nick){
$user_team->nick = $request->nick;
$data['nick'] = $request->nick;
}
if($custom && $user_team->custom != json_encode($custom)){
$user_team->custom = json_encode($custom);
$data['custom'] = json_encode($custom);
}
$result = $im_service->postDataCurl($url,$data);
if($result['code'] != 200){
return $this->failure('更新群成员信息失败');
}
$user_team->save();
return $this->success('ok');
}
public function checkStoreTeamLive($team_id)
{
try {
$is_anchor = auth()->user()->isAnchor($team_id);
if (empty($is_anchor)) return ['code'=>1, 'msg'=>'暂无主播权限'];
return true;
} catch (\Exception $e) {
$this->getError($e);
return false;
}
}
/**
* 开启群直播
*/
public function storeTeamLive(Request $request, $tid_id)
{
try {
$name = Request()->route()->getName();
$version = 1;
if($name == 'teams/lives/v2'){
$version = 2;
}
$team = Team::where('tid', $tid_id)->first();
if (empty($team)) return $this->failure("聊天群不存在");
//是否已经有直播在线
$live = TeamLive::where('team_id', $tid_id)->where('in_live', 1)->where('user_id', '<>',auth()->id())->where('version', $version)->first();
if ($live) return $this->failure("该群已有其他主播正在开播");
$team_id = $team->id;
//检查权限
$result = $this->checkStoreTeamLive($team_id);
if (empty($result))throw new \Exception("检查权限失败", 1);
if (is_array($result) && $result['code']) return $this->failure($result['msg']);
//开启直播间
$live = $this->liveCon->getOnlyTeamLive($team_id, $version);
//获取NERTC Token
$im_service= new IMService(env('IM_APP_KEY'), env('IM_APP_SECRET'));
$request->merge(['channel_name'=>$live->name]);
$curl_result = $im_service->getToken($request);
$token = '';
if($curl_result['code'] == 200 && !empty($curl_result['token'])){
$token = $curl_result['token'];
}
$live->in_live = 1;
if (empty($live)) throw new \Exception("开启群直播失败", 1);
//开启聊天室
if (empty($live->chat_room_id)) {
$chat_room_id = $this->liveCon->getChatRoom($live);
if (empty($chat_room_id)) throw new \Exception("聊天室失败", 1);
$live->chat_room_id = $chat_room_id;
}
$live->save();
//开启直播流
$channel = $this->liveCon->getChannel($live);
if (empty($channel)) throw new \Exception("直播流失败", 1);
//开启互动直播
$result = $this->liveCon->getChannelRoom($live);
if (empty($result)) throw new \Exception("互动直播失败", 1);
//开启直播记录
$live->liveLogs()->create(['user_id'=>auth()->id(), 'start_time'=>date('Y-m-d H:i:s')]);
//主播信息
$anchor = $live->anchor;
$profile = $anchor->profileCourtship()->where('user_id', $anchor->id)->select('id', 'user_id', 'birthday', 'stature', 'city')->first();
if ($profile) {
$anchor->age = \CommonUtilsService::getAge($profile->birthday);
}
$live->anchor = $anchor;
//点击量
$live->click_num = 0;
$nick = UserTeam::where('user_id', $live->user_id)->where('team_id', $team->tid)->value('nick');
if(empty($nick)){
$nick = User::where('id', $live->user_id)->value('nickname');
}
//增加群扩展
if ($team->custom) {
$old_custom = $team->custom;
$old_custom->in_live = 1;
$old_custom->anchor_id = $live->user_id;
$old_custom->anchor_name = $nick;
}else{
$old_custom['in_live'] = 1;
$old_custom['anchor_id'] = $live->user_id;
$old_custom['anchor_name'] = $nick;
}
$data['custom'] = json_encode($old_custom);
$data['tid'] = $tid_id;
$data['owner'] = $team->owner;
$im_service = new IMService(env('IM_APP_KEY'), env("IM_APP_SECRET"));
$url = "https://api.netease.im/nimserver/team/update.action";
$result = $im_service->postDataCurl($url,$data);
if ($result['code'] != 200) throw new \Exception("群扩展修改失败", 1);
$team->custom = $data['custom'];
$team->save();
$live->channel = $channel;
$live->channelv = $channel;
$live->token = $token;
//返回结果
return $this->success('ok', $live);
} catch (\Exception $e) {
$this->getError($e);
return $this->failure("开启直播失败,请稍后再试");
}
}
/**
* 关闭直播
*/
public function closeTeamLive(Request $request, $live_id)
{
try {
$live = TeamLive::find($live_id);
if (empty($live)) return $this->failure("直播间不存在");
//检查权限
$result = $this->checkStoreTeamLive($live->team_id);
if (empty($result))throw new \Exception("检查权限失败", 1);
if (is_array($result) && $result['code']) return $this->failure($result['msg']);
//下播
$live->in_live = 0;
$live->save();
$team = Team::find($live->team_id);
if (empty($team)) throw new \Exception("群聊直播错误", 1);
//主播
$anchor = $live->anchor;
$anchor->isSuperRank = $anchor->isSuperRank();
//生成直播记录
$log = $live->liveLogs()->where('user_id', auth()->id())->whereNull('end_time')->orderBy('id', 'desc')->first();
$live_duration = 0;
if ($log) {
$log->end_time = date('Y-m-d H:i:s');
$click_num_key = $live->clickNumKey();
$log->click_num = Cache::get($click_num_key);
$log->save();
//删除直播间缓存点击量
Cache::forget($click_num_key);
$live_duration = strtotime($log->end_time) - strtotime($log->start_time); //秒
//观看人数
$live->click_num = $log->click_num;
}
$live->live_duration = $live_duration;
$live->fans_count = $anchor->followers()->where('hidden_profile', '<>', 'ALLSEX')->count();
//修改群扩展信息
if ($team->custom) {
$old_custom = $team->custom;
$old_custom->in_live = 0;
$old_custom->anchor_id = $live->user_id;
}else{
$old_custom['in_live'] = 0;
$old_custom['anchor_id'] = $live->user_id;
}
$data['custom'] = json_encode($old_custom);
$data['tid'] = $live->team_id;
$data['owner'] = $team->owner;
$im_service = new IMService(env('IM_APP_KEY'), env("IM_APP_SECRET"));
$url = "https://api.netease.im/nimserver/team/update.action";
$result = $im_service->postDataCurl($url,$data);
$team->custom = $data['custom'];
$team->save();
//返回结果
return $this->success("ok", $live);
} catch (\Exception $e) {
$this->getError($e);
return $this->failure("关闭直播失败,请稍后再试");
}
}
public function lastLiveInfo(Request $request, $live_id)
{
try {
$live = TeamLive::find($live_id);
if (empty($live)) return $this->failure("直播间不存在");
$log = $live->liveLogs()->where('user_id', auth()->id())->orderBy('id', 'desc')->first();
if (empty($log)) return $this->failure("暂无直播信息");
$live->live_duration = strtotime($log->end_time) - strtotime($log->start_time); //秒
$anchor = $live->anchor;
$live->click_num = $log->click_num;
$live->fans_count = $anchor->followers()->where('hidden_profile', '<>', 'ALLSEX')->count();
$live->is_followed = auth()->user()->isFollowing($anchor)?1:0;
return $this->success('ok', $live);
} catch (\Exception $e) {
$this->getError($e);
return $this->failure("直播间已关闭");
}
}
/**
* 直播赠送礼物
*/
public function sendUserGift(Request $request, $gift_id, $user_id)
{
try {
$gift = InteractLiveGift::find($gift_id);
if (empty($gift)) return $this->failure("该礼物暂不能赠送");
//检查福币
$result = $this->checkSendGift($gift);
if (empty($result)) throw new \Exception("检查福币信息失败", 1);
if (is_array($result) && $result['code']) return $this->failure($result['msg']);
//赠送福币
$send_user = auth()->user();//赠送人
$other_user = User::find($user_id);//收获人
$result = $this->liveCon->sendGift($gift,$send_user, $other_user, $channel='team_live', $request->live_id);
if (empty($result)) throw new \Exception("送礼物失败", 1);
if ($request->live_id) {
//发送聊天室消息
$this->liveCon->chatroomSendMsg($user_id, $gift_id);
}
//返回结果
return $this->success('ok');
} catch (\Exception $e) {
$this->getError($e);
return $this->failure("赠送礼物失败,请稍后再试");
}
}
public function checkSendGift($gift)
{
try {
$mine_coin = auth()->user()->coinInfo();
//直播间id
if (empty(request()->input('live_id'))) return ['code'=>1, '直播间不存在'];
//检查是否够福币
if ($mine_coin->remain_amount + $mine_coin->amount_from_other < $gift->coin) return ['code'=>1, 'msg'=>'赠送失败,福币不足'];
return true;
} catch (\Exception $e) {
$this->getError($e);
return false;
}
}
/**
* 直播详情
* @param Request $request [description]
* @param int $live_id 直播详情
* @return [type] [description]
*/
public function live(Request $request, $tid)
{
try {
$name = Request()->route()->getName();
$version = 1;
//判断直播2.0版本
// $is_live2 = $this->isCurrentVersion($request, '1.3.17', '1.3.23');
if($name == 'teams/live/v2'){
$version = 2;
}
//是否被禁止
// $result = auth()->user()->isBanBylives($live_id);
// if (is_array($result)) return $this->failure('已被禁止进入该直播间至'.$result['msg']);
//直播间信息
$team = Team::where('tid', $tid)->first();
if (empty($team)) return $this->failure('群不存在');
$can_join = UserTeam::where('user_id', auth()->id())->where('team_id', $tid)->value('join_live');
if(empty($can_join)) return $this->failure("您被禁止加入本群直播");
$live = TeamLive::where('team_id', $team->id)->where('in_live', 1)/*->where('version', $version)*/->first();
if (empty($live)) return $this->failure('主播还未开播');
if ($version == 1 && $live->version == 1) {
$tasks = $live->getRtmpTask();
if (empty($tasks)) return $this->failure("暂无直播信息");
$live->tasks = $tasks;
}
//主播信息
$anchor = $live->anchor;
$rd_user_key = User::cacheUserKey($anchor->id);
if (Cache::has($rd_user_key)) {
$rd_user = Cache::get($rd_user_key);
}else{
$anchor->cacheUser();
$rd_user = Cache::get($rd_user_key);
}
if ($anchor) {
//是否关注
$anchor->is_followed = auth()->user()->isFollowing($anchor)?1:0;
//粉丝数
$anchor->fans_count = $rd_user->fans_count;
//是否是会员
$anchor->isSuperRank = $rd_user->is_super_rank;
$profile = $rd_user->profileCourtship;
$anchor->age = \CommonUtilsService::getAge($profile->birthday);
$anchor->stature = $profile->stature;
$anchor->city = $profile->city;
}
//返回点击量 缓存
$click_num_key = $live->clickNumKey();
$live->click_num = Cache::get($click_num_key,0);
//直播频道
$live->channel;
$live->channelv = $live->channel;
$token = '';
// if ($version == 2) {
//获取NERTC Token
$im_service= new IMService(env('IM_APP_KEY'), env('IM_APP_SECRET'));
$request->merge(['channel_name'=>$live->name]);
$result = $im_service->getToken($request);
if($result['code'] == 200 && !empty($result['token'])){
$token = $result['token'];
}
// }
return $this->success('ok', compact('live', 'anchor', 'token'));
} catch (\Exception $e) {
$this->getError($e);
return $this->failure('刷新直播失败,请稍后再试');
}
}
/**
* 礼物列表
*/
public function liveGifts(Request $request, $live_id)
{
try {
$gifts = InteractLiveGiftLog::with('gift')->where('user_id', auth()->id())->whereHas('channelLog', function($sql) use($live_id){
$sql->where("channel", 'team_live')->where('channel_id', $live_id);
})->groupBy('gift_id')->select(\DB::raw('sum(num) as num, gift_id'))->get();
return $this->success('ok', $gifts);
} catch (\Exception $e) {
$this->getError($e);
return $this->failure("获取礼物列表失败,请稍后再试");
}
}
public function joinTeamLive(Request $request, $tid)
{
try {
$name = Request()->route()->getName();
$version = 1;
if($name == 'teams/join/live/v2'){
$version = 2;
}
//是否被禁止
// $result = auth()->user()->isBanBylives($live_id);
// if (is_array($result)) return $this->failure('已被禁止进入该直播间至'.$result['msg']);
//直播间信息
$team = Team::where('tid', $tid)->first();
if (empty($team)) return $this->failure('群不存在');
// $can_join = UserTeam::where('user_id', auth()->id())->where('team_id', $tid)->value('join_live');
// if(empty($can_join)) return $this->failure("您被禁止加入本群直播");
$live = TeamLive::where('team_id', $team->id)->where('in_live', 1)/*->where('version', $version)*/->first();
if (empty($live)) return $this->failure('直播暂未开播');
if ($version == 1 && $live->version == 1) {
$tasks = $live->getRtmpTask();
if (empty($tasks)) return $this->failure("暂无直播信息");
$live->tasks = $tasks;
}
//主播信息
$anchor = $live->anchor;
$profile = $anchor->profileCourtship()->where('user_id', $anchor->id)->select('id', 'user_id', 'birthday', 'stature', 'city')->first();
if ($profile) {
$anchor->age = \CommonUtilsService::getAge($profile->birthday);
}
if ($anchor) {
//是否关注
$anchor->is_followed = auth()->user()->isFollowing($anchor)?1:0;
//粉丝数
$anchor->fans_count = $anchor->followers()->where('hidden_profile', '<>', 'ALLSEX')->count();
}
//增加点击量 缓存
if ($live->user_id != auth()->id()) {
$click_num_key = $live->clickNumKey();
Cache::increment($click_num_key);
$live->click_num = Cache::get($click_num_key);
}
//获取NERTC Token
$im_service= new IMService(env('IM_APP_KEY'), env('IM_APP_SECRET'));
$request->merge(['channel_name'=>$live->name]);
$result = $im_service->getToken($request);
$token = '';
if($result['code'] == 200 && !empty($result['token'])){
$token = $result['token'];
}
$live->channel;
return $this->success('ok', compact('live', 'anchor', 'token'));
} catch (\Exception $e) {
$this->getError($e);
return $this->failure("进入直播失败,请稍后再试");
}
}
/**
* 加入直播间-记录房间热度
*/
public function joinTeamLive_new(Request $request, $tid)
{
$live = InteractLive::where('id', $tid)->first();
if(!$live)
return $this->failure('未找到该房间,记录失败');
if ($live->user_id != auth()->id()) {
$live->increment('click_num');
$live->increment('total_click_num');
}
$data['click_num'] = $live->click_num;
return $this->success('ok',$data);
}
/**
* 直播用户信息
*/
public function liveUserInfo(Request $request, $user_id)
{
try {
$user = User::where('id', $user_id)->with('profileCourtship:user_id,birthday,stature,city,introduction')->select('id','app_avatar','sex','is_approved','nickname')->first();
$user = $this->liveCon->liveUserInfo($user);
if (empty($user)) throw new \Exception("获取观众信息失败", 1);
return $this->success('ok',$user);
} catch (\Exception $e) {
$this->getError($e);
return $this->failure('获取观众信息失败,请稍后再试');
}
}
public function getToken(Request $request){
//获取NERTC Token
$im_service= new IMService(env('IM_APP_KEY'), env('IM_APP_SECRET'));
$request->merge(['channel_name'=>'asdasd']);
$result = $im_service->getToken($request);
$token = '';
if($result['code'] == 200 && !empty($result['token'])){
$token = $result['token'];
}
print_r($token);
}
}