ChatService.php 49 KB

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