ChatService.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981
  1. <?php
  2. namespace App\JsonRpc;
  3. use App\Model\ChatFriends;
  4. use App\Model\ChatGroups;
  5. use App\Model\ChatGroupsMember;
  6. use App\Model\ChatRecords;
  7. use App\Model\ChatTopic;
  8. use App\Model\ChatTopicsReply;
  9. use App\Model\User;
  10. use App\Tools\PublicData;
  11. use App\Tools\Result;
  12. use Hyperf\DbConnection\Db;
  13. use Hyperf\RpcServer\Annotation\RpcService;
  14. #[RpcService(name: "ChatService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
  15. class ChatService implements ChatServiceInterface
  16. {
  17. /**
  18. * 获取用户信息
  19. * @param array $data
  20. * @return array
  21. */
  22. public function getFriendInfo(array $data): array
  23. {
  24. $result = ChatFriends::where('friend_id', $data['friend_id'])
  25. ->where('user_id', $data['user_id'])
  26. ->select(
  27. 'chat_friends.*',
  28. 'user.user_name',
  29. 'user.mobile',
  30. 'user.nickname',
  31. 'user.avatar'
  32. )
  33. ->leftJoin('user', 'user.id', '=', 'chat_friends.friend_id')
  34. ->first();
  35. return Result::success($result);
  36. }
  37. /**
  38. * 搜索好友
  39. * @param array $data
  40. * @return array
  41. */
  42. public function searchFriend(array $data): array
  43. {
  44. $keyword = $data['keyword'];
  45. $userId = $data['user_id'];
  46. $result = User::where('user_name', 'like', '%' . $data['keyword'] . '%')
  47. ->orWhere('nickname', 'like', '%' . $data['keyword'] . '%')
  48. ->orWhere('mobile', 'like', '%' . $data['keyword'] . '%')
  49. ->leftJoin('chat_friends', function ($join) use ($userId) {
  50. $join->on('user.id', '=', 'chat_friends.friend_id')
  51. ->where('chat_friends.user_id', $userId);
  52. })
  53. ->select('user.*', 'chat_friends.remark as remark', 'chat_friends.id as isfriend')
  54. ->get();
  55. if ($result) {
  56. return Result::success($result);
  57. } else {
  58. return Result::error('没有找到相关好友');
  59. }
  60. }
  61. /**
  62. * 添加申请
  63. * @param array $data
  64. * @return array
  65. */
  66. public function addFriend(array $data): array
  67. {
  68. Db::beginTransaction();
  69. try
  70. {
  71. // 检查是否存在相同的记录
  72. $existingRecord = ChatFriends::where([
  73. 'user_id' => $data['user_id'],
  74. 'friend_id' => $data['friend_id'],
  75. ])->first();
  76. if ($existingRecord) {
  77. Db::rollBack();
  78. if ($existingRecord->status == 1) {
  79. return Result::error("好友申请已存在", 0);
  80. } elseif ($existingRecord->status == 2) {
  81. return Result::error("已经是好友关系", 0);
  82. }
  83. }
  84. $result = ChatFriends::insertGetId($data);
  85. Db::commit();
  86. } catch (\Throwable $ex) {
  87. Db::rollBack();
  88. var_dump($ex->getMessage());
  89. return Result::error("添加好友申请失败", 0);
  90. }
  91. return Result::success("添加成功");
  92. }
  93. /**
  94. * 好友列表
  95. * @param array $data
  96. * @return array
  97. */
  98. public function getFriendsList(array $data): array
  99. {
  100. var_dump($data);
  101. $result = ChatFriends::leftJoin('user', 'user.id', '=', 'chat_friends.friend_id')
  102. ->select(
  103. 'chat_friends.*',
  104. 'user.user_name',
  105. 'user.mobile',
  106. 'user.nickname',
  107. 'user.avatar'
  108. )
  109. ->where([
  110. ['user_id', $data['user_id']],
  111. ['chat_friends.status', $data['status']], // 明确指定 chat_friends 表中的 status 列
  112. ])
  113. ->get();
  114. return Result::success($result);
  115. }
  116. /**
  117. * 好友列表
  118. * @param array $data
  119. * @return array
  120. */
  121. public function getFriendsApplyList(array $data): array
  122. {
  123. var_dump($data);
  124. $result = ChatFriends::leftJoin('user', 'user.id', '=', 'chat_friends.user_id')
  125. ->select(
  126. 'chat_friends.*',
  127. 'user.user_name',
  128. 'user.mobile',
  129. 'user.nickname',
  130. 'user.avatar'
  131. )
  132. ->where([
  133. ['friend_id', $data['friend_id']],
  134. ['chat_friends.status', $data['status']], // 明确指定 chat_friends 表中的 status 列
  135. ])
  136. ->get();
  137. return Result::success($result);
  138. }
  139. /**
  140. * 更新申请
  141. * @param array $data
  142. * @return array
  143. */
  144. public function applyFriend(array $data): array
  145. {
  146. $status = $data['status'];
  147. //判断同意还是不同意
  148. if ($status == 2) {
  149. Db::beginTransaction();
  150. try {
  151. $where = [
  152. 'id' => $data['id'],
  153. 'status' => 1,
  154. ];
  155. $fr = ChatFriends::where($where)->first();
  156. if (empty($fr)) {
  157. Db::rollBack();
  158. return Result::error("好友申请记录不存在,已经是好友了", 0);
  159. }
  160. $data['status'] = $status;
  161. $data['applied_at'] = date("Y-m-d H:i:s"); //好友审核时间操作人friend_id
  162. unset($data['user_id']);
  163. ChatFriends::where($where)->update($data);
  164. Db::table('chat_friends')
  165. ->updateOrInsert(
  166. [
  167. 'user_id' => $fr['friend_id'],
  168. 'friend_id' => $fr['user_id'],
  169. ],
  170. [
  171. 'status' => $status,
  172. ]
  173. );
  174. Db::commit();
  175. } catch (\Throwable $ex) {
  176. Db::rollBack();
  177. var_dump($ex->getMessage());
  178. return Result::error("同意添加为好友失败", 0);
  179. }
  180. return Result::success(['添加成功']);
  181. } else if ($status == 4) { //拒绝
  182. Db::beginTransaction();
  183. try {
  184. $where1 = [
  185. 'id' => $data['id'],
  186. ];
  187. $deletedRows = ChatFriends::where($where1)->delete();
  188. if ($deletedRows > 0) {
  189. Db::commit();
  190. } else {
  191. // 处理记录不存在的情况
  192. Db::rollBack();
  193. throw new \Exception('记录不存在');
  194. }
  195. } catch (\Throwable $ex) {
  196. Db::rollBack();
  197. var_dump($ex->getMessage());
  198. return Result::error("拒绝添加好友失败", 0);
  199. }
  200. return Result::success(['已经拒绝']);
  201. }
  202. }
  203. /**
  204. * 删除好友
  205. * @param array $data
  206. * @return array
  207. */
  208. public function delFriend(array $data): array
  209. {
  210. $where = [
  211. 'user_id' => $data['user_id'],
  212. 'friend_id' => $data['friend_id'],
  213. ];
  214. $orwhere = [
  215. 'friend_id' => $data['user_id'],
  216. 'user_id' => $data['friend_id'],
  217. ];
  218. $result = ChatFriends::where($where)
  219. ->orWhere($orwhere)->delete();
  220. var_dump($result, '-0------------------');
  221. ChatRecords::where($where)->orWhere($orwhere)->delete();
  222. if ($result) {
  223. return Result::success("删除成功”");
  224. } else {
  225. return Result::error('删除失败');
  226. }
  227. }
  228. /**
  229. * 是否好友
  230. * @param array $data
  231. * @return array
  232. */
  233. public function isFriend(array $data): array
  234. {
  235. $where = [
  236. 'user_id' => $data['user_id'],
  237. 'friend_id' => $data['friend_id'],
  238. ];
  239. $result = ChatFriends::where($where)->first();
  240. if ($result) {
  241. return Result::success(true);
  242. } else {
  243. return Result::error('不是好友');
  244. }
  245. }
  246. /**
  247. * 添加聊天内容
  248. * @param array $data
  249. * @return array
  250. */
  251. public function addChatRecords(array $data): array
  252. {
  253. Db::beginTransaction();
  254. try {;
  255. //添加会话内容
  256. $ChatRecordsData = [[
  257. 'msg_type' => $data['msg_type'] ?? 0,
  258. 'user_id' => $data['user_id'] ?? 0,
  259. 'is_read' => $data['is_read'] ?? 0,
  260. 'talk_type' => $data['talk_type'] ?? 0,
  261. 'action' => $data['action'] ?? 0,
  262. 'group_receiver_id' => $data['group_receiver_id'] ?? 0,
  263. 'content' => $data['content'] ?? '',
  264. 'receiver_id' => $data['receiver_id'] ?? '',
  265. ]];
  266. ChatRecords::insert($ChatRecordsData);
  267. Db::commit();} catch (\Throwable $ex) {
  268. Db::rollBack();
  269. var_dump($ex->getMessage());
  270. return Result::error("存储消息失败", 0);
  271. }
  272. return Result::success([]);
  273. }
  274. /**
  275. * 修改好友备注
  276. * @param array $data
  277. * @return array
  278. */
  279. public function updateFriend(array $data): array
  280. {
  281. $result = ChatFriends::where(['user_id' => $data['user_id'],
  282. 'friend_id' => $data['friend_id'],
  283. 'status' => 2,
  284. ])->update(['remark' => $data['remark']]);
  285. if ($result) {
  286. return Result::success('修改成功');
  287. } else {
  288. return Result::error('修改失败');
  289. }
  290. }
  291. /**
  292. * 会话列表
  293. * @param array $data
  294. * @return array
  295. */
  296. public function getConversation(array $data): array
  297. {
  298. $userId = $data['user_id'];
  299. $unreadMessages = ChatRecords::where('user_id', $userId)
  300. ->where('is_read', 0)
  301. // ->where('action', 'recieved')
  302. ->leftJoin('user', 'chat_records.receiver_id', '=', 'user.id')
  303. ->leftJoin('chat_groups', 'chat_records.receiver_id', '=', 'chat_groups.id')
  304. ->select(
  305. 'receiver_id',
  306. DB::raw('COUNT(receiver_id) AS num'),
  307. DB::raw('MAX(chat_records.id) AS max_id'),
  308. 'user.user_name as user_name',
  309. 'user.avatar as avatar',
  310. 'user.mobile as mobile',
  311. 'chat_groups.group_name as group_name'
  312. )
  313. ->groupBy('receiver_id')
  314. ->orderBy(DB::raw('MAX(chat_records.id)'), 'desc')
  315. ->get();
  316. // 查询已读消息,并将 num 字段设置为 0
  317. $readMessages = ChatRecords::where('user_id', $userId)
  318. ->where('is_read', 1)
  319. // ->where('action', 'recieved')
  320. ->leftJoin('user', 'chat_records.receiver_id', '=', 'user.id')
  321. ->leftJoin('chat_groups', 'chat_records.receiver_id', '=', 'chat_groups.id')
  322. ->select(
  323. 'receiver_id',
  324. DB::raw('0 AS num'),
  325. DB::raw('MAX(chat_records.id) AS max_id'),
  326. 'user.user_name as user_name',
  327. 'user.avatar as avatar',
  328. 'user.mobile as mobile',
  329. 'chat_groups.group_name as group_name'
  330. )
  331. ->groupBy('receiver_id')
  332. ->orderBy(DB::raw('MAX(chat_records.id)'), 'desc')
  333. ->get();
  334. // 合并未读消息和已读消息
  335. // $allMessages = array_merge($unreadMessages->toArray(), $readMessages->toArray());
  336. // 使用关联数组去重,并优先保留未读消息
  337. $allMessages = [];
  338. foreach ($unreadMessages as $message) {
  339. $allMessages[$message['receiver_id']] = $message->toArray();
  340. }
  341. foreach ($readMessages as $message) {
  342. if (strlen($message['receiver_id']) === 18) {
  343. } else {
  344. // $allMessages[$message['receiver_id']] = $message->toArray();
  345. }
  346. if (!isset($allMessages[$message['receiver_id']])) {
  347. $allMessages[$message['receiver_id']] = $message->toArray();
  348. }
  349. }
  350. // var_dump($allMessages);
  351. // 处理结果,判断是否是群聊
  352. $formattedMessages = [];
  353. foreach ($allMessages as $message) {
  354. $formattedMessage = [
  355. 'receiver_id' => $message['receiver_id'],
  356. 'num' => $message['num'],
  357. 'max_id' => $message['max_id'],
  358. 'user_name' => $message['user_name'],
  359. 'avatar' => $message['avatar'],
  360. 'mobile' => $message['mobile'],
  361. 'group_name' => $message['group_name'],
  362. ];
  363. if (strlen($message['receiver_id']) === 18) { // 判断是否是 UUID
  364. $formattedMessage['type'] = 'group';
  365. $formattedMessage['name'] = $message['group_name'];
  366. $formattedMessage['is_group'] = 1;
  367. } else {
  368. $formattedMessage['type'] = 'user';
  369. $formattedMessage['name'] = $message['user_name'];
  370. $formattedMessage['is_group'] = 0;
  371. }
  372. $formattedMessages[] = $formattedMessage;
  373. }
  374. if (!empty($formattedMessages)) {
  375. return Result::success($formattedMessages);
  376. } else {
  377. return Result::error('没有消息');
  378. }
  379. }
  380. /**
  381. * 获取聊天记录
  382. * @param array $data
  383. * @return array
  384. */
  385. public function getChatRecords(array $data): array
  386. {
  387. var_dump('222222');
  388. Db::beginTransaction();
  389. try {
  390. $userId = $data['user_id'];
  391. $friendId = $data['friend_id'];
  392. $result = ChatRecords::where(function ($query) use ($userId, $friendId) {
  393. $query->where('user_id', $userId)->where('receiver_id', $friendId);
  394. })
  395. // ->orWhere(function ($query) use ($userId, $friendId) {
  396. // $query->where('user_id', $friendId)->where('receiver_id', $userId);
  397. // })
  398. ->leftJoin('user as u1', 'chat_records.user_id', '=', 'u1.id')
  399. ->leftJoin('user as u2', 'chat_records.receiver_id', '=', 'u2.id')
  400. ->select('chat_records.*', 'u1.user_name as user_id_name', 'u1.avatar as user_avatar', 'u2.user_name as receiver_id_name', 'u2.avatar as receiver_avatar')
  401. ->orderBy('id', 'asc')->paginate(100, ['*'], 'page', $data['page'] ?? 1);
  402. //更新聊天记录已读
  403. ChatRecords::where('user_id', $userId)
  404. ->where('receiver_id', $friendId)
  405. ->where('is_read', 0)
  406. ->where('talk_type', 1)
  407. ->update(['is_read' => 1]);
  408. Db::commit();
  409. } catch (\Throwable $ex) {
  410. Db::rollBack();
  411. var_dump($ex->getMessage());
  412. return Result::error("获取聊天记录失败", 0);
  413. }
  414. if ($result) {
  415. return Result::success($result);
  416. } else {
  417. return Result::error('没有聊天记录');
  418. }
  419. }
  420. /**
  421. * 获取群聊天记录
  422. * @param array $data
  423. * @return array
  424. */
  425. public function getGroupChatRecords(array $data): array
  426. {
  427. Db::beginTransaction();
  428. try {
  429. $userId = $data['user_id'];
  430. $group_id = $data['group_id'];
  431. $result = ChatRecords::where('receiver_id', $group_id)
  432. ->where('user_id', $userId)
  433. ->leftJoin('user as u1', 'chat_records.user_id', '=', 'u1.id')
  434. ->leftJoin('user as u2', 'chat_records.group_receiver_id', '=', 'u2.id')
  435. ->select('chat_records.*', 'u1.user_name as user_id_name', 'u1.avatar as user_avatar', 'u2.user_name as receiver_id_name', 'u2.avatar as receiver_avatar')
  436. ->orderBy('id', 'asc')->paginate(100, ['*'], 'page', $data['page'] ?? 1);
  437. //更新群聊天记录
  438. ChatRecords::where('receiver_id', $group_id)
  439. ->where('user_id', $userId)
  440. ->update(['is_read' => 1]);
  441. Db::commit();
  442. } catch (\Throwable $ex) {
  443. Db::rollBack();
  444. var_dump($ex->getMessage());
  445. return Result::error("获取群聊天记录失败", 0);
  446. }
  447. if ($result) {
  448. return Result::success($result);
  449. } else {
  450. return Result::error('没有群消息');
  451. }
  452. }
  453. /**
  454. * 群组 - 创建群
  455. * @param array $data
  456. * @return array
  457. */
  458. public function addGroup(array $data): array
  459. {
  460. Db::beginTransaction();
  461. try {
  462. //创建群
  463. $groupData = [
  464. 'id' => PublicData::uuid(),
  465. 'creator_id' => $data['user_id'],
  466. 'group_name' => $data['group_name'],
  467. 'avatar' => $data['avatar'] ?? '',
  468. 'profile' => $data['profile'] ?? '',
  469. ];
  470. ChatGroups::insert($groupData);
  471. //创建群用户
  472. $groupMemberData = [];
  473. if ($data['group_member']) {
  474. foreach ($data['group_member'] as $key => $val) {
  475. $groupMemberData[$key] = [
  476. 'id' => PublicData::uuid(),
  477. 'group_id' => $groupData['id'],
  478. 'user_id' => $val,
  479. 'leader' => $data['user_id'] == $val ? 2 : 0,
  480. ];
  481. }
  482. }
  483. ChatGroupsMember::insert($groupMemberData);
  484. //插入一条消息
  485. $chatRecordsData = [
  486. 'user_id' => $data['user_id'],
  487. 'receiver_id' => $groupData['id'],
  488. 'content' => '我创建了一个群' . Date('Y-m-d H:i:s'),
  489. 'msg_type' => 1,
  490. 'is_read' => 0,
  491. 'talk_type' => 2,
  492. 'action' => 'said',
  493. 'group_receiver_id' => $data['user_id'],
  494. ];
  495. ChatRecords::insert($chatRecordsData);
  496. Db::commit();
  497. } catch (\Throwable $ex) {
  498. Db::rollBack();
  499. var_dump($ex->getMessage());
  500. return Result::error("创建群失败", 0);
  501. }
  502. return Result::success([]);
  503. }
  504. /**
  505. * 群组 - 加入群
  506. * @param array $data
  507. * @return array
  508. */
  509. public function addGroupMember(array $data): array
  510. {
  511. $result = ChatGroupsMember::where(['group_id' => $data['group_id'], 'user_id' => $data['user_id']])->update(['leader' => 2]);
  512. if ($result) {
  513. return Result::success('修改成功');
  514. } else {
  515. return Result::error('修改失败');
  516. }
  517. }
  518. /**
  519. * 群组 - 群信息
  520. * @param array $data
  521. * @return array
  522. */
  523. public function getGroupInfo(array $data): array
  524. {
  525. $result = ChatGroups::where(['chat_groups.id' => $data['group_id']])
  526. ->join('user', 'chat_groups.creator_id', '=', 'user.id')
  527. ->select('chat_groups.*', 'user.user_name as user_name', 'user.avatar as avatar')
  528. ->first();
  529. return Result::success($result);
  530. }
  531. /**
  532. * 群组 - 删除群
  533. * @param array $data
  534. * @return array
  535. */
  536. public function delGroup(array $data): array
  537. {
  538. Db::beginTransaction();
  539. try {
  540. $groupMember = ChatGroupsMember::where(['group_id' => $data['group_id']])->delete();
  541. $result = ChatGroups::where(['id' => $data['group_id']])->delete();
  542. $result = ChatRecords::where(['receiver_id' => $data['group_id']])->delete();
  543. //群聊记录
  544. Db::commit();
  545. } catch (\Throwable $ex) {
  546. Db::rollBack();
  547. var_dump($ex->getMessage());
  548. return Result::error("删除群失败", 0);
  549. }
  550. if ($result) {
  551. return Result::success('删除成功');
  552. } else {
  553. return Result::error('删除失败');
  554. }
  555. }
  556. /**
  557. * 群组 - 退出群
  558. * @param array $data
  559. * @return array
  560. */
  561. public function quitGroup(array $data): array
  562. {
  563. $result = ChatGroupsMember::where(['group_id' => $data['group_id'], 'user_id' => $data['user_id']])->delete();
  564. ChatRecords::where(['receiver_id' => $data['group_id'], 'user_id' => $data['user_id']])->delete();
  565. if ($result) {
  566. return Result::success('退出成功');
  567. } else {
  568. return Result::error('退出失败');
  569. }
  570. }
  571. /**
  572. * 群组 - 我的群
  573. * @param array $data
  574. * @return array
  575. */
  576. public function getGroupList(array $data): array
  577. {
  578. $result = ChatGroupsMember::where(['user_id' => $data['user_id']])
  579. ->leftJoin('chat_groups', 'chat_groups_members.group_id', '=', 'chat_groups.id')
  580. ->select('chat_groups.*', 'chat_groups_members.group_id as group_id')
  581. ->orderBy('chat_groups.id', 'desc')
  582. ->paginate(100, ['*'], 'page', $data['page'] ?? 1);
  583. return Result::success($result);
  584. }
  585. /**
  586. * 群组 - 删除群成员
  587. * @param array $data
  588. * @return array
  589. */
  590. public function delGroupMembers(array $data): array
  591. {
  592. $result = ChatGroupsMember::where(['group_id' => $data['group_id'], 'user_id' => $data['user_id']])->delete();
  593. if ($result) {
  594. return Result::success('删除成功');
  595. } else {
  596. return Result::error('删除失败');
  597. }
  598. }
  599. /**
  600. * 群组 - 更新群
  601. * @param array $data
  602. * @return array
  603. */
  604. public function updateGroup(array $data): array
  605. {
  606. // 提取需要的字段
  607. $groupId = $data['group_id'];
  608. $groupName = $data['group_name'] ?? null;
  609. $profile = $data['profile'] ?? null;
  610. $avatar = $data['avatar'] ?? null;
  611. // 查询群组
  612. $group = ChatGroups::find($groupId);
  613. if (!$group) {
  614. return Result::error('群组不存在');
  615. }
  616. // 更新群组信息
  617. if ($groupName !== null) {
  618. $group->group_name = $groupName;
  619. }
  620. if ($profile !== null) {
  621. $group->profile = $profile;
  622. }
  623. if ($avatar !== null) {
  624. $group->avatar = $avatar;
  625. }
  626. // 保存更改
  627. if ($group->save()) {
  628. return Result::success($group->toArray());
  629. } else {
  630. return Result::error('更新群组信息失败');
  631. }
  632. }
  633. /**
  634. * 群组 - 删除群
  635. * @param array $data
  636. * @return array
  637. */
  638. public function deleteGroup(array $data): array
  639. {
  640. $result = ChatGroups::where(['id' => $data['group_id']])->delete();
  641. if ($result) {
  642. return Result::success('删除成功');
  643. } else {
  644. return Result::error('删除失败');
  645. }
  646. }
  647. /**
  648. * 群组 - 群用户列表
  649. * @param array $data
  650. * @return array
  651. */
  652. public function getGroupMembers(array $data): array
  653. {
  654. $groupMember = ChatGroupsMember::where(['group_id' => $data['group_id']])
  655. ->leftJoin('user as u1', 'chat_groups_members.user_id', '=', 'u1.id')
  656. ->select('chat_groups_members.*', 'u1.user_name', 'u1.avatar')
  657. ->orderBy('id', 'desc')
  658. ->get();
  659. return Result::success($groupMember);
  660. }
  661. /**
  662. * 群组 - 添加群
  663. * @param array $data
  664. * @return array
  665. */
  666. public function joinGroup(array $data): array
  667. {
  668. $group = ChatGroups::where(['id' => $data['group_id']])->first();
  669. if (empty($group)) {
  670. return Result::error("群不存在", 0);
  671. }
  672. $groupMember = ChatGroupsMember::where(['user_id' => $data['user_id'], 'group_id' => $data['group_id']])->first();
  673. if ($groupMember) {
  674. return Result::error("已加入群", 0);
  675. }
  676. $info = [
  677. 'id' => PublicData::uuid(),
  678. 'user_id' => $data['user_id'],
  679. 'group_id' => $data['group_id'],
  680. ];
  681. $result = ChatGroupsMember::insertGetId($info);
  682. if ($result) {
  683. return Result::success($data);
  684. } else {
  685. return Result::error($data);
  686. };
  687. }
  688. /**
  689. * 话题 - 列表
  690. * @param array $data
  691. * @return array
  692. */
  693. public function getTopicsList(array $data): array
  694. {
  695. $where = [];
  696. if (!empty($data['title'])) {
  697. $where[] = ['chat_topics.title', 'like', '%' . $data['title'] . '%'];
  698. }
  699. if (!empty($data['user_id'])) {
  700. $where[] = ['chat_topics.user_id', '=', $data['user_id']];
  701. }
  702. if (!empty($data['status'])) {
  703. $where[] = ['chat_topics.status', '=', $data['status']];
  704. }
  705. if (!empty($data['type'])) {
  706. $where[] = ['chat_topics.type', '=', $data['type']];
  707. }
  708. if (!empty($data['nickname'])) {
  709. $where[] = ['user.nickname', '=', $data['nickname']];
  710. }
  711. var_dump($where);
  712. $result = ChatTopic::where($where)
  713. ->leftJoin('user', 'user.id', '=', 'chat_topics.user_id')
  714. ->leftJoin('chat_topics_reply', 'chat_topics.id', '=', 'chat_topics_reply.topic_id')
  715. ->select('chat_topics.*', 'user.nickname', 'user.avatar', 'user.user_name'
  716. ,
  717. DB::raw('count(chat_topics_reply.id) as num'))
  718. ->groupBy('chat_topics.id')
  719. ->orderBy('chat_topics.id', 'desc')
  720. ->paginate($data['page_size'], ['*'], 'page', $data['page'] ?? 1);
  721. return Result::success($result);
  722. }
  723. public function getTopic(array $data): array
  724. {
  725. $result = ChatTopic::where(['id' => $data['id']])->first();
  726. return Result::success($result);
  727. }
  728. public function addTopic(array $data): array
  729. {
  730. $chattopic = [];
  731. try {
  732. $data['created_at'] = date('Y-m-d H:i:s');
  733. $data['updated_at'] = date('Y-m-d H:i:s');
  734. $result = ChatTopic::insertGetId($data);
  735. if ($result && $data['is_group'] == 1) {
  736. //chat_group
  737. $group_id = PublicData::uuid();
  738. $groupData = [
  739. 'id' => $group_id,
  740. 'creator_id' => $data['user_id'],
  741. 'group_name' => $data['group_name'] ?? '',
  742. 'profile' => $data['profile'] ?? 0,
  743. ];
  744. $groupResult = ChatGroups::insertGetId($groupData);
  745. $groupMemberData = [
  746. 'id' => PublicData::uuid(),
  747. 'user_id' => $data['user_id'],
  748. 'group_id' => $group_id,
  749. 'leader' => 2,
  750. ];
  751. $groupMemberResult = ChatGroupsMember::insertGetId($groupMemberData);
  752. //更新result的 group_id
  753. $data['group_id'] = $group_id;
  754. ChatTopic::where(['id' => $result])->update($data);
  755. //插入一条消息
  756. $chatRecordsData = [
  757. 'user_id' => $data['user_id'],
  758. 'receiver_id' => $group_id,
  759. 'content' => '我创建了一个群' . Date('Y-m-d H:i:s'),
  760. 'msg_type' => 1,
  761. 'is_read' => 0,
  762. 'talk_type' => 2,
  763. 'action' => 'said',
  764. 'group_receiver_id' => $data['user_id'],
  765. ];
  766. ChatRecords::insert($chatRecordsData);
  767. // 查询 Chattopic 数据
  768. $chattopic = Chattopic::find($result);
  769. } else {
  770. $chattopic = Chattopic::find($result);
  771. }
  772. Db::beginTransaction();
  773. Db::commit();
  774. } catch (\Exception $e) {
  775. Db::rollBack();
  776. return Result::error($data, $e->getMessage());
  777. }
  778. return Result::success($chattopic);
  779. }
  780. public function updateTopic(array $data): array
  781. {
  782. $result = ChatTopic::where(['id' => $data['id']])->update($data);
  783. if ($result) {
  784. return Result::success($data);
  785. } else {
  786. return Result::error($data);
  787. };
  788. }
  789. public function delTopic(array $data): array
  790. {
  791. $result = ChatTopic::where(['id' => $data['id']])->delete();
  792. //删除群和成员和聊天
  793. //删除话题回复
  794. if ($result) {
  795. return Result::success($data);
  796. } else {
  797. return Result::error('删除失败');
  798. };
  799. }
  800. public function getTopicInfo(array $data): array
  801. {
  802. $result = ChatTopic::where(['chat_topics.id' => $data['id']])
  803. ->leftJoin('user', 'user.id', '=', 'chat_topics.user_id')
  804. ->select('chat_topics.*', 'user.nickname', 'user.avatar', 'user.user_name')
  805. ->first();
  806. return Result::success($result);
  807. }
  808. public function addReply(array $data): array
  809. {
  810. $result = ChatTopic::where(['id' => $data['id']])->get();
  811. if ($result) {
  812. $replydata['created_at'] = date('Y-m-d H:i:s');
  813. $replydata['updated_at'] = date('Y-m-d H:i:s');
  814. $replydata['content'] = $data['content'];
  815. $replydata['user_id'] = $data['user_id'];
  816. $replydata['topic_id'] = $data['id'];
  817. $re = ChatTopicsReply::insertGetId($replydata);
  818. }
  819. if ($re) {
  820. return Result::success($data);
  821. } else {
  822. return Result::error($data);
  823. }
  824. }
  825. public function getTopicReply(array $data): array
  826. {
  827. var_dump($data);
  828. $result = ChatTopicsReply::where(['topic_id' => $data['id']])
  829. ->leftJoin('user', 'user.id', '=', 'chat_topics_reply.user_id')
  830. ->select('chat_topics_reply.*', 'user.nickname', 'user.avatar', 'user.user_name')
  831. ->paginate($data['page_size'], ['*'], 'page', $data['page'] ?? 1);
  832. return Result::success($result);
  833. }
  834. /**
  835. * 修改群成员
  836. * @param array $data
  837. * @return array
  838. */
  839. public function updateGroupMembers(array $data): array
  840. {
  841. $where = [
  842. 'group_id' => $data['group_id'],
  843. ];
  844. $group_id = $data['group_id'];
  845. //先删除群成员
  846. $result = ChatGroupsMember::where($where)
  847. ->where([["user_id", '!=', $data['user_id']]])->delete();
  848. $groupMemberData = [];
  849. foreach ($data['group_member'] as $value) {
  850. $groupMemberData[] = [
  851. 'id' => PublicData::uuid(),
  852. 'user_id' => $value,
  853. 'group_id' => $group_id,
  854. 'leader' => 0,
  855. ];
  856. }
  857. $result = ChatGroupsMember::where($where)->insert($groupMemberData);
  858. // 获取群信息
  859. $groupInfo = ChatGroups::where(['id' => $group_id])->first();
  860. if ($result) {
  861. return Result::success($groupInfo);
  862. } else {
  863. return Result::error($data);
  864. }
  865. }
  866. public function clearGroupRecords(array $data): array
  867. {
  868. $result = ChatRecords::where(['user_id' => $data['user_id'], 'receiver_id' => $data['id']])->delete();
  869. if ($result) {
  870. return Result::success("删除成功");
  871. } else {
  872. return Result::error("删除失败");
  873. }
  874. }
  875. public function recallRecord(array $data): array
  876. {
  877. //获取所有id,并删除掉
  878. $ids = array_column($data, 'id');
  879. $result = ChatRecords::whereIn('id', $ids)->delete();
  880. if ($result) {
  881. return Result::success("删除成功");
  882. } else {
  883. return Result::error("删除失败");
  884. }
  885. }
  886. public function clearRecords(array $data): array
  887. {
  888. $result = ChatRecords::where(['user_id' => $data['user_id'], 'receiver_id' => $data['friend_id']])->delete();
  889. if ($result) {
  890. return Result::success("删除成功");
  891. } else {
  892. return Result::error("删除失败");
  893. }
  894. }
  895. public function getRecordByContent(array $data): array
  896. {
  897. $result = ChatRecords::where(['chat_records.user_id' => $data['user_id'], 'chat_records.receiver_id' => $data['receiver_id'], 'chat_records.content' => $data['content']])
  898. ->orWhere(['chat_records.receiver_id' => $data['user_id'], 'chat_records.user_id' => $data['receiver_id'], 'chat_records.content' => $data['content']])
  899. ->all();
  900. if ($result) {
  901. return Result::success($result['id']);
  902. } else {
  903. return Result::error("没有数据");
  904. }
  905. }
  906. public function getRecord(array $data): array
  907. {
  908. $result = ChatRecords::where(['chat_records.id' => $data['id']])
  909. ->leftJoin('user', 'user.id', '=', 'chat_records.user_id')
  910. ->leftJoin('user as user2', 'user2.id', '=', 'chat_records.receiver_id')
  911. ->select('chat_records.*', 'user.nickname', 'user.avatar', 'user.user_name', 'user2.nickname as receiver_nickname', 'user2.avatar as receiver_avatar')
  912. ->get();
  913. return Result::success($result);
  914. }
  915. public function delReply(array $data): array
  916. {
  917. $result = ChatTopicsReply::where(['id' => $data['id']])->delete();
  918. if ($result) {
  919. return Result::success("删除成功");
  920. } else {
  921. return Result::error("删除失败");
  922. }
  923. }
  924. public function delAllReply(array $data): array
  925. {
  926. $result = ChatTopicsReply::where(['topic_id' => $data['topicid']])->delete();
  927. if ($result) {
  928. return Result::success("删除成功");
  929. } else {
  930. return Result::error("删除失败");
  931. }
  932. }
  933. public function getTopicsListAdmin(array $data): array
  934. {
  935. $where = [];
  936. if (!empty($data['type'])) {
  937. $where['type'] = $data['type'];
  938. }
  939. if (!empty($data['title'])) {
  940. $where['title'] = $data['title'];
  941. }
  942. $result = ChatTopic::where($where)
  943. ->leftJoin('user', 'user.id', '=', 'chat_topics.user_id')
  944. ->select('chat_topics.*', 'user.nickname', 'user.avatar', 'user.user_name')
  945. ->paginate($data['page_size'], ['*'], 'page', $data['page'] ?? 1);
  946. return Result::success($result);
  947. }
  948. }