ChatService.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978
  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. if ($result) {
  222. return Result::success("删除成功”");
  223. } else {
  224. return Result::error('删除失败');
  225. }
  226. }
  227. /**
  228. * 是否好友
  229. * @param array $data
  230. * @return array
  231. */
  232. public function isFriend(array $data): array
  233. {
  234. $where = [
  235. 'user_id' => $data['user_id'],
  236. 'friend_id' => $data['friend_id'],
  237. ];
  238. $result = ChatFriends::where($where)->first();
  239. if ($result) {
  240. return Result::success(true);
  241. } else {
  242. return Result::error('不是好友');
  243. }
  244. }
  245. /**
  246. * 添加聊天内容
  247. * @param array $data
  248. * @return array
  249. */
  250. public function addChatRecords(array $data): array
  251. {
  252. Db::beginTransaction();
  253. try {;
  254. //添加会话内容
  255. $ChatRecordsData = [[
  256. 'msg_type' => $data['msg_type'] ?? 0,
  257. 'user_id' => $data['user_id'] ?? 0,
  258. 'is_read' => $data['is_read'] ?? 0,
  259. 'talk_type' => $data['talk_type'] ?? 0,
  260. 'action' => $data['action'] ?? 0,
  261. 'group_receiver_id' => $data['group_receiver_id'] ?? 0,
  262. 'content' => $data['content'] ?? '',
  263. 'receiver_id' => $data['receiver_id'] ?? '',
  264. ]];
  265. ChatRecords::insert($ChatRecordsData);
  266. Db::commit();} catch (\Throwable $ex) {
  267. Db::rollBack();
  268. var_dump($ex->getMessage());
  269. return Result::error("存储消息失败", 0);
  270. }
  271. return Result::success([]);
  272. }
  273. /**
  274. * 修改好友备注
  275. * @param array $data
  276. * @return array
  277. */
  278. public function updateFriend(array $data): array
  279. {
  280. $result = ChatFriends::where(['user_id' => $data['user_id'],
  281. 'friend_id' => $data['friend_id'],
  282. 'status' => 2,
  283. ])->update(['remark' => $data['remark']]);
  284. if ($result) {
  285. return Result::success('修改成功');
  286. } else {
  287. return Result::error('修改失败');
  288. }
  289. }
  290. /**
  291. * 会话列表
  292. * @param array $data
  293. * @return array
  294. */
  295. public function getConversation(array $data): array
  296. {
  297. $userId = $data['user_id'];
  298. $unreadMessages = ChatRecords::where('user_id', $userId)
  299. ->where('is_read', 0)
  300. ->where('action', 'recieved')
  301. ->leftJoin('user', 'chat_records.receiver_id', '=', 'user.id')
  302. ->leftJoin('chat_groups', 'chat_records.receiver_id', '=', 'chat_groups.id')
  303. ->select(
  304. 'receiver_id',
  305. DB::raw('COUNT(receiver_id) AS num'),
  306. DB::raw('MAX(chat_records.id) AS max_id'),
  307. 'user.user_name as user_name',
  308. 'user.avatar as avatar',
  309. 'user.mobile as mobile',
  310. 'chat_groups.group_name as group_name'
  311. )
  312. ->groupBy('receiver_id')
  313. ->orderBy(DB::raw('MAX(chat_records.id)'), 'desc')
  314. ->get();
  315. // 查询已读消息,并将 num 字段设置为 0
  316. $readMessages = ChatRecords::where('user_id', $userId)
  317. ->where('is_read', 1)
  318. ->where('action', 'recieved')
  319. ->leftJoin('user', 'chat_records.receiver_id', '=', 'user.id')
  320. ->leftJoin('chat_groups', 'chat_records.receiver_id', '=', 'chat_groups.id')
  321. ->select(
  322. 'receiver_id',
  323. DB::raw('0 AS num'),
  324. DB::raw('MAX(chat_records.id) AS max_id'),
  325. 'user.user_name as user_name',
  326. 'user.avatar as avatar',
  327. 'user.mobile as mobile',
  328. 'chat_groups.group_name as group_name'
  329. )
  330. ->groupBy('receiver_id')
  331. ->orderBy(DB::raw('MAX(chat_records.id)'), 'desc')
  332. ->get();
  333. // 合并未读消息和已读消息
  334. // $allMessages = array_merge($unreadMessages->toArray(), $readMessages->toArray());
  335. // 使用关联数组去重,并优先保留未读消息
  336. $allMessages = [];
  337. foreach ($unreadMessages as $message) {
  338. $allMessages[$message['receiver_id']] = $message->toArray();
  339. }
  340. foreach ($readMessages as $message) {
  341. if (strlen($message['receiver_id']) === 18) {
  342. if (!isset($allMessages[$message['receiver_id']])) {
  343. $allMessages[$message['receiver_id']] = $message->toArray();
  344. }
  345. } else {
  346. $allMessages[$message['receiver_id']] = $message->toArray();
  347. }
  348. }
  349. // var_dump($allMessages);
  350. // 处理结果,判断是否是群聊
  351. $formattedMessages = [];
  352. foreach ($allMessages as $message) {
  353. $formattedMessage = [
  354. 'receiver_id' => $message['receiver_id'],
  355. 'num' => $message['num'],
  356. 'max_id' => $message['max_id'],
  357. 'user_name' => $message['user_name'],
  358. 'avatar' => $message['avatar'],
  359. 'mobile' => $message['mobile'],
  360. 'group_name' => $message['group_name'],
  361. ];
  362. if (strlen($message['receiver_id']) === 18) { // 判断是否是 UUID
  363. $formattedMessage['type'] = 'group';
  364. $formattedMessage['name'] = $message['group_name'];
  365. $formattedMessage['is_group'] = 1;
  366. } else {
  367. $formattedMessage['type'] = 'user';
  368. $formattedMessage['name'] = $message['user_name'];
  369. $formattedMessage['is_group'] = 0;
  370. }
  371. $formattedMessages[] = $formattedMessage;
  372. }
  373. if (!empty($formattedMessages)) {
  374. return Result::success($formattedMessages);
  375. } else {
  376. return Result::error('没有消息');
  377. }
  378. }
  379. /**
  380. * 获取聊天记录
  381. * @param array $data
  382. * @return array
  383. */
  384. public function getChatRecords(array $data): array
  385. {
  386. var_dump('222222');
  387. Db::beginTransaction();
  388. try {
  389. $userId = $data['user_id'];
  390. $friendId = $data['friend_id'];
  391. $result = ChatRecords::where(function ($query) use ($userId, $friendId) {
  392. $query->where('user_id', $userId)->where('receiver_id', $friendId);
  393. })
  394. // ->orWhere(function ($query) use ($userId, $friendId) {
  395. // $query->where('user_id', $friendId)->where('receiver_id', $userId);
  396. // })
  397. ->leftJoin('user as u1', 'chat_records.user_id', '=', 'u1.id')
  398. ->leftJoin('user as u2', 'chat_records.receiver_id', '=', 'u2.id')
  399. ->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')
  400. ->orderBy('id', 'asc')->paginate(100, ['*'], 'page', $data['page'] ?? 1);
  401. //更新聊天记录已读
  402. ChatRecords::where('user_id', $userId)
  403. ->where('receiver_id', $friendId)
  404. ->where('is_read', 0)
  405. ->where('talk_type', 1)
  406. ->update(['is_read' => 1]);
  407. Db::commit();
  408. } catch (\Throwable $ex) {
  409. Db::rollBack();
  410. var_dump($ex->getMessage());
  411. return Result::error("获取聊天记录失败", 0);
  412. }
  413. if ($result) {
  414. return Result::success($result);
  415. } else {
  416. return Result::error('没有聊天记录');
  417. }
  418. }
  419. /**
  420. * 获取群聊天记录
  421. * @param array $data
  422. * @return array
  423. */
  424. public function getGroupChatRecords(array $data): array
  425. {
  426. Db::beginTransaction();
  427. try {
  428. $userId = $data['user_id'];
  429. $group_id = $data['group_id'];
  430. $result = ChatRecords::where('receiver_id', $group_id)
  431. ->where('user_id', $userId)
  432. ->leftJoin('user as u1', 'chat_records.user_id', '=', 'u1.id')
  433. ->leftJoin('user as u2', 'chat_records.group_receiver_id', '=', 'u2.id')
  434. ->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')
  435. ->orderBy('id', 'asc')->paginate(100, ['*'], 'page', $data['page'] ?? 1);
  436. //更新群聊天记录
  437. ChatRecords::where('receiver_id', $group_id)
  438. ->where('user_id', $userId)
  439. ->update(['is_read' => 1]);
  440. Db::commit();
  441. } catch (\Throwable $ex) {
  442. Db::rollBack();
  443. var_dump($ex->getMessage());
  444. return Result::error("获取群聊天记录失败", 0);
  445. }
  446. if ($result) {
  447. return Result::success($result);
  448. } else {
  449. return Result::error('没有群消息');
  450. }
  451. }
  452. /**
  453. * 群组 - 创建群
  454. * @param array $data
  455. * @return array
  456. */
  457. public function addGroup(array $data): array
  458. {
  459. Db::beginTransaction();
  460. try {
  461. //创建群
  462. $groupData = [
  463. 'id' => PublicData::uuid(),
  464. 'creator_id' => $data['user_id'],
  465. 'group_name' => $data['group_name'],
  466. 'avatar' => $data['avatar'] ?? '',
  467. 'profile' => $data['profile'] ?? '',
  468. ];
  469. ChatGroups::insert($groupData);
  470. //创建群用户
  471. $groupMemberData = [];
  472. if ($data['group_member']) {
  473. foreach ($data['group_member'] as $key => $val) {
  474. $groupMemberData[$key] = [
  475. 'id' => PublicData::uuid(),
  476. 'group_id' => $groupData['id'],
  477. 'user_id' => $val,
  478. 'leader' => $data['user_id'] == $val ? 2 : 0,
  479. ];
  480. }
  481. }
  482. ChatGroupsMember::insert($groupMemberData);
  483. //插入一条消息
  484. $chatRecordsData = [
  485. 'user_id' => $data['user_id'],
  486. 'receiver_id' => $groupData['id'],
  487. 'content' => '我创建了一个群' . Date('Y-m-d H:i:s'),
  488. 'msg_type' => 1,
  489. 'is_read' => 0,
  490. 'talk_type' => 2,
  491. 'action' => 'said',
  492. 'group_receiver_id' => $data['user_id'],
  493. ];
  494. ChatRecords::insert($chatRecordsData);
  495. Db::commit();
  496. } catch (\Throwable $ex) {
  497. Db::rollBack();
  498. var_dump($ex->getMessage());
  499. return Result::error("创建群失败", 0);
  500. }
  501. return Result::success([]);
  502. }
  503. /**
  504. * 群组 - 加入群
  505. * @param array $data
  506. * @return array
  507. */
  508. public function addGroupMember(array $data): array
  509. {
  510. $result = ChatGroupsMember::where(['group_id' => $data['group_id'], 'user_id' => $data['user_id']])->update(['leader' => 2]);
  511. if ($result) {
  512. return Result::success('修改成功');
  513. } else {
  514. return Result::error('修改失败');
  515. }
  516. }
  517. /**
  518. * 群组 - 群信息
  519. * @param array $data
  520. * @return array
  521. */
  522. public function getGroupInfo(array $data): array
  523. {
  524. $result = ChatGroups::where(['chat_groups.id' => $data['group_id']])
  525. ->join('user', 'chat_groups.creator_id', '=', 'user.id')
  526. ->select('chat_groups.*', 'user.user_name as user_name', 'user.avatar as avatar')
  527. ->first();
  528. return Result::success($result);
  529. }
  530. /**
  531. * 群组 - 删除群
  532. * @param array $data
  533. * @return array
  534. */
  535. public function delGroup(array $data): array
  536. {
  537. Db::beginTransaction();
  538. try {
  539. $groupMember = ChatGroupsMember::where(['group_id' => $data['group_id']])->delete();
  540. $result = ChatGroups::where(['id' => $data['group_id']])->delete();
  541. $result = ChatRecords::where(['receiver_id' => $data['group_id']])->delete();
  542. //群聊记录
  543. Db::commit();
  544. } catch (\Throwable $ex) {
  545. Db::rollBack();
  546. var_dump($ex->getMessage());
  547. return Result::error("删除群失败", 0);
  548. }
  549. if ($result) {
  550. return Result::success('删除成功');
  551. } else {
  552. return Result::error('删除失败');
  553. }
  554. }
  555. /**
  556. * 群组 - 退出群
  557. * @param array $data
  558. * @return array
  559. */
  560. public function quitGroup(array $data): array
  561. {
  562. $result = ChatGroupsMember::where(['group_id' => $data['group_id'], 'user_id' => $data['user_id']])->delete();
  563. if ($result) {
  564. return Result::success('退出成功');
  565. } else {
  566. return Result::error('退出失败');
  567. }
  568. }
  569. /**
  570. * 群组 - 我的群
  571. * @param array $data
  572. * @return array
  573. */
  574. public function getGroupList(array $data): array
  575. {
  576. $result = ChatGroupsMember::where(['user_id' => $data['user_id']])
  577. ->leftJoin('chat_groups', 'chat_groups_members.group_id', '=', 'chat_groups.id')
  578. ->select('chat_groups.*', 'chat_groups_members.group_id as group_id')
  579. ->orderBy('chat_groups.id', 'desc')
  580. ->paginate(100, ['*'], 'page', $data['page'] ?? 1);
  581. return Result::success($result);
  582. }
  583. /**
  584. * 群组 - 删除群成员
  585. * @param array $data
  586. * @return array
  587. */
  588. public function delGroupMembers(array $data): array
  589. {
  590. $result = ChatGroupsMember::where(['group_id' => $data['group_id'], 'user_id' => $data['user_id']])->delete();
  591. if ($result) {
  592. return Result::success('删除成功');
  593. } else {
  594. return Result::error('删除失败');
  595. }
  596. }
  597. /**
  598. * 群组 - 更新群
  599. * @param array $data
  600. * @return array
  601. */
  602. public function updateGroup(array $data): array
  603. {
  604. // 提取需要的字段
  605. $groupId = $data['group_id'];
  606. $groupName = $data['group_name'] ?? null;
  607. $profile = $data['profile'] ?? null;
  608. $avatar = $data['avatar'] ?? null;
  609. // 查询群组
  610. $group = ChatGroups::find($groupId);
  611. if (!$group) {
  612. return Result::error('群组不存在');
  613. }
  614. // 更新群组信息
  615. if ($groupName !== null) {
  616. $group->group_name = $groupName;
  617. }
  618. if ($profile !== null) {
  619. $group->profile = $profile;
  620. }
  621. if ($avatar !== null) {
  622. $group->avatar = $avatar;
  623. }
  624. // 保存更改
  625. if ($group->save()) {
  626. return Result::success($group->toArray());
  627. } else {
  628. return Result::error('更新群组信息失败');
  629. }
  630. }
  631. /**
  632. * 群组 - 删除群
  633. * @param array $data
  634. * @return array
  635. */
  636. public function deleteGroup(array $data): array
  637. {
  638. $result = ChatGroups::where(['id' => $data['group_id']])->delete();
  639. if ($result) {
  640. return Result::success('删除成功');
  641. } else {
  642. return Result::error('删除失败');
  643. }
  644. }
  645. /**
  646. * 群组 - 群用户列表
  647. * @param array $data
  648. * @return array
  649. */
  650. public function getGroupMembers(array $data): array
  651. {
  652. $groupMember = ChatGroupsMember::where(['group_id' => $data['group_id']])
  653. ->leftJoin('user as u1', 'chat_groups_members.user_id', '=', 'u1.id')
  654. ->select('chat_groups_members.*', 'u1.user_name', 'u1.avatar')
  655. ->orderBy('id', 'desc')
  656. ->get();
  657. return Result::success($groupMember);
  658. }
  659. /**
  660. * 群组 - 添加群
  661. * @param array $data
  662. * @return array
  663. */
  664. public function joinGroup(array $data): array
  665. {
  666. $group = ChatGroups::where(['id' => $data['group_id']])->first();
  667. if (empty($group)) {
  668. return Result::error("群不存在", 0);
  669. }
  670. $groupMember = ChatGroupsMember::where(['user_id' => $data['user_id'], 'group_id' => $data['group_id']])->first();
  671. if ($groupMember) {
  672. return Result::error("已加入群", 0);
  673. }
  674. $info = [
  675. 'id' => PublicData::uuid(),
  676. 'user_id' => $data['user_id'],
  677. 'group_id' => $data['group_id'],
  678. ];
  679. $result = ChatGroupsMember::insertGetId($info);
  680. if ($result) {
  681. return Result::success($data);
  682. } else {
  683. return Result::error($data);
  684. };
  685. }
  686. /**
  687. * 话题 - 列表
  688. * @param array $data
  689. * @return array
  690. */
  691. public function getTopicsList(array $data): array
  692. {
  693. $where = [];
  694. if (!empty($data['title'])) {
  695. $where[] = ['chat_topics.title', 'like', '%' . $data['title'] . '%'];
  696. }
  697. if (!empty($data['user_id'])) {
  698. $where[] = ['chat_topics.user_id', '=', $data['user_id']];
  699. }
  700. if (!empty($data['status'])) {
  701. $where[] = ['chat_topics.status', '=', $data['status']];
  702. }
  703. if (!empty($data['type'])) {
  704. $where[] = ['chat_topics.type', '=', $data['type']];
  705. }
  706. if (!empty($data['nickname'])) {
  707. $where[] = ['user.nickname', '=', $data['nickname']];
  708. }
  709. var_dump($where);
  710. $result = ChatTopic::where($where)
  711. ->leftJoin('user', 'user.id', '=', 'chat_topics.user_id')
  712. ->leftJoin('chat_topics_reply', 'chat_topics.id', '=', 'chat_topics_reply.topic_id')
  713. ->select('chat_topics.*', 'user.nickname', 'user.avatar', 'user.user_name'
  714. ,
  715. DB::raw('count(chat_topics_reply.id) as num'))
  716. ->groupBy('chat_topics.id')
  717. ->orderBy('chat_topics.id', 'desc')
  718. ->paginate($data['page_size'], ['*'], 'page', $data['page'] ?? 1);
  719. return Result::success($result);
  720. }
  721. public function getTopic(array $data): array
  722. {
  723. $result = ChatTopic::where(['id' => $data['id']])->first();
  724. return Result::success($result);
  725. }
  726. public function addTopic(array $data): array
  727. {
  728. $chattopic = [];
  729. try {
  730. $data['created_at'] = date('Y-m-d H:i:s');
  731. $data['updated_at'] = date('Y-m-d H:i:s');
  732. $result = ChatTopic::insertGetId($data);
  733. if ($result && $data['is_group'] == 1) {
  734. //chat_group
  735. $group_id = PublicData::uuid();
  736. $groupData = [
  737. 'id' => $group_id,
  738. 'creator_id' => $data['user_id'],
  739. 'group_name' => $data['group_name'] ?? '',
  740. 'profile' => $data['profile'] ?? 0,
  741. ];
  742. $groupResult = ChatGroups::insertGetId($groupData);
  743. $groupMemberData = [
  744. 'id' => PublicData::uuid(),
  745. 'user_id' => $data['user_id'],
  746. 'group_id' => $group_id,
  747. 'leader' => 2,
  748. ];
  749. $groupMemberResult = ChatGroupsMember::insertGetId($groupMemberData);
  750. //更新result的 group_id
  751. $data['group_id'] = $group_id;
  752. ChatTopic::where(['id' => $result])->update($data);
  753. //插入一条消息
  754. $chatRecordsData = [
  755. 'user_id' => $data['user_id'],
  756. 'receiver_id' => $group_id,
  757. 'content' => '我创建了一个群' . Date('Y-m-d H:i:s'),
  758. 'msg_type' => 1,
  759. 'is_read' => 0,
  760. 'talk_type' => 2,
  761. 'action' => 'said',
  762. 'group_receiver_id' => $data['user_id'],
  763. ];
  764. ChatRecords::insert($chatRecordsData);
  765. // 查询 Chattopic 数据
  766. $chattopic = Chattopic::find($result);
  767. } else {
  768. $chattopic = Chattopic::find($result);
  769. }
  770. Db::beginTransaction();
  771. Db::commit();
  772. } catch (\Exception $e) {
  773. Db::rollBack();
  774. return Result::error($data, $e->getMessage());
  775. }
  776. return Result::success($chattopic);
  777. }
  778. public function updateTopic(array $data): array
  779. {
  780. $result = ChatTopic::where(['id' => $data['id']])->update($data);
  781. if ($result) {
  782. return Result::success($data);
  783. } else {
  784. return Result::error($data);
  785. };
  786. }
  787. public function delTopic(array $data): array
  788. {
  789. $result = ChatTopic::where(['id' => $data['id']])->delete();
  790. //删除群和成员和聊天
  791. //删除话题回复
  792. if ($result) {
  793. return Result::success($data);
  794. } else {
  795. return Result::error('删除失败');
  796. };
  797. }
  798. public function getTopicInfo(array $data): array
  799. {
  800. $result = ChatTopic::where(['chat_topics.id' => $data['id']])
  801. ->leftJoin('user', 'user.id', '=', 'chat_topics.user_id')
  802. ->select('chat_topics.*', 'user.nickname', 'user.avatar', 'user.user_name')
  803. ->first();
  804. return Result::success($result);
  805. }
  806. public function addReply(array $data): array
  807. {
  808. $result = ChatTopic::where(['id' => $data['id']])->get();
  809. if ($result) {
  810. $replydata['created_at'] = date('Y-m-d H:i:s');
  811. $replydata['updated_at'] = date('Y-m-d H:i:s');
  812. $replydata['content'] = $data['content'];
  813. $replydata['user_id'] = $data['user_id'];
  814. $replydata['topic_id'] = $data['id'];
  815. $re = ChatTopicsReply::insertGetId($replydata);
  816. }
  817. if ($re) {
  818. return Result::success($data);
  819. } else {
  820. return Result::error($data);
  821. }
  822. }
  823. public function getTopicReply(array $data): array
  824. {
  825. var_dump($data);
  826. $result = ChatTopicsReply::where(['topic_id' => $data['id']])
  827. ->leftJoin('user', 'user.id', '=', 'chat_topics_reply.user_id')
  828. ->select('chat_topics_reply.*', 'user.nickname', 'user.avatar', 'user.user_name')
  829. ->paginate($data['page_size'], ['*'], 'page', $data['page'] ?? 1);
  830. return Result::success($result);
  831. }
  832. /**
  833. * 修改群成员
  834. * @param array $data
  835. * @return array
  836. */
  837. public function updateGroupMembers(array $data): array
  838. {
  839. $where = [
  840. 'group_id' => $data['group_id'],
  841. ];
  842. $group_id = $data['group_id'];
  843. //先删除群成员
  844. $result = ChatGroupsMember::where($where)
  845. ->where([["user_id", '!=', $data['user_id']]])->delete();
  846. $groupMemberData = [];
  847. foreach ($data['group_member'] as $value) {
  848. $groupMemberData[] = [
  849. 'id' => PublicData::uuid(),
  850. 'user_id' => $value,
  851. 'group_id' => $group_id,
  852. 'leader' => 0,
  853. ];
  854. }
  855. $result = ChatGroupsMember::where($where)->insert($groupMemberData);
  856. // 获取群信息
  857. $groupInfo = ChatGroups::where(['id' => $group_id])->first();
  858. if ($result) {
  859. return Result::success($groupInfo);
  860. } else {
  861. return Result::error($data);
  862. }
  863. }
  864. public function clearGroupRecords(array $data): array
  865. {
  866. $result = ChatRecords::where(['user_id' => $data['user_id'], 'receiver_id' => $data['id']])->delete();
  867. if ($result) {
  868. return Result::success("删除成功");
  869. } else {
  870. return Result::error("删除失败");
  871. }
  872. }
  873. public function recallRecord(array $data): array
  874. {
  875. //获取所有id,并删除掉
  876. $ids = array_column($data, 'id');
  877. $result = ChatRecords::whereIn('id', $ids)->delete();
  878. if ($result) {
  879. return Result::success("删除成功");
  880. } else {
  881. return Result::error("删除失败");
  882. }
  883. }
  884. public function clearRecords(array $data): array
  885. {
  886. $result = ChatRecords::where(['user_id' => $data['user_id'], 'receiver_id' => $data['friend_id']])->delete();
  887. if ($result) {
  888. return Result::success("删除成功");
  889. } else {
  890. return Result::error("删除失败");
  891. }
  892. }
  893. public function getRecordByContent(array $data): array
  894. {
  895. $result = ChatRecords::where(['chat_records.user_id' => $data['user_id'], 'chat_records.receiver_id' => $data['receiver_id'], 'chat_records.content' => $data['content']])
  896. ->orWhere(['chat_records.receiver_id' => $data['user_id'], 'chat_records.user_id' => $data['receiver_id'], 'chat_records.content' => $data['content']])
  897. ->all();
  898. if ($result) {
  899. return Result::success($result['id']);
  900. } else {
  901. return Result::error("没有数据");
  902. }
  903. }
  904. public function getRecord(array $data): array
  905. {
  906. $result = ChatRecords::where(['chat_records.id' => $data['id']])
  907. ->leftJoin('user', 'user.id', '=', 'chat_records.user_id')
  908. ->leftJoin('user as user2', 'user2.id', '=', 'chat_records.receiver_id')
  909. ->select('chat_records.*', 'user.nickname', 'user.avatar', 'user.user_name', 'user2.nickname as receiver_nickname', 'user2.avatar as receiver_avatar')
  910. ->get();
  911. return Result::success($result);
  912. }
  913. public function delReply(array $data): array
  914. {
  915. $result = ChatTopicsReply::where(['id' => $data['id']])->delete();
  916. if ($result) {
  917. return Result::success("删除成功");
  918. } else {
  919. return Result::error("删除失败");
  920. }
  921. }
  922. public function delAllReply(array $data): array
  923. {
  924. $result = ChatTopicsReply::where(['topic_id' => $data['topicid']])->delete();
  925. if ($result) {
  926. return Result::success("删除成功");
  927. } else {
  928. return Result::error("删除失败");
  929. }
  930. }
  931. public function getTopicsListAdmin(array $data): array
  932. {
  933. $where = [];
  934. if (!empty($data['type'])) {
  935. $where['type'] = $data['type'];
  936. }
  937. if (!empty($data['title'])) {
  938. $where['title'] = $data['title'];
  939. }
  940. $result = ChatTopic::where($where)
  941. ->leftJoin('user', 'user.id', '=', 'chat_topics.user_id')
  942. ->select('chat_topics.*', 'user.nickname', 'user.avatar', 'user.user_name')
  943. ->paginate($data['page_size'], ['*'], 'page', $data['page'] ?? 1);
  944. return Result::success($result);
  945. }
  946. }