ChatService.php 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308
  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\ChatTopicClass;
  10. use App\Model\User;
  11. use App\Tools\PublicData;
  12. use App\Tools\Result;
  13. use Hyperf\DbConnection\Db;
  14. use Hyperf\RpcServer\Annotation\RpcService;
  15. use Hyperf\Context\ApplicationContext as ContextApplicationContext;
  16. use Hyperf\Amqp\Producer;
  17. use App\Amqp\Producer\MqProducer;
  18. #[RpcService(name: "ChatService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
  19. class ChatService implements ChatServiceInterface
  20. {
  21. /**
  22. * 获取用户信息
  23. * @param array $data
  24. * @return array
  25. */
  26. public function getFriendInfo(array $data): array
  27. {
  28. $result = ChatFriends::where('friend_id', $data['friend_id'])
  29. ->where('user_id', $data['user_id'])
  30. ->select(
  31. 'chat_friends.*',
  32. 'user.user_name',
  33. 'user.mobile',
  34. 'user.nickname',
  35. 'user.avatar'
  36. )
  37. ->leftJoin('user', 'user.id', '=', 'chat_friends.friend_id')
  38. ->first();
  39. return Result::success($result);
  40. }
  41. /**
  42. * 搜索好友
  43. * @param array $data
  44. * @return array
  45. */
  46. public function searchFriend(array $data): array
  47. {
  48. $keyword = $data['keyword'];
  49. $userId = $data['user_id'];
  50. $result = User::leftJoin('chat_friends', function ($join) use ($userId) {
  51. $join->on('user.id', '=', 'chat_friends.friend_id')
  52. ->where('chat_friends.user_id', '=', $userId);
  53. })
  54. ->where(function ($query) use ($data) {
  55. $query->where('user.user_name', 'like', '%' . $data['keyword'] . '%')
  56. ->orWhere('user.nickname', 'like', '%' . $data['keyword'] . '%')
  57. ->orWhere('user.mobile', 'like', '%' . $data['keyword'] . '%');
  58. })
  59. ->where('user.id', '<>', $userId)
  60. ->where('user.type_id', '<>', '20000')
  61. ->select('user.*', 'chat_friends.remark as remark', 'chat_friends.id as isfriend')
  62. // ->select('user.*', 'chat_friends.friend_id')
  63. ->get();
  64. return Result::success($result);
  65. if ($result) {
  66. return Result::success($result);
  67. } else {
  68. return Result::error('没有找到相关好友');
  69. }
  70. }
  71. /**
  72. * 添加申请
  73. * @param array $data
  74. * @return array
  75. */
  76. public function addFriend(array $data): array
  77. {
  78. Db::beginTransaction();
  79. try {
  80. // 检查是否存在相同的记录
  81. $existingRecord = ChatFriends::where([
  82. 'user_id' => $data['user_id'],
  83. 'friend_id' => $data['friend_id'],
  84. ])->first();
  85. if ($existingRecord) {
  86. Db::rollBack();
  87. if ($existingRecord->status == 1) {
  88. return Result::error("好友申请已存在", 0);
  89. } elseif ($existingRecord->status == 2) {
  90. return Result::error("已经是好友关系", 0);
  91. }
  92. }
  93. $result = ChatFriends::insertGetId($data);
  94. Db::commit();
  95. } catch (\Throwable $ex) {
  96. Db::rollBack();
  97. var_dump($ex->getMessage());
  98. return Result::error("添加好友申请失败", 0);
  99. }
  100. return Result::success("添加成功");
  101. }
  102. /**
  103. * 好友列表
  104. * @param array $data
  105. * @return array
  106. */
  107. public function getFriendsList(array $data): array
  108. {
  109. var_dump($data);
  110. $result = ChatFriends::leftJoin('user', 'user.id', '=', 'chat_friends.friend_id')
  111. ->select(
  112. 'chat_friends.*',
  113. 'user.user_name',
  114. 'user.mobile',
  115. 'user.nickname',
  116. 'user.avatar'
  117. )
  118. ->where([
  119. ['user_id', $data['user_id']],
  120. ['chat_friends.status', $data['status']], // 明确指定 chat_friends 表中的 status 列
  121. ])
  122. ->get();
  123. return Result::success($result);
  124. }
  125. /**
  126. * 好友列表
  127. * @param array $data
  128. * @return array
  129. */
  130. public function getFriendsApplyList(array $data): array
  131. {
  132. var_dump($data);
  133. $result = ChatFriends::leftJoin('user', 'user.id', '=', 'chat_friends.user_id')
  134. ->select(
  135. 'chat_friends.*',
  136. 'user.user_name',
  137. 'user.mobile',
  138. 'user.nickname',
  139. 'user.avatar'
  140. )
  141. ->where([
  142. ['friend_id', $data['friend_id']],
  143. ['chat_friends.status', $data['status']], // 明确指定 chat_friends 表中的 status 列
  144. ])
  145. ->get();
  146. return Result::success($result);
  147. }
  148. /**
  149. * 更新申请
  150. * @param array $data
  151. * @return array
  152. */
  153. public function applyFriend(array $data): array
  154. {
  155. $status = $data['status'];
  156. //判断同意还是不同意
  157. if ($status == 2) {
  158. Db::beginTransaction();
  159. try {
  160. $where = [
  161. 'id' => $data['id'],
  162. 'status' => 1,
  163. ];
  164. $fr = ChatFriends::where($where)->first();
  165. if (empty($fr)) {
  166. Db::rollBack();
  167. return Result::error("好友申请记录不存在,已经是好友了", 0);
  168. }
  169. $data['status'] = $status;
  170. $data['applied_at'] = date("Y-m-d H:i:s"); //好友审核时间操作人friend_id
  171. unset($data['user_id']);
  172. ChatFriends::where($where)->update($data);
  173. Db::table('chat_friends')
  174. ->updateOrInsert(
  175. [
  176. 'user_id' => $fr['friend_id'],
  177. 'friend_id' => $fr['user_id'],
  178. ],
  179. [
  180. 'status' => $status,
  181. ]
  182. );
  183. // 给对方发送通知
  184. $friendId = $fr['user_id'];
  185. $userId = $fr['friend_id'];
  186. $content = "你们已经成为好友了" . date("Y-m-d H:i:s");
  187. $chatRecordsData = [[
  188. 'user_id' => $userId,
  189. 'receiver_id' => $friendId,
  190. 'content' => $content,
  191. 'msg_type' => 1,
  192. 'is_read' => 1,
  193. 'talk_type' => 1,
  194. 'action' => 'said',
  195. ], [
  196. 'user_id' => $friendId,
  197. 'receiver_id' => $userId,
  198. 'content' => $content,
  199. 'msg_type' => 1,
  200. 'is_read' => 1,
  201. 'talk_type' => 1,
  202. 'action' => 'recieved',
  203. ]];
  204. ChatRecords::insert($chatRecordsData);
  205. Db::commit();
  206. } catch (\Throwable $ex) {
  207. Db::rollBack();
  208. var_dump($ex->getMessage());
  209. return Result::error("同意添加为好友失败", 0);
  210. }
  211. return Result::success(['添加成功']);
  212. } else if ($status == 4) { //拒绝
  213. Db::beginTransaction();
  214. try {
  215. $where1 = [
  216. 'id' => $data['id'],
  217. ];
  218. $deletedRows = ChatFriends::where($where1)->delete();
  219. if ($deletedRows > 0) {
  220. Db::commit();
  221. } else {
  222. // 处理记录不存在的情况
  223. Db::rollBack();
  224. throw new \Exception('记录不存在');
  225. }
  226. } catch (\Throwable $ex) {
  227. Db::rollBack();
  228. var_dump($ex->getMessage());
  229. return Result::error("拒绝添加好友失败", 0);
  230. }
  231. return Result::success(['已经拒绝']);
  232. }
  233. }
  234. /**
  235. * 删除好友
  236. * @param array $data
  237. * @return array
  238. */
  239. public function delFriend(array $data): array
  240. {
  241. Db::beginTransaction();
  242. try {
  243. $where = [
  244. 'user_id' => $data['user_id'],
  245. 'friend_id' => $data['friend_id'],
  246. ];
  247. $orwhere = [
  248. 'user_id' => $data['friend_id'],
  249. 'friend_id' => $data['user_id'],
  250. ];
  251. // 使用闭包来确保正确的 OR 关系
  252. $result = ChatFriends::where(function ($query) use ($where) {
  253. $query->where($where);
  254. })
  255. ->orWhere(function ($query) use ($orwhere) {
  256. $query->where($orwhere);
  257. })
  258. ->delete();
  259. var_dump($result, '-0------------------');
  260. $wherechat = [
  261. 'user_id' => $data['user_id'],
  262. 'receiver_id' => $data['friend_id'],
  263. ];
  264. $orwherechat = [
  265. 'user_id' => $data['friend_id'],
  266. 'receiver_id' => $data['user_id'],
  267. ];
  268. // 使用闭包来确保正确的 OR 关系
  269. ChatRecords::where(function ($query) use ($wherechat) {
  270. $query->where($wherechat);
  271. })
  272. ->orWhere(function ($query) use ($orwherechat) {
  273. $query->where($orwherechat);
  274. })
  275. ->delete();
  276. Db::commit();
  277. return Result::success("删除成功");
  278. } catch (\Exception $e) {
  279. Db::rollback();
  280. return Result::error($e->getMessage());
  281. }
  282. }
  283. /**
  284. * 是否好友
  285. * @param array $data
  286. * @return array
  287. */
  288. public function isFriend(array $data): array
  289. {
  290. $where = [
  291. 'user_id' => $data['user_id'],
  292. 'friend_id' => $data['friend_id'],
  293. ];
  294. $result = ChatFriends::where($where)->first();
  295. if ($result) {
  296. return Result::success(true);
  297. } else {
  298. return Result::error('不是好友');
  299. }
  300. }
  301. /**
  302. * 添加聊天内容
  303. * @param array $data
  304. * @return array
  305. */
  306. public function addChatRecords(array $data): array
  307. {
  308. Db::beginTransaction();
  309. try {
  310. //添加会话内容
  311. $ChatRecordsData = [[
  312. 'msg_type' => $data['msg_type'] ?? 0,
  313. 'user_id' => $data['user_id'] ?? 0,
  314. 'is_read' => $data['is_read'] ?? 0,
  315. 'talk_type' => $data['talk_type'] ?? 0,
  316. 'action' => $data['action'] ?? 0,
  317. 'group_receiver_id' => $data['group_receiver_id'] ?? 0,
  318. 'content' => $data['content'] ?? '',
  319. 'receiver_id' => $data['receiver_id'] ?? '',
  320. ]];
  321. ChatRecords::insert($ChatRecordsData);
  322. Db::commit();
  323. } catch (\Throwable $ex) {
  324. Db::rollBack();
  325. var_dump($ex->getMessage());
  326. return Result::error("存储消息失败", 0);
  327. }
  328. return Result::success([]);
  329. }
  330. /**
  331. * 修改好友备注
  332. * @param array $data
  333. * @return array
  334. */
  335. public function updateFriend(array $data): array
  336. {
  337. $result = ChatFriends::where([
  338. 'user_id' => $data['user_id'],
  339. 'friend_id' => $data['friend_id'],
  340. 'status' => 2,
  341. ])->update(['remark' => $data['remark']]);
  342. if ($result) {
  343. return Result::success('修改成功');
  344. } else {
  345. return Result::error('修改失败');
  346. }
  347. }
  348. /**
  349. * 会话列表
  350. * @param array $data
  351. * @return array
  352. */
  353. public function getConversation(array $data): array
  354. {
  355. $userId = $data['user_id'];
  356. $unreadMessages = ChatRecords::where('user_id', $userId)
  357. ->where('is_read', 0)
  358. // ->where('action', 'recieved')
  359. ->leftJoin('user', 'chat_records.receiver_id', '=', 'user.id')
  360. ->leftJoin('chat_groups', 'chat_records.receiver_id', '=', 'chat_groups.id')
  361. ->select(
  362. 'receiver_id',
  363. DB::raw('COUNT(receiver_id) AS num'),
  364. DB::raw('MAX(chat_records.id) AS max_id'),
  365. 'user.user_name as user_name',
  366. 'user.avatar as avatar',
  367. 'user.mobile as mobile',
  368. 'chat_groups.group_name as group_name'
  369. )
  370. ->groupBy('receiver_id')
  371. ->orderBy(DB::raw('MAX(chat_records.id)'), 'desc')
  372. ->get();
  373. // 查询已读消息,并将 num 字段设置为 0
  374. $readMessages = ChatRecords::where('user_id', $userId)
  375. ->where('is_read', 1)
  376. // ->where('action', 'recieved')
  377. ->leftJoin('user', 'chat_records.receiver_id', '=', 'user.id')
  378. ->leftJoin('chat_groups', 'chat_records.receiver_id', '=', 'chat_groups.id')
  379. ->select(
  380. 'receiver_id',
  381. DB::raw('0 AS num'),
  382. DB::raw('MAX(chat_records.id) AS max_id'),
  383. 'user.user_name as user_name',
  384. 'user.avatar as avatar',
  385. 'user.mobile as mobile',
  386. 'chat_groups.group_name as group_name'
  387. )
  388. ->groupBy('receiver_id')
  389. ->orderBy(DB::raw('MAX(chat_records.id)'), 'desc')
  390. ->get();
  391. // 合并未读消息和已读消息
  392. // $allMessages = array_merge($unreadMessages->toArray(), $readMessages->toArray());
  393. // 使用关联数组去重,并优先保留未读消息
  394. $allMessages = [];
  395. foreach ($unreadMessages as $message) {
  396. $allMessages[$message['receiver_id']] = $message->toArray();
  397. }
  398. foreach ($readMessages as $message) {
  399. if (strlen($message['receiver_id']) === 18) {
  400. } else {
  401. // $allMessages[$message['receiver_id']] = $message->toArray();
  402. }
  403. if (!isset($allMessages[$message['receiver_id']])) {
  404. $allMessages[$message['receiver_id']] = $message->toArray();
  405. }
  406. }
  407. // var_dump($allMessages);
  408. // 处理结果,判断是否是群聊
  409. $formattedMessages = [];
  410. foreach ($allMessages as $message) {
  411. $formattedMessage = [
  412. 'receiver_id' => $message['receiver_id'],
  413. 'num' => $message['num'],
  414. 'max_id' => $message['max_id'],
  415. 'user_name' => $message['user_name'],
  416. 'avatar' => $message['avatar'],
  417. 'mobile' => $message['mobile'],
  418. 'group_name' => $message['group_name'],
  419. ];
  420. if (strlen($message['receiver_id']) === 18) { // 判断是否是 UUID
  421. $formattedMessage['type'] = 'group';
  422. $formattedMessage['name'] = $message['group_name'];
  423. $formattedMessage['is_group'] = 1;
  424. } else {
  425. $formattedMessage['type'] = 'user';
  426. $formattedMessage['name'] = $message['user_name'];
  427. $formattedMessage['is_group'] = 0;
  428. }
  429. $formattedMessages[] = $formattedMessage;
  430. }
  431. if (!empty($formattedMessages)) {
  432. return Result::success($formattedMessages);
  433. } else {
  434. return Result::error('没有消息');
  435. }
  436. }
  437. /**
  438. * 获取聊天记录
  439. * @param array $data
  440. * @return array
  441. */
  442. public function getChatRecords(array $data): array
  443. {
  444. var_dump('222222');
  445. Db::beginTransaction();
  446. try {
  447. $userId = $data['user_id'];
  448. $friendId = $data['friend_id'];
  449. $result = ChatRecords::where(function ($query) use ($userId, $friendId) {
  450. $query->where('user_id', $userId)->where('receiver_id', $friendId);
  451. })
  452. // ->orWhere(function ($query) use ($userId, $friendId) {
  453. // $query->where('user_id', $friendId)->where('receiver_id', $userId);
  454. // })
  455. ->leftJoin('user as u1', 'chat_records.user_id', '=', 'u1.id')
  456. ->leftJoin('user as u2', 'chat_records.receiver_id', '=', 'u2.id')
  457. ->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')
  458. ->orderBy('id', 'asc')->paginate(100, ['*'], 'page', $data['page'] ?? 1);
  459. //更新聊天记录已读
  460. ChatRecords::where('user_id', $userId)
  461. ->where('receiver_id', $friendId)
  462. ->where('is_read', 0)
  463. ->where('talk_type', 1)
  464. ->update(['is_read' => 1]);
  465. Db::commit();
  466. } catch (\Throwable $ex) {
  467. Db::rollBack();
  468. var_dump($ex->getMessage());
  469. return Result::error("获取聊天记录失败", 0);
  470. }
  471. if ($result) {
  472. return Result::success($result);
  473. } else {
  474. return Result::error('没有聊天记录');
  475. }
  476. }
  477. /**
  478. * 获取群聊天记录
  479. * @param array $data
  480. * @return array
  481. */
  482. public function getGroupChatRecords(array $data): array
  483. {
  484. Db::beginTransaction();
  485. try {
  486. $userId = $data['user_id'];
  487. $group_id = $data['group_id'];
  488. $result = ChatRecords::where('receiver_id', $group_id)
  489. ->where('user_id', $userId)
  490. ->leftJoin('user as u1', 'chat_records.user_id', '=', 'u1.id')
  491. ->leftJoin('user as u2', 'chat_records.group_receiver_id', '=', 'u2.id')
  492. ->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')
  493. ->orderBy('id', 'asc')->paginate(100, ['*'], 'page', $data['page'] ?? 1);
  494. //更新群聊天记录
  495. ChatRecords::where('receiver_id', $group_id)
  496. ->where('user_id', $userId)
  497. ->update(['is_read' => 1]);
  498. Db::commit();
  499. } catch (\Throwable $ex) {
  500. Db::rollBack();
  501. var_dump($ex->getMessage());
  502. return Result::error("获取群聊天记录失败", 0);
  503. }
  504. if ($result) {
  505. return Result::success($result);
  506. } else {
  507. return Result::error('没有群消息');
  508. }
  509. }
  510. /**
  511. * 群组 - 创建群
  512. * @param array $data
  513. * @return array
  514. */
  515. public function addGroup(array $data): array
  516. {
  517. Db::beginTransaction();
  518. try {
  519. //创建群
  520. $groupData = [
  521. 'id' => PublicData::uuid(),
  522. 'creator_id' => $data['user_id'],
  523. 'group_name' => $data['group_name'],
  524. 'avatar' => $data['avatar'] ?? '',
  525. 'profile' => $data['profile'] ?? '',
  526. ];
  527. ChatGroups::insert($groupData);
  528. //创建群用户
  529. $groupMemberData = [];
  530. $groupChatData = [];
  531. if ($data['group_member']) {
  532. foreach ($data['group_member'] as $key => $val) {
  533. $groupMemberData[$key] = [
  534. 'id' => PublicData::uuid(),
  535. 'group_id' => $groupData['id'],
  536. 'user_id' => $val,
  537. 'leader' => $data['user_id'] == $val ? 2 : 0,
  538. ];
  539. $groupChatData[$key] = [
  540. 'user_id' => $val,
  541. 'receiver_id' => $groupData['id'],
  542. 'content' => '创建群' . Date('Y-m-d H:i:s'),
  543. 'msg_type' => 1,
  544. 'is_read' => 0,
  545. 'talk_type' => 2,
  546. 'action' => 'recieved',
  547. 'group_receiver_id' => $data['user_id'],
  548. ];
  549. }
  550. }
  551. ChatGroupsMember::insert($groupMemberData);
  552. //插入一条消息
  553. ChatRecords::insert($groupChatData);
  554. Db::commit();
  555. } catch (\Throwable $ex) {
  556. Db::rollBack();
  557. var_dump($ex->getMessage());
  558. return Result::error("创建群失败", 0);
  559. }
  560. return Result::success([]);
  561. }
  562. /**
  563. * 群组 - 加入群
  564. * @param array $data
  565. * @return array
  566. */
  567. public function addGroupMember(array $data): array
  568. {
  569. $result = ChatGroupsMember::where(['group_id' => $data['group_id'], 'user_id' => $data['user_id']])->update(['leader' => 2]);
  570. $groupChatData = [
  571. 'user_id' => $data['user_id'],
  572. 'receiver_id' => $data['group_id'],
  573. 'content' => '加入群' . Date('Y-m-d H:i:s'),
  574. 'msg_type' => 1,
  575. 'is_read' => 0,
  576. 'talk_type' => 2,
  577. 'action' => 'recieved',
  578. 'group_receiver_id' => $data['user_id'],
  579. ];
  580. ChatRecords::insert($groupChatData);
  581. if ($result) {
  582. return Result::success('修改成功');
  583. } else {
  584. return Result::error('修改失败');
  585. }
  586. }
  587. /**
  588. * 群组 - 群信息
  589. * @param array $data
  590. * @return array
  591. */
  592. public function getGroupInfo(array $data): array
  593. {
  594. $result = ChatGroups::where(['chat_groups.id' => $data['group_id']])
  595. ->join('user', 'chat_groups.creator_id', '=', 'user.id')
  596. ->select('chat_groups.*', 'user.user_name as user_name', 'user.avatar as avatar')
  597. ->first();
  598. return Result::success($result);
  599. }
  600. /**
  601. * 群组 - 删除群
  602. * @param array $data
  603. * @return array
  604. */
  605. public function delGroup(array $data): array
  606. {
  607. Db::beginTransaction();
  608. try {
  609. $groupMember = ChatGroupsMember::where(['group_id' => $data['group_id']])->delete();
  610. $result = ChatGroups::where(['id' => $data['group_id']])->delete();
  611. $result = ChatRecords::where(['receiver_id' => $data['group_id']])->delete();
  612. //群聊记录
  613. Db::commit();
  614. } catch (\Throwable $ex) {
  615. Db::rollBack();
  616. var_dump($ex->getMessage());
  617. return Result::error("删除群失败", 0);
  618. }
  619. if ($result) {
  620. return Result::success('删除成功');
  621. } else {
  622. return Result::error('删除失败');
  623. }
  624. }
  625. /**
  626. * 群组 - 退出群
  627. * @param array $data
  628. * @return array
  629. */
  630. public function quitGroup(array $data): array
  631. {
  632. $result = ChatGroupsMember::where(['group_id' => $data['group_id'], 'user_id' => $data['user_id']])->delete();
  633. ChatRecords::where(['receiver_id' => $data['group_id'], 'user_id' => $data['user_id']])->delete();
  634. if ($result) {
  635. return Result::success('退出成功');
  636. } else {
  637. return Result::error('退出失败');
  638. }
  639. }
  640. /**
  641. * 群组 - 我的群
  642. * @param array $data
  643. * @return array
  644. */
  645. public function getGroupList(array $data): array
  646. {
  647. $result = ChatGroupsMember::where(['user_id' => $data['user_id']])
  648. ->leftJoin('chat_groups', 'chat_groups_members.group_id', '=', 'chat_groups.id')
  649. ->select('chat_groups.*', 'chat_groups_members.group_id as group_id')
  650. ->orderBy('chat_groups.id', 'desc')
  651. ->paginate(100, ['*'], 'page', $data['page'] ?? 1);
  652. return Result::success($result);
  653. }
  654. /**
  655. * 群组 - 删除群成员
  656. * @param array $data
  657. * @return array
  658. */
  659. public function delGroupMembers(array $data): array
  660. {
  661. $result = ChatGroupsMember::where(['group_id' => $data['group_id'], 'user_id' => $data['user_id']])->delete();
  662. ChatRecords::where(['receiver_id' => $data['group_id'], 'user_id' => $data['user_id']])->delete();
  663. if ($result) {
  664. return Result::success('删除成功');
  665. } else {
  666. return Result::error('删除失败');
  667. }
  668. }
  669. /**
  670. * 群组 - 更新群
  671. * @param array $data
  672. * @return array
  673. */
  674. public function updateGroup(array $data): array
  675. {
  676. // 提取需要的字段
  677. $groupId = $data['group_id'];
  678. $groupName = $data['group_name'] ?? null;
  679. $profile = $data['profile'] ?? null;
  680. $avatar = $data['avatar'] ?? null;
  681. // 查询群组
  682. $group = ChatGroups::find($groupId);
  683. if (!$group) {
  684. return Result::error('群组不存在');
  685. }
  686. // 更新群组信息
  687. if ($groupName !== null) {
  688. $group->group_name = $groupName;
  689. }
  690. if ($profile !== null) {
  691. $group->profile = $profile;
  692. }
  693. if ($avatar !== null) {
  694. $group->avatar = $avatar;
  695. }
  696. // 保存更改
  697. if ($group->save()) {
  698. return Result::success($group->toArray());
  699. } else {
  700. return Result::error('更新群组信息失败');
  701. }
  702. }
  703. /**
  704. * 群组 - 删除群
  705. * @param array $data
  706. * @return array
  707. */
  708. public function deleteGroup(array $data): array
  709. {
  710. $result = ChatGroups::where(['id' => $data['group_id']])->delete();
  711. if ($result) {
  712. return Result::success('删除成功');
  713. } else {
  714. return Result::error('删除失败');
  715. }
  716. }
  717. /**
  718. * 群组 - 群用户列表
  719. * @param array $data
  720. * @return array
  721. */
  722. public function getGroupMembers(array $data): array
  723. {
  724. $groupMember = ChatGroupsMember::where(['group_id' => $data['group_id']])
  725. ->leftJoin('user as u1', 'chat_groups_members.user_id', '=', 'u1.id')
  726. ->leftJoin('chat_groups as c1', 'c1.id', '=', 'chat_groups_members.group_id')
  727. ->select('chat_groups_members.*', 'u1.user_name', 'u1.avatar', 'c1.group_name as group_name')
  728. ->orderBy('id', 'desc')
  729. ->get();
  730. return Result::success($groupMember);
  731. }
  732. /**
  733. * 群组 - 添加群
  734. * @param array $data
  735. * @return array
  736. */
  737. public function joinGroup(array $data): array
  738. {
  739. $group = ChatGroups::where(['id' => $data['group_id']])->first();
  740. if (empty($group)) {
  741. return Result::error("群不存在", 0);
  742. }
  743. $groupMember = ChatGroupsMember::where(['user_id' => $data['user_id'], 'group_id' => $data['group_id']])->first();
  744. if ($groupMember) {
  745. return Result::error("已加入群", 0);
  746. }
  747. $info = [
  748. 'id' => PublicData::uuid(),
  749. 'user_id' => $data['user_id'],
  750. 'group_id' => $data['group_id'],
  751. ];
  752. $result = ChatGroupsMember::insert($info);
  753. var_dump($result, '--------------------');
  754. if ($result) {
  755. return Result::success($data);
  756. } else {
  757. return Result::error($data);
  758. };
  759. }
  760. /**
  761. * 话题 - 列表
  762. * @param array $data
  763. * @return array
  764. */
  765. public function getTopicsList(array $data): array
  766. {
  767. $where = [];
  768. if (!empty($data['title'])) {
  769. $where[] = ['chat_topics.title', 'like', '%' . $data['title'] . '%'];
  770. }
  771. // 不是看自己的话题
  772. if (!empty($data['user_id_search'])) {
  773. $where[] = ['chat_topics.user_id', '=', $data['user_id_search']];
  774. }
  775. if (!empty($data['status'])) {
  776. $where[] = ['chat_topics.status', '=', $data['status']];
  777. }
  778. if (!empty($data['type'])) {
  779. $where[] = ['chat_topics.type', '=', $data['type']];
  780. }
  781. if (!empty($data['nickname'])) {
  782. $where[] = ['user.nickname', '=', $data['nickname']];
  783. }
  784. var_dump($where);
  785. $result = ChatTopic::where($where)
  786. ->leftJoin('user', 'user.id', '=', 'chat_topics.user_id')
  787. ->leftJoin('chat_topics_reply', 'chat_topics.id', '=', 'chat_topics_reply.topic_id')
  788. ->leftJoin('chat_topic_class', 'chat_topics.type', '=', 'chat_topic_class.id')
  789. ->select(
  790. 'chat_topics.*',
  791. 'chat_topic_class.topicname as type_name',
  792. 'user.nickname',
  793. 'user.avatar',
  794. 'user.user_name',
  795. DB::raw('count(chat_topics_reply.id) as num')
  796. )
  797. ->groupBy('chat_topics.id')
  798. ->orderBy('chat_topics.id', 'desc')
  799. ->paginate($data['page_size'], ['*'], 'page', $data['page'] ?? 1);
  800. return Result::success($result);
  801. }
  802. public function getTopic(array $data): array
  803. {
  804. $result = ChatTopic::where(['id' => $data['id']])->first();
  805. return Result::success($result);
  806. }
  807. public function addTopic(array $data): array
  808. {
  809. $chattopic = [];
  810. Db::beginTransaction();
  811. try {
  812. $data['created_at'] = date('Y-m-d H:i:s');
  813. $data['updated_at'] = date('Y-m-d H:i:s');
  814. if($data['type_id'] != 10000){
  815. $this->sendMessage([
  816. 'talk_type'=>300,
  817. 'title'=>$data['title'],
  818. 'content'=>'提交了审核',
  819. 'messageType'=>4,
  820. ]);
  821. $data['status'] = 1;
  822. }else{
  823. $data['status'] = 2;
  824. if($data['is_group'] == 1){
  825. $group_id = PublicData::uuid();
  826. $groupData = [
  827. 'id' => $group_id,
  828. 'creator_id' => $data['user_id'],
  829. 'group_name' => $data['group_name'] ?? '',
  830. 'profile' => '',
  831. ];
  832. $groupResult = ChatGroups::insertGetId($groupData);
  833. $groupMemberData = [
  834. 'id' => PublicData::uuid(),
  835. 'user_id' => $data['user_id'],
  836. 'group_id' => $group_id,
  837. 'leader' => 2,
  838. ];
  839. $groupMemberResult = ChatGroupsMember::insertGetId($groupMemberData);
  840. //更新result的 group_id
  841. $datas['group_id'] = $group_id;
  842. // ChatTopic::where(['id' => $data['id']])->update($datas);
  843. //插入一条消息
  844. $chatRecordsData = [
  845. 'user_id' => $data['user_id'],
  846. 'receiver_id' => $group_id,
  847. 'content' => '我创建了一个群' . Date('Y-m-d H:i:s'),
  848. 'msg_type' => 1,
  849. 'is_read' => 0,
  850. 'talk_type' => 2,
  851. 'action' => 'said',
  852. 'group_receiver_id' => $data['user_id'],
  853. ];
  854. ChatRecords::insert($chatRecordsData);
  855. }
  856. }
  857. unset($data['type_id']);
  858. $result = ChatTopic::insertGetId($data);
  859. $chattopic = Chattopic::find($result);
  860. Db::commit();
  861. } catch (\Exception $e) {
  862. Db::rollBack();
  863. return Result::error($data, $e->getMessage());
  864. }
  865. return Result::success($chattopic);
  866. }
  867. public function applyTopic(array $data): array
  868. {
  869. date_default_timezone_set('Asia/Shanghai');
  870. db::beginTransaction();
  871. try {
  872. $query = ChatTopic::where(['id' => $data['id']]);
  873. $topdata = $query->first();
  874. $result = ChatTopic::where(['id' => $data['id']])->update(['status' => $data['status']]);
  875. // var_dump($data, 'tedst111111111111111');
  876. // var_dump(date('Y-m-d H:i:s'), 'tedst111111111111111');
  877. $creatter = $topdata['user_id'];
  878. if ($data['status'] == 2 && $topdata['is_group'] == 1) {
  879. if(empty($topdata['group_id'])){
  880. $group_id = PublicData::uuid();
  881. $groupData = [
  882. 'id' => $group_id,
  883. 'creator_id' => $topdata['user_id'],
  884. 'group_name' => $topdata['group_name'] ?? '',
  885. 'profile' => '',
  886. ];
  887. $groupResult = ChatGroups::insertGetId($groupData);
  888. $groupMemberData = [
  889. 'id' => PublicData::uuid(),
  890. 'user_id' => $topdata['user_id'],
  891. 'group_id' => $group_id,
  892. 'leader' => 2,
  893. ];
  894. $groupMemberResult = ChatGroupsMember::insertGetId($groupMemberData);
  895. }else{
  896. $group_id = $topdata['group_id'];
  897. }
  898. //更新result的 group_id
  899. $datas['group_id'] = $group_id;
  900. ChatTopic::where(['id' => $data['id']])->update($datas);
  901. //插入一条消息
  902. $chatRecordsData = [
  903. 'user_id' => $topdata['user_id'],
  904. 'receiver_id' => $group_id,
  905. 'content' => '我创建了一个群' . Date('Y-m-d H:i:s'),
  906. 'msg_type' => 1,
  907. 'is_read' => 0,
  908. 'talk_type' => 2,
  909. 'action' => 'said',
  910. 'group_receiver_id' => $topdata['user_id'],
  911. ];
  912. ChatRecords::insert($chatRecordsData);
  913. } elseif ($data['status'] == 3) {
  914. if(isset($topdata['group_id']) && !empty($topdata['group_id'])){
  915. ChatRecords::where('receiver_id', $topdata['group_id'])->delete();
  916. ChatGroupsMember::where('group_id', $topdata['group_id'])
  917. ->where([["user_id", '!=', $creatter]])->delete();
  918. }
  919. }
  920. if($data['status'] == 2){
  921. $this->sendMessage([
  922. 'talk_type'=>301,
  923. 'title'=>$topdata['title'],
  924. 'content'=>'审核通过',
  925. 'messageType'=>4,
  926. 'user_id'=>$topdata['user_id'],
  927. ]);
  928. }else if($data['status'] == 3){
  929. $this->sendMessage([
  930. 'talk_type'=>301,
  931. 'title'=>$topdata['title'],
  932. 'content'=>'审核拒绝',
  933. 'messageType'=>4,
  934. 'user_id'=>$topdata['user_id'],
  935. ]);
  936. }
  937. Db::commit();
  938. if ($result) {
  939. return Result::success($data);
  940. } else {
  941. return Result::error($data);
  942. }
  943. } catch (\Exception $e) {
  944. Db::rollBack();
  945. return Result::error($data, $e->getMessage());
  946. }
  947. if (empty($data['id'])) {
  948. return Result::error('id不能为空');
  949. }
  950. }
  951. public function updateTopic(array $data): array
  952. {
  953. if (empty($data['id'])) {
  954. return Result::error('id不能为空');
  955. }
  956. if($data['type_id'] != 10000){
  957. $this->sendMessage([
  958. 'talk_type'=>300,
  959. 'title'=>$data['title'],
  960. 'content'=>'提交了审核',
  961. 'messageType'=>4,
  962. ]);
  963. $data['status'] = 1;
  964. }
  965. $result = ChatTopic::where(['id' => $data['id']])->update($data);
  966. if ($result) {
  967. return Result::success($data);
  968. } else {
  969. return Result::error($data);
  970. };
  971. }
  972. public function delTopic(array $data): array
  973. {
  974. $result = ChatTopic::where(['id' => $data['id']])->delete();
  975. //删除群和成员和聊天
  976. //删除话题回复
  977. if ($result) {
  978. return Result::success($data);
  979. } else {
  980. return Result::error('删除失败');
  981. };
  982. }
  983. public function getTopicInfo(array $data): array
  984. {
  985. $result = ChatTopic::where(['chat_topics.id' => $data['id']])
  986. ->leftJoin('user', 'user.id', '=', 'chat_topics.user_id')
  987. ->leftJoin('chat_topic_class', 'chat_topic_class.id', '=', 'chat_topics.type')
  988. ->select('chat_topics.*', 'user.nickname', 'user.avatar', 'user.user_name','chat_topic_class.topicname')
  989. ->first();
  990. return Result::success($result);
  991. }
  992. public function addReply(array $data): array
  993. {
  994. $result = ChatTopic::where(['id' => $data['id']])->get();
  995. if ($result) {
  996. $replydata['created_at'] = date('Y-m-d H:i:s');
  997. $replydata['updated_at'] = date('Y-m-d H:i:s');
  998. $replydata['content'] = $data['content'];
  999. $replydata['user_id'] = $data['user_id'];
  1000. $replydata['topic_id'] = $data['id'];
  1001. $re = ChatTopicsReply::insertGetId($replydata);
  1002. }
  1003. if ($re) {
  1004. return Result::success($data);
  1005. } else {
  1006. return Result::error($data);
  1007. }
  1008. }
  1009. public function getTopicReply(array $data): array
  1010. {
  1011. var_dump($data);
  1012. $result = ChatTopicsReply::where(['topic_id' => $data['id']])
  1013. ->leftJoin('user', 'user.id', '=', 'chat_topics_reply.user_id')
  1014. ->select('chat_topics_reply.*', 'user.nickname', 'user.avatar', 'user.user_name')
  1015. ->orderBy('chat_topics_reply.id', 'desc')
  1016. ->paginate($data['page_size'], ['*'], 'page', $data['page'] ?? 1);
  1017. return Result::success($result);
  1018. }
  1019. /**
  1020. * 修改群成员
  1021. * @param array $data
  1022. * @return array
  1023. */
  1024. public function updateGroupMembers(array $data): array
  1025. {
  1026. $where = [
  1027. 'group_id' => $data['group_id'],
  1028. ];
  1029. $group_id = $data['group_id'];
  1030. //先删除群成员
  1031. $result = ChatGroupsMember::where($where)
  1032. ->where([["user_id", '!=', $data['user_id']]])->delete();
  1033. $groupMemberData = [];
  1034. foreach ($data['group_member'] as $value) {
  1035. $groupMemberData[] = [
  1036. 'id' => PublicData::uuid(),
  1037. 'user_id' => $value,
  1038. 'group_id' => $group_id,
  1039. 'leader' => 0,
  1040. ];
  1041. }
  1042. $result = ChatGroupsMember::where($where)->insert($groupMemberData);
  1043. // 获取群信息
  1044. $groupInfo = ChatGroups::where(['id' => $group_id])->first();
  1045. if ($result) {
  1046. return Result::success($groupInfo);
  1047. } else {
  1048. return Result::error($data);
  1049. }
  1050. }
  1051. public function clearGroupRecords(array $data): array
  1052. {
  1053. $result = ChatRecords::where(['user_id' => $data['user_id'], 'receiver_id' => $data['id']])->delete();
  1054. if ($result) {
  1055. return Result::success("删除成功");
  1056. } else {
  1057. return Result::error("删除失败");
  1058. }
  1059. }
  1060. public function recallRecord(array $data): array
  1061. {
  1062. //获取所有id,并删除掉
  1063. $ids = array_column($data, 'id');
  1064. $result = ChatRecords::whereIn('id', $ids)->delete();
  1065. if ($result) {
  1066. return Result::success("删除成功");
  1067. } else {
  1068. return Result::error("删除失败");
  1069. }
  1070. }
  1071. public function clearRecords(array $data): array
  1072. {
  1073. $result = ChatRecords::where(['user_id' => $data['user_id'], 'receiver_id' => $data['friend_id']])->delete();
  1074. if ($result) {
  1075. return Result::success("删除成功");
  1076. } else {
  1077. return Result::error("删除失败");
  1078. }
  1079. }
  1080. public function getRecordByContent(array $data): array
  1081. {
  1082. $result = ChatRecords::where(['chat_records.user_id' => $data['user_id'], 'chat_records.receiver_id' => $data['receiver_id'], 'chat_records.content' => $data['content']])
  1083. ->orWhere(['chat_records.receiver_id' => $data['user_id'], 'chat_records.user_id' => $data['receiver_id'], 'chat_records.content' => $data['content']])
  1084. ->all();
  1085. if ($result) {
  1086. return Result::success($result['id']);
  1087. } else {
  1088. return Result::error("没有数据");
  1089. }
  1090. }
  1091. public function getRecord(array $data): array
  1092. {
  1093. $result = ChatRecords::where(['chat_records.id' => $data['id']])
  1094. ->leftJoin('user', 'user.id', '=', 'chat_records.user_id')
  1095. ->leftJoin('user as user2', 'user2.id', '=', 'chat_records.receiver_id')
  1096. ->select('chat_records.*', 'user.nickname', 'user.avatar', 'user.user_name', 'user2.nickname as receiver_nickname', 'user2.avatar as receiver_avatar')
  1097. ->get();
  1098. return Result::success($result);
  1099. }
  1100. public function delReply(array $data): array
  1101. {
  1102. $result = ChatTopicsReply::where(['id' => $data['id']])->delete();
  1103. if ($result) {
  1104. return Result::success("删除成功");
  1105. } else {
  1106. return Result::error("删除失败");
  1107. }
  1108. }
  1109. public function delAllReply(array $data): array
  1110. {
  1111. $result = ChatTopicsReply::where(['topic_id' => $data['topicid']])->delete();
  1112. if ($result) {
  1113. return Result::success("删除成功");
  1114. } else {
  1115. return Result::error("删除失败");
  1116. }
  1117. }
  1118. public function getTopicsListAdmin(array $data): array
  1119. {
  1120. $where = [];
  1121. if (!empty($data['type'])) {
  1122. $where['type'] = $data['type'];
  1123. }
  1124. if (!empty($data['title'])) {
  1125. $where['title'] = $data['title'];
  1126. }
  1127. $result = ChatTopic::where($where)
  1128. ->leftJoin('user', 'user.id', '=', 'chat_topics.user_id')
  1129. ->select('chat_topics.*', 'user.nickname', 'user.avatar', 'user.user_name')
  1130. ->paginate($data['page_size'], ['*'], 'page', $data['page'] ?? 1);
  1131. return Result::success($result);
  1132. }
  1133. public function getTopicClassList(array $data): array
  1134. {
  1135. $where = [];
  1136. if (!empty($data['topicname'])) {
  1137. $where[] = ['topicname', 'like', '%' . $data['topicname'] . '%'];
  1138. }
  1139. $result = ChatTopicClass::where($where)->orderBy('updated_at', 'desc')
  1140. ->paginate($data['page_size'], ['*'], 'page', $data['page'] ?? 1);
  1141. return Result::success($result);
  1142. }
  1143. public function deleteTopicClass(array $data): array
  1144. {
  1145. $result = ChatTopicClass::where(['id' => $data['id']])->delete();
  1146. if ($result) {
  1147. return Result::success("删除成功");
  1148. }
  1149. return Result::error("删除失败");
  1150. }
  1151. public function updateTopicClass(array $data): array
  1152. {
  1153. //topicname
  1154. if (!empty($data['topicname'])) {
  1155. $re = ChatTopicClass::where(['topicname' => $data['topicname']])->first();
  1156. if ($re) {
  1157. return Result::error("话题分类已存在");
  1158. }
  1159. }
  1160. $result = ChatTopicClass::where(['id' => $data['id']])->update([
  1161. 'topicname' => $data['topicname'],
  1162. 'updated_at' => date('Y-m-d H:i:s'),
  1163. ]);
  1164. if ($result) {
  1165. return Result::success("修改成功");
  1166. }
  1167. return Result::error("修改失败");
  1168. }
  1169. public function addTopicClass(array $data): array
  1170. {
  1171. //topicname
  1172. if (!empty($data['topicname'])) {
  1173. $re = ChatTopicClass::where(['topicname' => $data['topicname']])->first();
  1174. if ($re) {
  1175. return Result::error("话题分类已存在");
  1176. }
  1177. }
  1178. $result = ChatTopicClass::insert($data);
  1179. if ($result) {
  1180. return Result::success("添加成功");
  1181. }
  1182. return Result::error("添加失败");
  1183. }
  1184. /**
  1185. * 获取话题分类信息
  1186. * @param array $data
  1187. * @return array
  1188. */
  1189. public function getTopicClassInfo(array $data): array
  1190. {
  1191. $result = ChatTopicClass::where(['id' => $data['id']])->first();
  1192. return Result::success($result);
  1193. }
  1194. /**
  1195. * @param array $data
  1196. * @return array
  1197. */
  1198. public function getBusinessDistrictList(array $data): array
  1199. {
  1200. $query = ChatGroupsMember::Join('chat_topics', 'chat_topics.group_id', '=', 'chat_groups_members.group_id')
  1201. ->leftJoin('chat_topic_class', 'chat_topic_class.id', '=', 'chat_topics.type')
  1202. ->where(['chat_groups_members.user_id' => $data['user_id']])
  1203. ->when($data, function ($query) use ($data) {
  1204. if(!empty($data['type'])){
  1205. $query->where(['chat_topics.type' => $data['type']]);
  1206. }
  1207. if(!empty($data['title'])){
  1208. $query->where('chat_topics.title','like','%'.$data['title'].'%');
  1209. }
  1210. if(!empty($data['created_at'])){
  1211. $query->whereDate('chat_topics.created_at', $data['created_at']);
  1212. }
  1213. })
  1214. ->select(
  1215. 'chat_topics.id',
  1216. 'chat_topics.title',
  1217. 'chat_topics.author',
  1218. 'chat_topics.created_at',
  1219. 'chat_topics.updated_at',
  1220. 'chat_topic_class.topicname',
  1221. )
  1222. ->orderBy('chat_topics.created_at', 'desc');
  1223. $total = $query->count();
  1224. $list = $query->forPage($data['page'], $data['page_size'])->get();
  1225. $result = [
  1226. 'list' => $list,
  1227. 'total' => $total,
  1228. 'page' => intval($data['page']),
  1229. 'page_size' => intval($data['page_size']),
  1230. ];
  1231. return Result::success($result);
  1232. }
  1233. /**
  1234. * 发送消息
  1235. * @param array $messageData
  1236. * @return void
  1237. */
  1238. public function sendMessage($messageData){
  1239. $msg =[
  1240. 'talk_type'=>$messageData['talk_type'],
  1241. 'title'=>$messageData['title'],
  1242. 'content'=>$messageData['content'],
  1243. 'messageType'=>$messageData['messageType'],
  1244. 'user_id'=>$messageData['user_id']??'',
  1245. 'time'=>microtime(),
  1246. ];
  1247. $message = new MqProducer($msg);
  1248. $producer = ContextApplicationContext::getContainer()->get(Producer::class);
  1249. $producer->produce($message);
  1250. }
  1251. }