ChatService.php 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304
  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. //更新result的 group_id
  896. $datas['group_id'] = $group_id;
  897. ChatTopic::where(['id' => $data['id']])->update($datas);
  898. //插入一条消息
  899. $chatRecordsData = [
  900. 'user_id' => $topdata['user_id'],
  901. 'receiver_id' => $group_id,
  902. 'content' => '我创建了一个群' . Date('Y-m-d H:i:s'),
  903. 'msg_type' => 1,
  904. 'is_read' => 0,
  905. 'talk_type' => 2,
  906. 'action' => 'said',
  907. 'group_receiver_id' => $topdata['user_id'],
  908. ];
  909. ChatRecords::insert($chatRecordsData);
  910. }
  911. } elseif ($data['status'] == 3) {
  912. if(isset($topdata['group_id']) && !empty($topdata['group_id'])){
  913. ChatRecords::where('receiver_id', $topdata['group_id'])->delete();
  914. ChatGroupsMember::where('group_id', $topdata['group_id'])
  915. ->where([["user_id", '!=', $creatter]])->delete();
  916. }
  917. }
  918. var_dump("状态:".$data['status']);
  919. if($data['status'] == 2){
  920. $this->sendMessage([
  921. 'talk_type'=>301,
  922. 'title'=>$topdata['title'],
  923. 'content'=>'审核通过',
  924. 'messageType'=>4,
  925. 'user_id'=>$topdata['user_id'],
  926. ]);
  927. }else if($data['status'] == 3){
  928. $this->sendMessage([
  929. 'talk_type'=>301,
  930. 'title'=>$topdata['title'],
  931. 'content'=>'审核拒绝',
  932. 'messageType'=>4,
  933. 'user_id'=>$topdata['user_id'],
  934. ]);
  935. }
  936. Db::commit();
  937. if ($result) {
  938. return Result::success($data);
  939. } else {
  940. return Result::error($data);
  941. }
  942. } catch (\Exception $e) {
  943. Db::rollBack();
  944. return Result::error($data, $e->getMessage());
  945. }
  946. if (empty($data['id'])) {
  947. return Result::error('id不能为空');
  948. }
  949. }
  950. public function updateTopic(array $data): array
  951. {
  952. if (empty($data['id'])) {
  953. return Result::error('id不能为空');
  954. }
  955. if($data['type_id'] != 10000){
  956. $this->sendMessage([
  957. 'talk_type'=>300,
  958. 'title'=>$data['title'],
  959. 'content'=>'提交了审核',
  960. 'messageType'=>4,
  961. ]);
  962. $data['status'] = 1;
  963. }
  964. $result = ChatTopic::where(['id' => $data['id']])->update($data);
  965. if ($result) {
  966. return Result::success($data);
  967. } else {
  968. return Result::error($data);
  969. };
  970. }
  971. public function delTopic(array $data): array
  972. {
  973. $result = ChatTopic::where(['id' => $data['id']])->delete();
  974. //删除群和成员和聊天
  975. //删除话题回复
  976. if ($result) {
  977. return Result::success($data);
  978. } else {
  979. return Result::error('删除失败');
  980. };
  981. }
  982. public function getTopicInfo(array $data): array
  983. {
  984. $result = ChatTopic::where(['chat_topics.id' => $data['id']])
  985. ->leftJoin('user', 'user.id', '=', 'chat_topics.user_id')
  986. ->leftJoin('chat_topic_class', 'chat_topic_class.id', '=', 'chat_topics.type')
  987. ->select('chat_topics.*', 'user.nickname', 'user.avatar', 'user.user_name','chat_topic_class.topicname')
  988. ->first();
  989. return Result::success($result);
  990. }
  991. public function addReply(array $data): array
  992. {
  993. $result = ChatTopic::where(['id' => $data['id']])->get();
  994. if ($result) {
  995. $replydata['created_at'] = date('Y-m-d H:i:s');
  996. $replydata['updated_at'] = date('Y-m-d H:i:s');
  997. $replydata['content'] = $data['content'];
  998. $replydata['user_id'] = $data['user_id'];
  999. $replydata['topic_id'] = $data['id'];
  1000. $re = ChatTopicsReply::insertGetId($replydata);
  1001. }
  1002. if ($re) {
  1003. return Result::success($data);
  1004. } else {
  1005. return Result::error($data);
  1006. }
  1007. }
  1008. public function getTopicReply(array $data): array
  1009. {
  1010. var_dump($data);
  1011. $result = ChatTopicsReply::where(['topic_id' => $data['id']])
  1012. ->leftJoin('user', 'user.id', '=', 'chat_topics_reply.user_id')
  1013. ->select('chat_topics_reply.*', 'user.nickname', 'user.avatar', 'user.user_name')
  1014. ->orderBy('chat_topics_reply.id', 'desc')
  1015. ->paginate($data['page_size'], ['*'], 'page', $data['page'] ?? 1);
  1016. return Result::success($result);
  1017. }
  1018. /**
  1019. * 修改群成员
  1020. * @param array $data
  1021. * @return array
  1022. */
  1023. public function updateGroupMembers(array $data): array
  1024. {
  1025. $where = [
  1026. 'group_id' => $data['group_id'],
  1027. ];
  1028. $group_id = $data['group_id'];
  1029. //先删除群成员
  1030. $result = ChatGroupsMember::where($where)
  1031. ->where([["user_id", '!=', $data['user_id']]])->delete();
  1032. $groupMemberData = [];
  1033. foreach ($data['group_member'] as $value) {
  1034. $groupMemberData[] = [
  1035. 'id' => PublicData::uuid(),
  1036. 'user_id' => $value,
  1037. 'group_id' => $group_id,
  1038. 'leader' => 0,
  1039. ];
  1040. }
  1041. $result = ChatGroupsMember::where($where)->insert($groupMemberData);
  1042. // 获取群信息
  1043. $groupInfo = ChatGroups::where(['id' => $group_id])->first();
  1044. if ($result) {
  1045. return Result::success($groupInfo);
  1046. } else {
  1047. return Result::error($data);
  1048. }
  1049. }
  1050. public function clearGroupRecords(array $data): array
  1051. {
  1052. $result = ChatRecords::where(['user_id' => $data['user_id'], 'receiver_id' => $data['id']])->delete();
  1053. if ($result) {
  1054. return Result::success("删除成功");
  1055. } else {
  1056. return Result::error("删除失败");
  1057. }
  1058. }
  1059. public function recallRecord(array $data): array
  1060. {
  1061. //获取所有id,并删除掉
  1062. $ids = array_column($data, 'id');
  1063. $result = ChatRecords::whereIn('id', $ids)->delete();
  1064. if ($result) {
  1065. return Result::success("删除成功");
  1066. } else {
  1067. return Result::error("删除失败");
  1068. }
  1069. }
  1070. public function clearRecords(array $data): array
  1071. {
  1072. $result = ChatRecords::where(['user_id' => $data['user_id'], 'receiver_id' => $data['friend_id']])->delete();
  1073. if ($result) {
  1074. return Result::success("删除成功");
  1075. } else {
  1076. return Result::error("删除失败");
  1077. }
  1078. }
  1079. public function getRecordByContent(array $data): array
  1080. {
  1081. $result = ChatRecords::where(['chat_records.user_id' => $data['user_id'], 'chat_records.receiver_id' => $data['receiver_id'], 'chat_records.content' => $data['content']])
  1082. ->orWhere(['chat_records.receiver_id' => $data['user_id'], 'chat_records.user_id' => $data['receiver_id'], 'chat_records.content' => $data['content']])
  1083. ->all();
  1084. if ($result) {
  1085. return Result::success($result['id']);
  1086. } else {
  1087. return Result::error("没有数据");
  1088. }
  1089. }
  1090. public function getRecord(array $data): array
  1091. {
  1092. $result = ChatRecords::where(['chat_records.id' => $data['id']])
  1093. ->leftJoin('user', 'user.id', '=', 'chat_records.user_id')
  1094. ->leftJoin('user as user2', 'user2.id', '=', 'chat_records.receiver_id')
  1095. ->select('chat_records.*', 'user.nickname', 'user.avatar', 'user.user_name', 'user2.nickname as receiver_nickname', 'user2.avatar as receiver_avatar')
  1096. ->get();
  1097. return Result::success($result);
  1098. }
  1099. public function delReply(array $data): array
  1100. {
  1101. $result = ChatTopicsReply::where(['id' => $data['id']])->delete();
  1102. if ($result) {
  1103. return Result::success("删除成功");
  1104. } else {
  1105. return Result::error("删除失败");
  1106. }
  1107. }
  1108. public function delAllReply(array $data): array
  1109. {
  1110. $result = ChatTopicsReply::where(['topic_id' => $data['topicid']])->delete();
  1111. if ($result) {
  1112. return Result::success("删除成功");
  1113. } else {
  1114. return Result::error("删除失败");
  1115. }
  1116. }
  1117. public function getTopicsListAdmin(array $data): array
  1118. {
  1119. $where = [];
  1120. if (!empty($data['type'])) {
  1121. $where['type'] = $data['type'];
  1122. }
  1123. if (!empty($data['title'])) {
  1124. $where['title'] = $data['title'];
  1125. }
  1126. $result = ChatTopic::where($where)
  1127. ->leftJoin('user', 'user.id', '=', 'chat_topics.user_id')
  1128. ->select('chat_topics.*', 'user.nickname', 'user.avatar', 'user.user_name')
  1129. ->paginate($data['page_size'], ['*'], 'page', $data['page'] ?? 1);
  1130. return Result::success($result);
  1131. }
  1132. public function getTopicClassList(array $data): array
  1133. {
  1134. $where = [];
  1135. if (!empty($data['topicname'])) {
  1136. $where[] = ['topicname', 'like', '%' . $data['topicname'] . '%'];
  1137. }
  1138. $result = ChatTopicClass::where($where)->orderBy('updated_at', 'desc')
  1139. ->paginate($data['page_size'], ['*'], 'page', $data['page'] ?? 1);
  1140. return Result::success($result);
  1141. }
  1142. public function deleteTopicClass(array $data): array
  1143. {
  1144. $result = ChatTopicClass::where(['id' => $data['id']])->delete();
  1145. if ($result) {
  1146. return Result::success("删除成功");
  1147. }
  1148. return Result::error("删除失败");
  1149. }
  1150. public function updateTopicClass(array $data): array
  1151. {
  1152. //topicname
  1153. if (!empty($data['topicname'])) {
  1154. $re = ChatTopicClass::where(['topicname' => $data['topicname']])->first();
  1155. if ($re) {
  1156. return Result::error("话题分类已存在");
  1157. }
  1158. }
  1159. $result = ChatTopicClass::where(['id' => $data['id']])->update([
  1160. 'topicname' => $data['topicname'],
  1161. 'updated_at' => date('Y-m-d H:i:s'),
  1162. ]);
  1163. if ($result) {
  1164. return Result::success("修改成功");
  1165. }
  1166. return Result::error("修改失败");
  1167. }
  1168. public function addTopicClass(array $data): array
  1169. {
  1170. //topicname
  1171. if (!empty($data['topicname'])) {
  1172. $re = ChatTopicClass::where(['topicname' => $data['topicname']])->first();
  1173. if ($re) {
  1174. return Result::error("话题分类已存在");
  1175. }
  1176. }
  1177. $result = ChatTopicClass::insert($data);
  1178. if ($result) {
  1179. return Result::success("添加成功");
  1180. }
  1181. return Result::error("添加失败");
  1182. }
  1183. /**
  1184. * 获取话题分类信息
  1185. * @param array $data
  1186. * @return array
  1187. */
  1188. public function getTopicClassInfo(array $data): array
  1189. {
  1190. $result = ChatTopicClass::where(['id' => $data['id']])->first();
  1191. return Result::success($result);
  1192. }
  1193. /**
  1194. * @param array $data
  1195. * @return array
  1196. */
  1197. public function getBusinessDistrictList(array $data): array
  1198. {
  1199. $query = ChatGroupsMember::Join('chat_topics', 'chat_topics.group_id', '=', 'chat_groups_members.group_id')
  1200. ->leftJoin('chat_topic_class', 'chat_topic_class.id', '=', 'chat_topics.type')
  1201. ->where(['chat_groups_members.user_id' => $data['user_id']])
  1202. ->when($data, function ($query) use ($data) {
  1203. if(!empty($data['type'])){
  1204. $query->where(['chat_topics.type' => $data['type']]);
  1205. }
  1206. if(!empty($data['title'])){
  1207. $query->where('chat_topics.title','like','%'.$data['title'].'%');
  1208. }
  1209. if(!empty($data['created_at'])){
  1210. $query->whereDate('chat_topics.created_at', $data['created_at']);
  1211. }
  1212. })
  1213. ->select(
  1214. 'chat_topics.id',
  1215. 'chat_topics.title',
  1216. 'chat_topics.author',
  1217. 'chat_topics.created_at',
  1218. 'chat_topics.updated_at',
  1219. 'chat_topic_class.topicname',
  1220. )
  1221. ->orderBy('chat_topics.created_at', 'desc');
  1222. $total = $query->count();
  1223. $list = $query->forPage($data['page'], $data['page_size'])->get();
  1224. $result = [
  1225. 'list' => $list,
  1226. 'total' => $total,
  1227. 'page' => intval($data['page']),
  1228. 'page_size' => intval($data['page_size']),
  1229. ];
  1230. return Result::success($result);
  1231. }
  1232. /**
  1233. * 发送消息
  1234. * @param array $messageData
  1235. * @return void
  1236. */
  1237. public function sendMessage($messageData){
  1238. $msg =[
  1239. 'talk_type'=>$messageData['talk_type'],
  1240. 'title'=>$messageData['title'],
  1241. 'content'=>$messageData['content'],
  1242. 'messageType'=>$messageData['messageType'],
  1243. 'user_id'=>$messageData['user_id']??'',
  1244. 'time'=>microtime(),
  1245. ];
  1246. $message = new MqProducer($msg);
  1247. $producer = ContextApplicationContext::getContainer()->get(Producer::class);
  1248. $producer->produce($message);
  1249. }
  1250. }