LiuJ 20 годин тому
батько
коміт
d6657eed70
1 змінених файлів з 23 додано та 18 видалено
  1. 23 18
      app/JsonRpc/ChatService.php

+ 23 - 18
app/JsonRpc/ChatService.php

@@ -1,4 +1,5 @@
 <?php
+
 namespace App\JsonRpc;
 
 use App\Model\ChatFriends;
@@ -61,7 +62,7 @@ class ChatService implements ChatServiceInterface
             ->where('user.id', '<>', $userId)
             ->where('user.type_id', '<>', '20000')
             ->select('user.*', 'chat_friends.remark as remark', 'chat_friends.id as isfriend')
-        // ->select('user.*', 'chat_friends.friend_id')
+            // ->select('user.*', 'chat_friends.friend_id')
             ->get();
 
         return Result::success($result);
@@ -80,8 +81,7 @@ class ChatService implements ChatServiceInterface
     public function addFriend(array $data): array
     {
         Db::beginTransaction();
-        try
-        {
+        try {
             // 检查是否存在相同的记录
             $existingRecord = ChatFriends::where([
                 'user_id' => $data['user_id'],
@@ -236,7 +236,6 @@ class ChatService implements ChatServiceInterface
                     Db::rollBack();
                     throw new \Exception('记录不存在');
                 }
-
             } catch (\Throwable $ex) {
                 Db::rollBack();
                 var_dump($ex->getMessage());
@@ -340,7 +339,8 @@ class ChatService implements ChatServiceInterface
                 'receiver_id' => $data['receiver_id'] ?? '',
             ]];
             ChatRecords::insert($ChatRecordsData);
-            Db::commit();} catch (\Throwable $ex) {
+            Db::commit();
+        } catch (\Throwable $ex) {
             Db::rollBack();
             var_dump($ex->getMessage());
             return Result::error("存储消息失败", 0);
@@ -355,7 +355,8 @@ class ChatService implements ChatServiceInterface
      */
     public function updateFriend(array $data): array
     {
-        $result = ChatFriends::where(['user_id' => $data['user_id'],
+        $result = ChatFriends::where([
+            'user_id' => $data['user_id'],
             'friend_id' => $data['friend_id'],
             'status' => 2,
         ])->update(['remark' => $data['remark']]);
@@ -375,7 +376,7 @@ class ChatService implements ChatServiceInterface
         $userId = $data['user_id'];
         $unreadMessages = ChatRecords::where('user_id', $userId)
             ->where('is_read', 0)
-        // ->where('action', 'recieved')
+            // ->where('action', 'recieved')
             ->leftJoin('user', 'chat_records.receiver_id', '=', 'user.id')
             ->leftJoin('chat_groups', 'chat_records.receiver_id', '=', 'chat_groups.id')
             ->select(
@@ -394,7 +395,7 @@ class ChatService implements ChatServiceInterface
         // 查询已读消息,并将 num 字段设置为 0
         $readMessages = ChatRecords::where('user_id', $userId)
             ->where('is_read', 1)
-        // ->where('action', 'recieved')
+            // ->where('action', 'recieved')
             ->leftJoin('user', 'chat_records.receiver_id', '=', 'user.id')
             ->leftJoin('chat_groups', 'chat_records.receiver_id', '=', 'chat_groups.id')
             ->select(
@@ -420,7 +421,6 @@ class ChatService implements ChatServiceInterface
 
         foreach ($readMessages as $message) {
             if (strlen($message['receiver_id']) === 18) {
-
             } else {
                 // $allMessages[$message['receiver_id']] = $message->toArray();
             }
@@ -477,9 +477,9 @@ class ChatService implements ChatServiceInterface
             $result = ChatRecords::where(function ($query) use ($userId, $friendId) {
                 $query->where('user_id', $userId)->where('receiver_id', $friendId);
             })
-            // ->orWhere(function ($query) use ($userId, $friendId) {
-            //     $query->where('user_id', $friendId)->where('receiver_id', $userId);
-            // })
+                // ->orWhere(function ($query) use ($userId, $friendId) {
+                //     $query->where('user_id', $friendId)->where('receiver_id', $userId);
+                // })
                 ->leftJoin('user as u1', 'chat_records.user_id', '=', 'u1.id')
                 ->leftJoin('user as u2', 'chat_records.receiver_id', '=', 'u2.id')
                 ->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')
@@ -819,9 +819,13 @@ class ChatService implements ChatServiceInterface
         $result = ChatTopic::where($where)
             ->leftJoin('user', 'user.id', '=', 'chat_topics.user_id')
             ->leftJoin('chat_topics_reply', 'chat_topics.id', '=', 'chat_topics_reply.topic_id')
-            ->select('chat_topics.*', 'user.nickname', 'user.avatar', 'user.user_name'
-                ,
-                DB::raw('count(chat_topics_reply.id) as num'))
+            ->select(
+                'chat_topics.*',
+                'user.nickname',
+                'user.avatar',
+                'user.user_name',
+                DB::raw('count(chat_topics_reply.id) as num')
+            )
             ->groupBy('chat_topics.id')
             ->orderBy('chat_topics.id', 'desc')
             ->paginate($data['page_size'], ['*'], 'page', $data['page'] ?? 1);
@@ -836,10 +840,13 @@ class ChatService implements ChatServiceInterface
     public function addTopic(array $data): array
     {
         $chattopic = [];
+        Db::beginTransaction();
         try {
             $data['created_at'] = date('Y-m-d H:i:s');
             $data['updated_at'] = date('Y-m-d H:i:s');
             $result = ChatTopic::insertGetId($data);
+            var_dump($result, 'tedst111111111111111111111111111111');
+
             if ($result && $data['is_group'] == 1) {
                 //chat_group
                 $group_id = PublicData::uuid();
@@ -866,7 +873,7 @@ class ChatService implements ChatServiceInterface
             } else {
                 $chattopic = Chattopic::find($result);
             }
-            Db::beginTransaction();
+
             Db::commit();
         } catch (\Exception $e) {
             Db::rollBack();
@@ -949,7 +956,6 @@ class ChatService implements ChatServiceInterface
             ->select('chat_topics.*', 'user.nickname', 'user.avatar', 'user.user_name')
             ->first();
         return Result::success($result);
-
     }
     public function addReply(array $data): array
     {
@@ -1159,5 +1165,4 @@ class ChatService implements ChatServiceInterface
         $result = ChatTopicClass::where(['id' => $data['id']])->first();
         return Result::success($result);
     }
-
 }