ChatGroup.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. <?php
  2. /**
  3. * @Created by PhpStorm
  4. * @author: Kirin
  5. * @day: 2024/11/12
  6. * @time: 16:35
  7. */
  8. namespace app\controller\api\chat;
  9. use app\common\ApiBaseController;
  10. use app\Request;
  11. use app\services\chat\ChatGroupMemberServices;
  12. use app\services\chat\ChatGroupServices;
  13. use app\services\chat\ChatLogServices;
  14. use app\services\user\UserServices;
  15. use app\webscoket\SocketPush;
  16. use think\db\exception\DataNotFoundException;
  17. use think\db\exception\DbException;
  18. use think\db\exception\ModelNotFoundException;
  19. use think\Exception;
  20. /**
  21. * Class ChatGroup
  22. * @package app\controller\api\chat
  23. */
  24. class ChatGroup extends ApiBaseController
  25. {
  26. /** @var ChatGroupServices $groupService */
  27. private $groupService;
  28. /** @var ChatGroupMemberServices $groupMemberService */
  29. private $groupMemberService;
  30. /**
  31. * @param Request $request
  32. * @param ChatLogServices $service
  33. * @param ChatGroupMemberServices $groupMemberService
  34. * @param ChatGroupServices $groupService
  35. */
  36. public function __construct(Request $request, ChatLogServices $service, ChatGroupMemberServices $groupMemberService, ChatGroupServices $groupService)
  37. {
  38. parent::__construct($request);
  39. $this->service = $service;
  40. $this->groupMemberService = $groupMemberService;
  41. $this->groupService = $groupService;
  42. }
  43. /**
  44. * 我的群列表
  45. * @para
  46. * @return mixed
  47. * @throws DataNotFoundException
  48. * @throws DbException
  49. * @throws ModelNotFoundException
  50. */
  51. public function group()
  52. {
  53. $groups = $this->groupMemberService->getColumn(['uid' => $this->request->uid()], 'id');
  54. $list = $this->groupService->search()->where('id', 'in', $groups)
  55. ->order('last_message_time', 'desc')
  56. ->select();
  57. return $this->success('ok', compact('list'));
  58. }
  59. /**
  60. * 群详情
  61. * @param $id
  62. * @para
  63. * @return mixed
  64. * @throws DataNotFoundException
  65. * @throws DbException
  66. * @throws ModelNotFoundException
  67. */
  68. public function groupInfo($id)
  69. {
  70. $group = $this->groupService->get($id);
  71. if (!$group) return $this->error('群不存在');
  72. return $this->success('ok', compact('group'));
  73. }
  74. /**
  75. * 群成员列表
  76. * @param $id
  77. * @para
  78. * @return mixed
  79. * @throws DataNotFoundException
  80. * @throws DbException
  81. * @throws ModelNotFoundException
  82. */
  83. public function groupMembers($id)
  84. {
  85. $group = $this->groupService->get($id);
  86. if (!$group) return $this->error('群不存在');
  87. $list = $this->groupMemberService->search()->where('chat_group_id', $id)->select();
  88. foreach ($list as &$v) {
  89. $v['ext_info'] = $this->service->getInfo($v['identity'], $v['uid']);
  90. }
  91. return $this->success('ok', compact('list'));
  92. }
  93. /**
  94. * 添加群成员
  95. * @param $id
  96. * @return mixed
  97. * @throws DataNotFoundException
  98. * @throws DbException
  99. * @throws ModelNotFoundException
  100. * @para
  101. */
  102. public function groupAddMember($id)
  103. {
  104. $group = $this->groupService->get($id);
  105. if (!$group) return $this->error('群不存在');
  106. if (!$this->groupMemberService->search()->where('uid', $this->request->uid())->where('chat_group_id', $id)->where('is_hoster', 1)->find()) {
  107. return $this->error('你没有权限');
  108. }
  109. $uid = $this->request->post('uid', 0);
  110. $identity = $this->request->post('identity', 0);
  111. if ($this->groupMemberService->search()->where('uid', $uid)->where('chat_group_id', $id)->find()) {
  112. return $this->error('用户已在群聊中');
  113. }
  114. /** @var UserServices $userService */
  115. $userService = app()->make(UserServices::class);
  116. $user = $userService->getUserInfo($uid);
  117. if (!$user) return $this->error('用户不存在');
  118. $ext_info = $this->service->getInfo($identity, $uid);
  119. if (!$ext_info) return $this->error('用户不存在');
  120. try {
  121. $this->groupMemberService->create([
  122. 'uid' => $uid,
  123. 'chat_group_id' => $id,
  124. 'identity' => $identity,
  125. ]);
  126. $this->service->create([
  127. 'uid' => $uid,
  128. 'to_uid' => 0,
  129. 'add_time' => time(),
  130. 'status' => 1,
  131. 'msn_type' => 'notice',
  132. 'group_id' => $id,
  133. 'msn' => $ext_info['info']['name'] . ' 加入群聊',
  134. 'group_see' => $uid,
  135. ]);
  136. $this->groupService->update($id, ['last_message_time' => time()]);
  137. 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']);
  138. } catch (Exception $e) {
  139. return $this->error($e->getMessage());
  140. }
  141. }
  142. /**
  143. * 移除群成员
  144. * @param $id
  145. * @return mixed
  146. * @throws DataNotFoundException
  147. * @throws DbException
  148. * @throws ModelNotFoundException
  149. * @para
  150. */
  151. public function groupRemoveMember($id)
  152. {
  153. $group = $this->groupService->get($id);
  154. if (!$group) return $this->error('群不存在');
  155. if (!$this->groupMemberService->search()->where('uid', $this->request->uid())->where('chat_group_id', $id)->where('is_hoster', 1)->find()) {
  156. return $this->error('你没有权限');
  157. }
  158. $uid = $this->request->post('uid', 0);
  159. if (!$user = $this->groupMemberService->search()->where('uid', $uid)->where('chat_group_id', $id)->find()) {
  160. return $this->error('用户不在群聊中');
  161. }
  162. if ($user['is_hoster']) return $this->error('群主不能移除');
  163. try {
  164. $this->groupMemberService->delete($user['id']);
  165. return $this->success('ok');
  166. } catch (Exception $e) {
  167. return $this->error($e->getMessage());
  168. }
  169. }
  170. /**
  171. * 修改群昵称
  172. * @param $id
  173. * @return mixed
  174. * @throws DataNotFoundException
  175. * @throws DbException
  176. * @throws ModelNotFoundException
  177. */
  178. public function setMark($id)
  179. {
  180. $group = $this->groupService->get($id);
  181. if (!$group) return $this->error('群不存在');
  182. $mark = $this->request->post('mark');
  183. $info = $this->groupMemberService->search()->where('uid', $this->request->uid())->where('chat_group_id', $id)->find();
  184. if (!$info) return $this->error('你没有权限');
  185. $info->nickname = $mark;
  186. $res = $info->save();
  187. if ($res)
  188. return $this->success('修改成功');
  189. else
  190. return $this->error('修改失败');
  191. }
  192. /**
  193. * 修改群信息
  194. * @param $id
  195. * @return mixed
  196. * @throws DataNotFoundException
  197. * @throws DbException
  198. * @throws ModelNotFoundException
  199. * @para
  200. */
  201. public function editGroup($id)
  202. {
  203. $info = $this->request->post('info', '');
  204. $name = $this->request->post('name', '');
  205. $avatar = $this->request->post('avatar', '');
  206. $group = $this->groupService->get($id);
  207. if (!$group) return $this->error('群不存在');
  208. if (!$this->groupMemberService->search()->where('uid', $this->request->uid())->where('chat_group_id', $id)->where('is_hoster', 1)->find()) {
  209. return $this->error('你没有权限');
  210. }
  211. $res = $this->groupService->update($id, ['name' => $name, 'info' => $info, 'avatar' => $avatar]);
  212. if ($res)
  213. return $this->success('修改成功');
  214. else
  215. return $this->error('修改失败');
  216. }
  217. /**
  218. * 新增群公告
  219. * @param $id
  220. * @return mixed
  221. * @throws DataNotFoundException
  222. * @throws DbException
  223. * @throws ModelNotFoundException
  224. * @para
  225. */
  226. public function setNotice($id)
  227. {
  228. $notice = $this->request->post('notice', '');
  229. $group = $this->groupService->get($id);
  230. if (!$group) return $this->error('群不存在');
  231. if (!$this->groupMemberService->search()->where('uid', $this->request->uid())->where('chat_group_id', $id)->where('is_hoster', 1)->find()) {
  232. return $this->error('你没有权限');
  233. }
  234. $res = $this->groupService->update($id, ['notice' => $notice]);
  235. $this->service->create([
  236. 'uid' => $this->request->uid(),
  237. 'to_uid' => 0,
  238. 'add_time' => time(),
  239. 'status' => 1,
  240. 'msn_type' => 'announcement',
  241. 'group_id' => $id,
  242. 'msn' => '群公告' . ':' . $notice,
  243. 'group_see' => $this->request->uid(),
  244. ]);
  245. /** @var ChatGroupMemberServices $chatMemberService */
  246. $chatMemberService = app()->make(ChatGroupMemberServices::class);
  247. $members = $chatMemberService->getColumn(['chat_group_id' => $id], 'uid');
  248. foreach ($members as $member) {
  249. SocketPush::user()->toGroup($member, $id)->type('announcement')
  250. ->data('群公告' . ':' . $notice)
  251. ->push();
  252. }
  253. $res = $this->groupService->update($id, ['last_message_time' => time()]);
  254. if ($res)
  255. return $this->success('修改成功', ['type' => 'group:notice', 'data' => ['notice' => $notice]]);
  256. else
  257. return $this->error('修改失败');
  258. }
  259. }