userCon = $userCon; } /** * 群聊详情 * @param Request $request * @return \Illuminate\Http\JsonResponse|string */ public function groupChatDetail(Request $request) { try { $user = auth()->user(); $team_id = $request->input('team_id'); if (!$team_id) return $this->failure('群聊不存在'); // $data = Team::with(['members' => function ($query) { // $query->selectRaw('ufutx_users.nickname,ufutx_users.app_avatar,ufutx_users.type'); // }])->where('id', $team_id)->first(); $data = Team::with('members')->where('id', $team_id)->first(); if (!$data){ return $this->success('ok'); } $data = $data->toArray(); $user_team = UserTeam::where('user_id', $user->id)->where('team_id', $team_id) ->select('id', 'user_id', 'team_id', 'nick', 'show_other_nick', 'is_top') ->first(); if ($user_team && strlen($user_team->nick) <= 0) { $user_team->nick = User::where('id', $user->id)->value('nickname'); } $data['user_team'] = $user_team; $data['is_join'] = $user_team ? 1 : 0; return $this->success('ok', $data); } catch (\Exception $e) { // $this->getError($e); return $this->failure($e->getMessage()); } } /** * 退出群聊 * @param Request $request * @return \Illuminate\Http\JsonResponse|string */ public function groupChatOut(Request $request) { try { $team_id = $request->input('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) { //解散群聊 $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'); } //退群 $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(); } return $this->success('ok'); } catch (\Exception $e) { $this->getError($e); return $this->failure($e->getMessage()); } } /** * 群聊发消息 * @param Request $request * @return \Illuminate\Http\JsonResponse|string */ public function sendMessage(Request $request) { try { $user = auth()->user(); $type = $request->input('type'); $team_id = $request->input('team_id'); $content = $request->input('content'); if (!strlen($content)){ return $this->failure('文本不能为空'); } if ($user->id != 69938) { $check = BadWord::whereRaw("instr('$content',content) > 0")->first(); if ($check) { return $this->failure('检测到有敏感词,发送失败'); } } $content = $this->filterContent($content); // 每天第一条消息 模板消息通知群成员 // 是否第一条消息 $status = $this->temDailyFirstMessage($team_id); if ($status) { // 修改 Team::where('tid', $team_id)->update(['notice_time'=>time()]); // if ($team_id == 3838854261) { // 发送模板消息 SendTeamMessageTemplate::dispatch($team_id, $user)->onQueue("start_message"); // } } return $this->success('ok',compact('content')); } catch (\Exception $e) { // $this->getError($e); return $this->failure($e->getMessage()); } } public function temDailyFirstMessage($team_id) { $team = Team::where('tid', $team_id)->first(); if (empty($team)) return 0; if (empty($team->notice_time)) return 1; $now_day = date("Y-m-d"); $notice_day = date("Y-m-d", $team->notice_time); if ($notice_day == $now_day) return 0; return 1; } /** * 群聊消息已读 * @param Request $request * @return \Illuminate\Http\JsonResponse|string */ public function groupChatRead(Request $request) { try { $user = auth()->user(); $user_id = $user->id; $team_id = $request->input('team_id'); if (!$team_id) return $this->failure('群聊id不能为空'); $check = UserTeam::where('team_id', $team_id)->where('user_id', $user_id)->first(); if (!$check) { return $this->failure('不在该群聊'); } $cache_key = ApiRedisKey::getGroupChatUnreadKey($team_id); Redis::sRem($cache_key, $user_id); return $this->success('ok'); } catch (\Exception $e) { $this->getError($e); return $this->failure($e->getMessage()); } } /** * 修改我在群聊的昵称 * @param Request $request * @return \Illuminate\Http\JsonResponse|string */ public function groupChatUpdateNick(Request $request) { try { $user = auth()->user(); $user_id = $user->id; $team_id = $request->input('team_id'); $nick = $request->input('nick'); if (strlen($nick) <= 0) { return $this->failure('请输入要修改我在群聊的昵称'); } $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 (!$team) { return $this->failure('群聊不存在'); } if ($nick == $user_team->nick) { return $this->success('ok'); } $im_service = new IMService(env('IM_APP_KEY'), env('IM_APP_SECRET')); $result = $im_service->updateGroupNick($team_id, $team->owner, $user_id, $nick); if ($result['code'] == 200) { $user_team->nick = $nick; $user_team->save(); } return $this->success('ok'); } catch (\Exception $e) { $this->getError($e); return $this->failure($e->getMessage()); } } /** * 切换是否显示其他群成员昵称 * @param Request $request * @return \Illuminate\Http\JsonResponse|string */ public function groupChatUpdateShowOtherNick(Request $request) { try { $user = auth()->user(); $user_id = $user->id; $team_id = $request->input('team_id'); $show_other_nick = $request->input('show_other_nick'); $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 (!$team) { return $this->failure('群聊不存在'); } if ($show_other_nick == $user_team->show_other_nick) { return $this->success('ok'); } $user_team->show_other_nick = $show_other_nick; $user_team->save(); return $this->success('ok'); } catch (\Exception $e) { $this->getError($e); return $this->failure($e->getMessage()); } } /** * 置顶我的群聊 * @param Request $request * @return \Illuminate\Http\JsonResponse|string */ public function groupChatUpdateIsTop(Request $request) { try { $user = auth()->user(); $user_id = $user->id; $team_id = $request->input('team_id'); $is_top = $request->input('is_top'); $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 (!$team) { return $this->failure('群聊不存在'); } if ($is_top == $user_team->is_top) { return $this->success('ok'); } $user_team->is_top = $is_top; $user_team->save(); return $this->success('ok'); } catch (\Exception $e) { $this->getError($e); return $this->failure($e->getMessage()); } } /** * 加入群聊 * @return \Illuminate\Http\JsonResponse|string */ public function groupChatJoin(Request $request) { try { $user = auth()->user(); $team_id = $request->input('team_id'); if (!$team_id) { return $this->failure('参数错误'); } $team = Team::find($team_id); if (!$team) { return $this->failure('群聊不存在'); } $user_team = UserTeam::where('user_id', $user->id)->where('team_id', $team_id)->first(); if ($user_team) { return $this->failure('你已在该群聊'); } $im_service = new IMService(env('IM_APP_KEY'), env('IM_APP_SECRET')); $res = $im_service->addIntoGroup($team_id, $team->owner, [$user->id]); if (isset($res['code']) && $res['code'] == 200) { $user_team = new UserTeam(); $user_team->user_id = $user->id; $user_team->team_id = $team_id; $user_team->save(); } return $this->success('ok'); } catch (\Exception $e) { return $this->failure($e->getMessage()); } } }