ChatService.php 49 KB

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