ChatService.php 49 KB

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