ChatService.php 50 KB

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