ChatService.php 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418
  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. $talk_session = [
  759. 'user_id' => $data['user_id'],
  760. 'to_from_id' => $data['group_id'],
  761. 'talk_mode' => 2,
  762. 'created_at' => Date('Y-m-d H:i:s'),
  763. 'updated_at' => Date('Y-m-d H:i:s'),
  764. ];
  765. $talk_session_result = GroupTalkImp::insertGetId($talk_session);
  766. if ($groupMemberResult) {
  767. return Result::success($data);
  768. } else {
  769. return Result::error($data);
  770. };
  771. //ljim
  772. $group = ChatGroups::where(['id' => $data['group_id']])->first();
  773. if (empty($group)) {
  774. return Result::error("群不存在", 0);
  775. }
  776. $groupMember = ChatGroupsMember::where(['user_id' => $data['user_id'], 'group_id' => $data['group_id']])->first();
  777. if ($groupMember) {
  778. return Result::error("已加入群", 0);
  779. }
  780. $info = [
  781. 'id' => PublicData::uuid(),
  782. 'user_id' => $data['user_id'],
  783. 'group_id' => $data['group_id'],
  784. ];
  785. $result = ChatGroupsMember::insert($info);
  786. var_dump($result, '--------------------');
  787. if ($result) {
  788. return Result::success($data);
  789. } else {
  790. return Result::error($data);
  791. };
  792. }
  793. /**
  794. * 话题 - 列表
  795. * @param array $data
  796. * @return array
  797. */
  798. public function getTopicsList(array $data): array
  799. {
  800. $where = [];
  801. if (!empty($data['title'])) {
  802. $where[] = ['chat_topics.title', 'like', '%' . $data['title'] . '%'];
  803. }
  804. // 不是看自己的话题
  805. if (!empty($data['user_id_search'])) {
  806. $where[] = ['chat_topics.user_id', '=', $data['user_id_search']];
  807. }
  808. if (!empty($data['status'])) {
  809. $where[] = ['chat_topics.status', '=', $data['status']];
  810. }
  811. if (!empty($data['type'])) {
  812. $where[] = ['chat_topics.type', '=', $data['type']];
  813. }
  814. if (!empty($data['nickname'])) {
  815. $where[] = ['user.nickname', '=', $data['nickname']];
  816. }
  817. var_dump($where);
  818. $result = ChatTopic::where($where)
  819. ->leftJoin('user', 'user.id', '=', 'chat_topics.user_id')
  820. ->leftJoin('chat_topics_reply', 'chat_topics.id', '=', 'chat_topics_reply.topic_id')
  821. ->leftJoin('chat_topic_class', 'chat_topics.type', '=', 'chat_topic_class.id')
  822. ->select(
  823. 'chat_topics.*',
  824. 'chat_topic_class.topicname as type_name',
  825. 'user.nickname',
  826. 'user.avatar',
  827. 'user.user_name',
  828. DB::raw('count(chat_topics_reply.id) as num')
  829. )
  830. ->groupBy('chat_topics.id')
  831. ->orderBy('chat_topics.updated_at', 'desc')
  832. ->paginate($data['page_size'], ['*'], 'page', $data['page'] ?? 1);
  833. return Result::success($result);
  834. }
  835. public function getTopic(array $data): array
  836. {
  837. $result = ChatTopic::where(['id' => $data['id']])->first();
  838. return Result::success($result);
  839. }
  840. public function addTopic(array $data): array
  841. {
  842. $chattopic = [];
  843. date_default_timezone_set('Asia/Shanghai');
  844. $data['created_at'] = date('Y-m-d H:i:s');
  845. $data['updated_at'] = date('Y-m-d H:i:s');
  846. $group_id = '';
  847. if ($data['type_id'] != 10000) {
  848. $this->sendMessage([
  849. 'talk_type' => 300,
  850. 'title' => $data['title'],
  851. 'content' => '提交了审核',
  852. 'messageType' => 4,
  853. ]);
  854. $data['status'] = 1;
  855. } else {
  856. $data['status'] = 2;
  857. if ($data['is_group'] == 1) {
  858. $group_id = PublicData::uuid();
  859. var_dump($group_id);
  860. $groupData = [
  861. 'id' => $group_id,
  862. 'creator_id' => $data['user_id'],
  863. 'group_name' => $data['group_name'] ?? '',
  864. 'profile' => '',
  865. ];
  866. $groupResult = ChatGroups::insertGetId($groupData);
  867. $groupMemberData = [
  868. 'id' => PublicData::uuid(),
  869. 'user_id' => $data['user_id'],
  870. 'group_id' => $group_id,
  871. 'leader' => 2,
  872. ];
  873. $groupMemberResult = ChatGroupsMember::insertGetId($groupMemberData);
  874. //更新result的 group_id
  875. // $datas['group_id'] = $group_id;
  876. // ChatTopic::where(['id' => $data['id']])->update($datas);
  877. //插入一条消息
  878. $chatRecordsData = [
  879. 'user_id' => $data['user_id'],
  880. 'receiver_id' => $group_id,
  881. 'content' => '我创建了一个群' . Date('Y-m-d H:i:s'),
  882. 'msg_type' => 1,
  883. 'is_read' => 0,
  884. 'talk_type' => 2,
  885. 'action' => 'said',
  886. 'group_receiver_id' => $data['user_id'],
  887. ];
  888. ChatRecords::insert($chatRecordsData);
  889. //插入imp group group_member
  890. $group = [
  891. 'type' => 1,
  892. 'name' => $data['group_name'] ?? '',
  893. 'profile' => '',
  894. 'avatar' => '',
  895. 'creator_id' => $data['user_id'],
  896. ];
  897. $groupResult = GroupImp::insertGetId($group);
  898. var_dump($groupResult, '获取imp group_id');
  899. $groupMember = [
  900. 'user_id' => $data['user_id'],
  901. 'group_id' => $groupResult,
  902. 'leader' => 1,
  903. 'join_time' => Date('Y-m-d H:i:s')
  904. ];
  905. $groupMemberResult = GroupMemberImp::insertGetId($groupMember);
  906. $talk_session = [
  907. 'user_id' => $data['user_id'],
  908. 'to_from_id' => $groupResult,
  909. 'talk_mode' => 2,
  910. 'created_at' => Date('Y-m-d H:i:s'),
  911. 'updated_at' => Date('Y-m-d H:i:s'),
  912. ];
  913. $talk_session_result = GroupTalkImp::insertGetId($talk_session);
  914. $group_id = $groupResult;
  915. }
  916. }
  917. unset($data['type_id']);
  918. if ($group_id) {
  919. $data['group_id'] = $group_id;
  920. }
  921. $result = ChatTopic::insertGetId($data);
  922. $chattopic = Chattopic::find($result);
  923. Db::commit();
  924. Db::beginTransaction();
  925. try {
  926. } catch (\Exception $e) {
  927. Db::rollBack();
  928. return Result::error($data, $e->getMessage());
  929. }
  930. return Result::success($chattopic);
  931. }
  932. public function applyTopic(array $data): array
  933. {
  934. date_default_timezone_set('Asia/Shanghai');
  935. db::beginTransaction();
  936. try {
  937. $query = ChatTopic::where(['id' => $data['id']]);
  938. $topdata = $query->first();
  939. $result = ChatTopic::where(['id' => $data['id']])->update(['status' => $data['status']]);
  940. // var_dump($data, 'tedst111111111111111');
  941. // var_dump(date('Y-m-d H:i:s'), 'tedst111111111111111');
  942. $creatter = $topdata['user_id'];
  943. if ($data['status'] == 2 && $topdata['is_group'] == 1) {
  944. $group_id = '';
  945. if (empty($topdata['group_id'])) {
  946. $group_id = PublicData::uuid();
  947. $groupData = [
  948. 'id' => $group_id,
  949. 'creator_id' => $topdata['user_id'],
  950. 'group_name' => $topdata['group_name'] ?? '',
  951. 'profile' => '',
  952. ];
  953. $groupResult = ChatGroups::insertGetId($groupData);
  954. $groupMemberData = [
  955. 'id' => PublicData::uuid(),
  956. 'user_id' => $topdata['user_id'],
  957. 'group_id' => $group_id,
  958. 'leader' => 2,
  959. ];
  960. $groupMemberResult = ChatGroupsMember::insertGetId($groupMemberData);
  961. $group = [
  962. 'type' => 1,
  963. 'name' => $topdata['group_name'] ?? '',
  964. 'profile' => '',
  965. 'avatar' => '',
  966. 'creator_id' => $topdata['user_id'],
  967. ];
  968. $groupResult = GroupImp::insertGetId($group);
  969. var_dump($groupResult, '获取imp group_id');
  970. $groupMember = [
  971. 'user_id' => $topdata['user_id'],
  972. 'group_id' => $groupResult,
  973. 'leader' => 1,
  974. 'join_time' => Date('Y-m-d H:i:s')
  975. ];
  976. $groupMemberResult = GroupMemberImp::insertGetId($groupMember);
  977. $talk_session = [
  978. 'user_id' => $topdata['user_id'],
  979. 'to_from_id' => $groupResult,
  980. 'talk_mode' => 2,
  981. 'created_at' => Date('Y-m-d H:i:s'),
  982. 'updated_at' => Date('Y-m-d H:i:s'),
  983. ];
  984. $talk_session_result = GroupTalkImp::insertGetId($talk_session);
  985. $group_id = $groupResult;
  986. } else {
  987. $group_id = $topdata['group_id'];
  988. }
  989. //更新result的 group_id
  990. $datas['group_id'] = $group_id;
  991. ChatTopic::where(['id' => $data['id']])->update($datas);
  992. //插入一条消息
  993. $chatRecordsData = [
  994. 'user_id' => $topdata['user_id'],
  995. 'receiver_id' => $group_id,
  996. 'content' => '我创建了一个群' . Date('Y-m-d H:i:s'),
  997. 'msg_type' => 1,
  998. 'is_read' => 0,
  999. 'talk_type' => 2,
  1000. 'action' => 'said',
  1001. 'group_receiver_id' => $topdata['user_id'],
  1002. ];
  1003. ChatRecords::insert($chatRecordsData);
  1004. //新数据库
  1005. } elseif ($data['status'] == 3) {
  1006. if (isset($topdata['group_id']) && !empty($topdata['group_id'])) {
  1007. ChatRecords::where('receiver_id', $topdata['group_id'])->delete();
  1008. ChatGroupsMember::where('group_id', $topdata['group_id'])
  1009. ->where([["user_id", '!=', $creatter]])->delete();
  1010. //直接删除imp
  1011. GroupImp::where('id', $topdata['group_id'])->delete();
  1012. GroupMemberImp::where('group_id', $topdata['group_id'])->delete();
  1013. GroupTalkImp::where('to_from_id', $topdata['group_id'])->delete();
  1014. //删除group_id
  1015. ChatTopic::where('id', $topdata['id'])->update(['group_id' => 0]);
  1016. }
  1017. }
  1018. if ($data['status'] == 2) {
  1019. $this->sendMessage([
  1020. 'talk_type' => 301,
  1021. 'title' => $topdata['title'],
  1022. 'content' => '审核通过',
  1023. 'messageType' => 4,
  1024. 'user_id' => $topdata['user_id'],
  1025. ]);
  1026. } else if ($data['status'] == 3) {
  1027. $this->sendMessage([
  1028. 'talk_type' => 301,
  1029. 'title' => $topdata['title'],
  1030. 'content' => '审核拒绝',
  1031. 'messageType' => 4,
  1032. 'user_id' => $topdata['user_id'],
  1033. ]);
  1034. }
  1035. Db::commit();
  1036. if ($result) {
  1037. return Result::success($data);
  1038. } else {
  1039. return Result::error($data);
  1040. }
  1041. } catch (\Exception $e) {
  1042. Db::rollBack();
  1043. return Result::error($data, $e->getMessage());
  1044. }
  1045. if (empty($data['id'])) {
  1046. return Result::error('id不能为空');
  1047. }
  1048. }
  1049. public function updateTopic(array $data): array
  1050. {
  1051. if (empty($data['id'])) {
  1052. return Result::error('id不能为空');
  1053. }
  1054. if ($data['type_id'] != 10000) {
  1055. $this->sendMessage([
  1056. 'talk_type' => 300,
  1057. 'title' => $data['title'],
  1058. 'content' => '提交了审核',
  1059. 'messageType' => 4,
  1060. ]);
  1061. $data['status'] = 1;
  1062. }
  1063. $result = ChatTopic::where(['id' => $data['id']])->update($data);
  1064. if ($result) {
  1065. return Result::success($data);
  1066. } else {
  1067. return Result::error($data);
  1068. };
  1069. }
  1070. public function delTopic(array $data): array
  1071. {
  1072. $result = ChatTopic::where(['id' => $data['id']])->delete();
  1073. //删除群和成员和聊天
  1074. //删除话题回复
  1075. if ($result) {
  1076. return Result::success($data);
  1077. } else {
  1078. return Result::error('删除失败');
  1079. };
  1080. }
  1081. public function getTopicInfo(array $data): array
  1082. {
  1083. $result = ChatTopic::where(['chat_topics.id' => $data['id']])
  1084. ->leftJoin('user', 'user.id', '=', 'chat_topics.user_id')
  1085. ->leftJoin('chat_topic_class', 'chat_topic_class.id', '=', 'chat_topics.type')
  1086. ->select('chat_topics.*', 'user.nickname', 'user.avatar', 'user.user_name', 'chat_topic_class.topicname')
  1087. ->first();
  1088. return Result::success($result);
  1089. }
  1090. public function addReply(array $data): array
  1091. {
  1092. $result = ChatTopic::where(['id' => $data['id']])->get();
  1093. if ($result) {
  1094. $replydata['created_at'] = date('Y-m-d H:i:s');
  1095. $replydata['updated_at'] = date('Y-m-d H:i:s');
  1096. $replydata['content'] = $data['content'];
  1097. $replydata['user_id'] = $data['user_id'];
  1098. $replydata['topic_id'] = $data['id'];
  1099. $re = ChatTopicsReply::insertGetId($replydata);
  1100. }
  1101. if ($re) {
  1102. return Result::success($data);
  1103. } else {
  1104. return Result::error($data);
  1105. }
  1106. }
  1107. public function getTopicReply(array $data): array
  1108. {
  1109. var_dump($data);
  1110. $result = ChatTopicsReply::where(['topic_id' => $data['id']])
  1111. ->leftJoin('user', 'user.id', '=', 'chat_topics_reply.user_id')
  1112. ->select('chat_topics_reply.*', 'user.nickname', 'user.avatar', 'user.user_name')
  1113. ->orderBy('chat_topics_reply.id', 'desc')
  1114. ->paginate($data['page_size'], ['*'], 'page', $data['page'] ?? 1);
  1115. return Result::success($result);
  1116. }
  1117. /**
  1118. * 修改群成员
  1119. * @param array $data
  1120. * @return array
  1121. */
  1122. public function updateGroupMembers(array $data): array
  1123. {
  1124. DB::beginTransaction();
  1125. try {
  1126. $where = [
  1127. 'group_id' => $data['group_id'],
  1128. ];
  1129. $group_id = $data['group_id'];
  1130. //先删除群成员
  1131. $result = ChatGroupsMember::where($where)
  1132. ->where([["user_id", '!=', $data['user_id']]])->delete();
  1133. $groupMemberData = [];
  1134. foreach ($data['group_member'] as $value) {
  1135. $groupMemberData[] = [
  1136. 'id' => PublicData::uuid(),
  1137. 'user_id' => $value,
  1138. 'group_id' => $group_id,
  1139. 'leader' => 0,
  1140. ];
  1141. }
  1142. $result = ChatGroupsMember::where($where)->insert($groupMemberData);
  1143. //删除会话踢出群的会话信息
  1144. // var_dump("data",$data);
  1145. //字符串转数组,
  1146. $groupIdList = $data['group_member'];
  1147. array_push($groupIdList, $data['user_id']);
  1148. // var_dump("几个",$groupIdList);
  1149. ChatRecords::whereNotIn('receiver_id', $groupIdList)->delete();
  1150. // 获取群信息
  1151. $groupInfo = ChatGroups::where(['id' => $group_id])->first();
  1152. DB::commit();
  1153. return Result::success($groupInfo);
  1154. } catch (\Exception $e) {
  1155. DB::rollBack();
  1156. return Result::error($data, $e->getMessage());
  1157. }
  1158. }
  1159. public function clearGroupRecords(array $data): array
  1160. {
  1161. $result = ChatRecords::where(['user_id' => $data['user_id'], 'receiver_id' => $data['id']])->delete();
  1162. if ($result) {
  1163. return Result::success("删除成功");
  1164. } else {
  1165. return Result::error("删除失败");
  1166. }
  1167. }
  1168. public function recallRecord(array $data): array
  1169. {
  1170. //获取所有id,并删除掉
  1171. $ids = array_column($data, 'id');
  1172. $result = ChatRecords::whereIn('id', $ids)->delete();
  1173. if ($result) {
  1174. return Result::success("删除成功");
  1175. } else {
  1176. return Result::error("删除失败");
  1177. }
  1178. }
  1179. public function clearRecords(array $data): array
  1180. {
  1181. $result = ChatRecords::where(['user_id' => $data['user_id'], 'receiver_id' => $data['friend_id']])->delete();
  1182. if ($result) {
  1183. return Result::success("删除成功");
  1184. } else {
  1185. return Result::error("删除失败");
  1186. }
  1187. }
  1188. public function getRecordByContent(array $data): array
  1189. {
  1190. $result = ChatRecords::where(['chat_records.user_id' => $data['user_id'], 'chat_records.receiver_id' => $data['receiver_id'], 'chat_records.content' => $data['content']])
  1191. ->orWhere(['chat_records.receiver_id' => $data['user_id'], 'chat_records.user_id' => $data['receiver_id'], 'chat_records.content' => $data['content']])
  1192. ->all();
  1193. if ($result) {
  1194. return Result::success($result['id']);
  1195. } else {
  1196. return Result::error("没有数据");
  1197. }
  1198. }
  1199. public function getRecord(array $data): array
  1200. {
  1201. $result = ChatRecords::where(['chat_records.id' => $data['id']])
  1202. ->leftJoin('user', 'user.id', '=', 'chat_records.user_id')
  1203. ->leftJoin('user as user2', 'user2.id', '=', 'chat_records.receiver_id')
  1204. ->select('chat_records.*', 'user.nickname', 'user.avatar', 'user.user_name', 'user2.nickname as receiver_nickname', 'user2.avatar as receiver_avatar')
  1205. ->get();
  1206. return Result::success($result);
  1207. }
  1208. public function delReply(array $data): array
  1209. {
  1210. $result = ChatTopicsReply::where(['id' => $data['id']])->delete();
  1211. if ($result) {
  1212. return Result::success("删除成功");
  1213. } else {
  1214. return Result::error("删除失败");
  1215. }
  1216. }
  1217. public function delAllReply(array $data): array
  1218. {
  1219. $result = ChatTopicsReply::where(['topic_id' => $data['topicid']])->delete();
  1220. if ($result) {
  1221. return Result::success("删除成功");
  1222. } else {
  1223. return Result::error("删除失败");
  1224. }
  1225. }
  1226. public function getTopicsListAdmin(array $data): array
  1227. {
  1228. $where = [];
  1229. if (!empty($data['type'])) {
  1230. $where['type'] = $data['type'];
  1231. }
  1232. if (!empty($data['title'])) {
  1233. $where['title'] = $data['title'];
  1234. }
  1235. $result = ChatTopic::where($where)
  1236. ->leftJoin('user', 'user.id', '=', 'chat_topics.user_id')
  1237. ->select('chat_topics.*', 'user.nickname', 'user.avatar', 'user.user_name')
  1238. ->paginate($data['page_size'], ['*'], 'page', $data['page'] ?? 1);
  1239. return Result::success($result);
  1240. }
  1241. public function getTopicClassList(array $data): array
  1242. {
  1243. $where = [];
  1244. if (!empty($data['topicname'])) {
  1245. $where[] = ['topicname', 'like', '%' . $data['topicname'] . '%'];
  1246. }
  1247. $result = ChatTopicClass::where($where)->orderBy('updated_at', 'desc')
  1248. ->paginate($data['page_size'], ['*'], 'page', $data['page'] ?? 1);
  1249. return Result::success($result);
  1250. }
  1251. public function deleteTopicClass(array $data): array
  1252. {
  1253. $result = ChatTopicClass::where(['id' => $data['id']])->delete();
  1254. if ($result) {
  1255. return Result::success("删除成功");
  1256. }
  1257. return Result::error("删除失败");
  1258. }
  1259. public function updateTopicClass(array $data): array
  1260. {
  1261. //topicname
  1262. if (!empty($data['topicname'])) {
  1263. $re = ChatTopicClass::where(['topicname' => $data['topicname']])->first();
  1264. if ($re) {
  1265. return Result::error("话题分类已存在");
  1266. }
  1267. }
  1268. $result = ChatTopicClass::where(['id' => $data['id']])->update([
  1269. 'topicname' => $data['topicname'],
  1270. 'updated_at' => date('Y-m-d H:i:s'),
  1271. ]);
  1272. if ($result) {
  1273. return Result::success("修改成功");
  1274. }
  1275. return Result::error("修改失败");
  1276. }
  1277. public function addTopicClass(array $data): array
  1278. {
  1279. //topicname
  1280. if (!empty($data['topicname'])) {
  1281. $re = ChatTopicClass::where(['topicname' => $data['topicname']])->first();
  1282. if ($re) {
  1283. return Result::error("话题分类已存在");
  1284. }
  1285. }
  1286. $result = ChatTopicClass::insert($data);
  1287. if ($result) {
  1288. return Result::success("添加成功");
  1289. }
  1290. return Result::error("添加失败");
  1291. }
  1292. /**
  1293. * 获取话题分类信息
  1294. * @param array $data
  1295. * @return array
  1296. */
  1297. public function getTopicClassInfo(array $data): array
  1298. {
  1299. $result = ChatTopicClass::where(['id' => $data['id']])->first();
  1300. return Result::success($result);
  1301. }
  1302. /**
  1303. * @param array $data
  1304. * @return array
  1305. */
  1306. public function getBusinessDistrictList(array $data): array
  1307. {
  1308. $query = GroupMemberImp::Join('hyperf.chat_topics', 'hyperf.chat_topics.group_id', '=', 'imp.group_member.group_id')
  1309. ->leftJoin('hyperf.chat_topic_class', 'hyperf.chat_topic_class.id', '=', 'hyperf.chat_topics.type')
  1310. ->where(['imp.group_member.user_id' => $data['user_id']])
  1311. ->when($data, function ($query) use ($data) {
  1312. if (!empty($data['type'])) {
  1313. $query->where(['hyperf.chat_topics.type' => $data['type']]);
  1314. }
  1315. if (!empty($data['title'])) {
  1316. $query->where('hyperf.chat_topics.title', 'like', '%' . $data['title'] . '%');
  1317. }
  1318. if (!empty($data['created_at'])) {
  1319. $query->whereDate('hyperf.chat_topics.created_at', $data['created_at']);
  1320. }
  1321. })
  1322. ->select(
  1323. 'hyperf.chat_topics.id',
  1324. 'hyperf.chat_topics.title',
  1325. 'hyperf.chat_topics.author',
  1326. 'hyperf.chat_topics.created_at',
  1327. 'hyperf.chat_topics.updated_at',
  1328. 'hyperf.chat_topic_class.topicname',
  1329. )
  1330. ->orderBy('hyperf.chat_topics.created_at', 'desc');
  1331. $total = $query->count();
  1332. $list = $query->forPage($data['page'], $data['page_size'])->get();
  1333. $result = [
  1334. 'list' => $list,
  1335. 'total' => $total,
  1336. 'page' => intval($data['page']),
  1337. 'page_size' => intval($data['page_size']),
  1338. ];
  1339. return Result::success($result);
  1340. }
  1341. /**
  1342. * 发送消息
  1343. * @param array $messageData
  1344. * @return void
  1345. */
  1346. public function sendMessage($messageData)
  1347. {
  1348. $msg = [
  1349. 'talk_type' => $messageData['talk_type'],
  1350. 'title' => $messageData['title'],
  1351. 'content' => $messageData['content'],
  1352. 'messageType' => $messageData['messageType'],
  1353. 'user_id' => $messageData['user_id'] ?? '',
  1354. 'time' => microtime(),
  1355. ];
  1356. $message = new MqProducer($msg);
  1357. $producer = ContextApplicationContext::getContainer()->get(Producer::class);
  1358. $producer->produce($message);
  1359. }
  1360. }