merchant_id ?? 0; $merchant_user_id = $request->merchant_user_id; $live = MerchantLive::where('id', $id)->first(); if (!$live) return $this->failure('直播不存在'); $accid = 10000000 + $merchant_user_id; $merchant_user = MerchantUser::select('nickname', 'pic', 'mobile')->where('id', $merchant_user_id)->first(); // 查看网易云账户 $IMUser = WangYiYunUser::where('accid', $accid)->first(); if (!$IMUser) { $data['name'] = $merchant_user->name; $data['pic'] = $merchant_user->pic ?? User::DefaultAvatar; $data['mobile'] = $merchant_user->mobile; $data['anchor_id'] = 0; $data['accid'] = $accid; $IMUser = MerchantAccount::createIMUser($data); } $live->accid = $IMUser->accid; $live->token = $IMUser->token; $live->pv = Redis::zincrby('merchantLive', 1, $id); $live->channel; $merchant = MerchantAccount::select('id', 'mobile')->where('id', $merchant_id)->first(); if ($merchant) { $merchant->pic = $merchant->anchorV2->pic ?? User::DefaultAvatar; $merchant->name = $merchant->anchorV2->name ?? '匿名用户'; unset($merchant->anchorV2); } else { $merchant['id'] = $merchant_id; $merchant['pic'] = User::DefaultAvatar; $merchant['name'] = '匿名用户'; } $live->merchant = $merchant; // $im_service= new IMService(env('IM_APP_KEY'), env('IM_APP_SECRET')); // $data = ['channel_name'=>$live->title,'uid'=>10000000+$merchant_user_id]; // $curl_result = $im_service->getTokenV2($data); // $live->token = ''; // if($curl_result['code'] == 200 && !empty($curl_result['token'])){ // $live->token = $curl_result['token']; // } return $this->success('ok', $live); }catch (\Exception $e){ $this->getError($e); return $this->failure('服务器休息中,请稍后再试'); } } /** * 直播列表 * @param Request $request * @return JsonResponse|string */ public function liveList(Request $request) { try { $merchant_id = $request->merchant_id; $live = MerchantLive::where('merchant_id', $merchant_id) ->where('is_show', 1) ->orderBy('is_top', 'desc') ->orderBy('top_time', 'desc') ->orderBy('id', 'desc') ->paginate(); return $this->success('ok', $live); }catch (\Exception $e){ $this->getError($e); return $this->failure('服务器休息中,请稍后再试'); } } /** * 发送聊天信息 * @param Request $request * @return JsonResponse|string */ public function chat(Request $request) { try { # code... // 查看用户是否禁言 $ban_log = MerchantLiveBanLog::where('live_id', $request->live_id) ->where('m_user_id', $request->merchant_user_id) ->where('start_time', '<', now()) ->where('end_time', '>', now()) ->exists(); if ($ban_log) return $this->failure('您已被系统禁言'); $merchant_user_id = $request->merchant_user_id; $record = new Record(); $record->live_id = $request->live_id; $record->viewer_id = $merchant_user_id; $record->content = $request->input('content'); $record->pic = $request->pic; $record->ip = $request->getclientIp(); $record->related_msg = $request->related_msg; $record->class = 'merchant_user'; $record->save(); return $this->success('ok'); }catch (\Exception $e){ $this->getError($e); return $this->failure('服务器休息中,请稍后再试'); } } /**获取聊天室的地址 * @param Request $request * @return JsonResponse|string */ public function getChatroomAddress(Request $request) { try { $merchant_id = $request->merchant_id; $accid = 1000000 + $merchant_id; $im = new IMService(env('IM_APP_KEY'), env('IM_APP_SECRET')); $url = $im->getChatroomAddress($request->roomid, $accid, $request->clienttype); return $this->success('ok', ['chatroomAddress' => $url]); }catch (\Exception $e){ $this->getError($e); return $this->failure('服务器休息中,请稍后再试'); } } /**更新网易云token * @param Request $request * @return JsonResponse|string */ public function updateToken(Request $request) { try { $accid = $request->accid; $im = new IMService(env('IM_APP_KEY'), env('IM_APP_SECRET')); $result = $im->updateUserToken($accid); if ($result['code'] == 200) { $token = $result['info']['token']; WangYiYunUser::where('accid', $accid)->update(['token' => $token]); return $this->success('ok', $result); } else { return $this->failure('更新失败', $result); } }catch (\Exception $e){ $this->getError($e); return $this->failure('服务器休息中,请稍后再试'); } } //提交直播反馈 public function submitFeedbacks(Request $request, $live_id) { try { $live = MerchantLive::find($live_id); if (!$live) return $this->failure('直播不存在或已被移除'); $type = $request->type ?? 'live'; $content = $request->input('content'); if (empty($content)) return $this->failure('请先填写反馈内容'); $history = new MerchantFeedbackHistory(); $history->type = $type; $history->type_id = $live_id; $history->m_user_id = $request->merchant_user_id; $history->content = $request->input('content'); $history->save(); return $this->success('ok'); } catch (\Exception $e) { $this->getError($e); return $this->failure('服务器开小差,请稍后再试'); } } }