123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282 |
- <?php
- /**
- * @Created by PhpStorm
- * @author: Kirin
- * @day: 2024/11/12
- * @time: 16:35
- */
- namespace app\controller\api\chat;
- use app\common\ApiBaseController;
- use app\Request;
- use app\services\chat\ChatGroupMemberServices;
- use app\services\chat\ChatGroupServices;
- use app\services\chat\ChatLogServices;
- use app\services\user\UserServices;
- use app\webscoket\SocketPush;
- use think\db\exception\DataNotFoundException;
- use think\db\exception\DbException;
- use think\db\exception\ModelNotFoundException;
- use think\Exception;
- /**
- * Class ChatGroup
- * @package app\controller\api\chat
- */
- class ChatGroup extends ApiBaseController
- {
- /** @var ChatGroupServices $groupService */
- private $groupService;
- /** @var ChatGroupMemberServices $groupMemberService */
- private $groupMemberService;
- /**
- * @param Request $request
- * @param ChatLogServices $service
- * @param ChatGroupMemberServices $groupMemberService
- * @param ChatGroupServices $groupService
- */
- public function __construct(Request $request, ChatLogServices $service, ChatGroupMemberServices $groupMemberService, ChatGroupServices $groupService)
- {
- parent::__construct($request);
- $this->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('修改失败');
- }
- }
|