service = $service; $this->groupMemberService = $groupMemberService; $this->groupService = $groupService; } /** * 我的群列表 * @para * @return mixed * @throws DataNotFoundException * @throws DbException * @throws ModelNotFoundException */ public function group() { $groups = $this->groupMemberService->getColumn(['uid' => $this->request->uid()], 'id'); $list = $this->groupService->search()->where('id', 'in', $groups) ->order('last_message_time', 'desc') ->select(); return $this->success('ok', compact('list')); } /** * 群详情 * @param $id * @para * @return mixed * @throws DataNotFoundException * @throws DbException * @throws ModelNotFoundException */ public function groupInfo($id) { $group = $this->groupService->get($id); if (!$group) return $this->error('群不存在'); return $this->success('ok', compact('group')); } /** * 群成员列表 * @param $id * @para * @return mixed * @throws DataNotFoundException * @throws DbException * @throws ModelNotFoundException */ public function groupMembers($id) { $group = $this->groupService->get($id); if (!$group) return $this->error('群不存在'); $list = $this->groupMemberService->search()->where('chat_group_id', $id)->select(); foreach ($list as &$v) { $v['ext_info'] = $this->service->getInfo($v['identity'], $v['uid']); } return $this->success('ok', compact('list')); } /** * 添加群成员 * @param $id * @return mixed * @throws DataNotFoundException * @throws DbException * @throws ModelNotFoundException * @para */ public function groupAddMember($id) { $group = $this->groupService->get($id); if (!$group) return $this->error('群不存在'); if (!$this->groupMemberService->search()->where('uid', $this->request->uid())->where('chat_group_id', $id)->where('is_hoster', 1)->find()) { return $this->error('你没有权限'); } $uid = $this->request->post('uid', 0); $identity = $this->request->post('identity', 0); if ($this->groupMemberService->search()->where('uid', $uid)->where('chat_group_id', $id)->find()) { return $this->error('用户已在群聊中'); } /** @var UserServices $userService */ $userService = app()->make(UserServices::class); $user = $userService->getUserInfo($uid); if (!$user) return $this->error('用户不存在'); $ext_info = $this->service->getInfo($identity, $uid); if (!$ext_info) return $this->error('用户不存在'); try { $this->groupMemberService->create([ 'uid' => $uid, 'chat_group_id' => $id, 'identity' => $identity, ]); $this->service->create([ 'uid' => $uid, 'to_uid' => 0, 'add_time' => time(), 'status' => 1, 'msn_type' => 'notice', 'group_id' => $id, 'msn' => $ext_info['info']['name'] . ' 加入群聊', 'group_see' => $uid, ]); $this->groupService->update($id, ['last_message_time' => time()]); return $this->success('ok', ['date' => ['name' => $ext_info['info']['name'], 'type' => $ext_info['type'], 'uid' => $uid, 'add_time' => time(), 'group_id' => $id], 'type' => 'group:new_member']); } catch (Exception $e) { return $this->error($e->getMessage()); } } /** * 移除群成员 * @param $id * @return mixed * @throws DataNotFoundException * @throws DbException * @throws ModelNotFoundException * @para */ public function groupRemoveMember($id) { $group = $this->groupService->get($id); if (!$group) return $this->error('群不存在'); if (!$this->groupMemberService->search()->where('uid', $this->request->uid())->where('chat_group_id', $id)->where('is_hoster', 1)->find()) { return $this->error('你没有权限'); } $uid = $this->request->post('uid', 0); if (!$user = $this->groupMemberService->search()->where('uid', $uid)->where('chat_group_id', $id)->find()) { return $this->error('用户不在群聊中'); } if ($user['is_hoster']) return $this->error('群主不能移除'); try { $this->groupMemberService->delete($user['id']); return $this->success('ok'); } catch (Exception $e) { return $this->error($e->getMessage()); } } /** * 修改群昵称 * @param $id * @return mixed * @throws DataNotFoundException * @throws DbException * @throws ModelNotFoundException */ public function setMark($id) { $group = $this->groupService->get($id); if (!$group) return $this->error('群不存在'); $mark = $this->request->post('mark'); $info = $this->groupMemberService->search()->where('uid', $this->request->uid())->where('chat_group_id', $id)->find(); if (!$info) return $this->error('你没有权限'); $info->nickname = $mark; $res = $info->save(); if ($res) return $this->success('修改成功'); else return $this->error('修改失败'); } /** * 修改群信息 * @param $id * @return mixed * @throws DataNotFoundException * @throws DbException * @throws ModelNotFoundException * @para */ public function editGroup($id) { $info = $this->request->post('info', ''); $name = $this->request->post('name', ''); $avatar = $this->request->post('avatar', ''); $group = $this->groupService->get($id); if (!$group) return $this->error('群不存在'); if (!$this->groupMemberService->search()->where('uid', $this->request->uid())->where('chat_group_id', $id)->where('is_hoster', 1)->find()) { return $this->error('你没有权限'); } $res = $this->groupService->update($id, ['name' => $name, 'info' => $info, 'avatar' => $avatar]); if ($res) return $this->success('修改成功'); else return $this->error('修改失败'); } /** * 新增群公告 * @param $id * @return mixed * @throws DataNotFoundException * @throws DbException * @throws ModelNotFoundException * @para */ public function setNotice($id) { $notice = $this->request->post('notice', ''); $group = $this->groupService->get($id); if (!$group) return $this->error('群不存在'); if (!$this->groupMemberService->search()->where('uid', $this->request->uid())->where('chat_group_id', $id)->where('is_hoster', 1)->find()) { return $this->error('你没有权限'); } $res = $this->groupService->update($id, ['notice' => $notice]); $this->service->create([ 'uid' => $this->request->uid(), 'to_uid' => 0, 'add_time' => time(), 'status' => 1, 'msn_type' => 'announcement', 'group_id' => $id, 'msn' => '群公告' . ':' . $notice, 'group_see' => $this->request->uid(), ]); /** @var ChatGroupMemberServices $chatMemberService */ $chatMemberService = app()->make(ChatGroupMemberServices::class); $members = $chatMemberService->getColumn(['chat_group_id' => $id], 'uid'); foreach ($members as $member) { SocketPush::user()->toGroup($member, $id)->type('announcement') ->data('群公告' . ':' . $notice) ->push(); } $res = $this->groupService->update($id, ['last_message_time' => time()]); if ($res) return $this->success('修改成功', ['type' => 'group:notice', 'data' => ['notice' => $notice]]); else return $this->error('修改失败'); } }