WIN-2308041133\Administrator hace 1 mes
padre
commit
778f6b427e
Se han modificado 2 ficheros con 32 adiciones y 12 borrados
  1. 12 7
      app/api/controller/Chat.php
  2. 20 5
      app/model/api/ChatRecord.php

+ 12 - 7
app/api/controller/Chat.php

@@ -133,7 +133,8 @@ class Chat extends BaseController
     }
     
     /**
-     * 获取聊天记录
+     * 获取聊天记录(分页)
+     * page=1 为最新的一批消息,页码递增则获取更早的消息
      */
     public function recordList(Request $request)
     {
@@ -147,15 +148,19 @@ class Chat extends BaseController
             return app('json')->fail('对方用户ID错误');
         }
         
+        if ($page < 1) $page = 1;
+        if ($limit < 1) $limit = 20;
+        if ($limit > 100) $limit = 100;
+        
         $result = ChatRecord::getChatRecords($userId, $toUserId, $page, $limit);
         
-        // 处理消息类型文本
+        // 处理附加字段
         foreach ($result['list'] as &$item) {
             $item['type_text'] = ChatRecord::getTypeText($item['type']);
             $item['form_type_text'] = ChatRecord::getFormTypeText($item['form_type'] ?? 0);
-            // 判断是否是自己发送的
             $item['is_mine'] = $item['user_id'] == $userId;
         }
+        unset($item);
         
         return app('json')->success('获取成功', $result);
     }
@@ -172,8 +177,8 @@ class Chat extends BaseController
         if ($fromUserId <= 0) {
             return app('json')->fail('用户ID错误');
         }
-        @file_put_contents('quanju.txt',  $userId."-何意啊111\r\n", 8);
-        @file_put_contents('quanju.txt',  $fromUserId."-何意啊222\r\n", 8);
+//        @file_put_contents('quanju.txt',  $userId."-何意啊111\r\n", 8);
+//        @file_put_contents('quanju.txt',  $fromUserId."-何意啊222\r\n", 8);
 
         ChatRecord::markAsRead($userId, $fromUserId);
 
@@ -188,8 +193,8 @@ class Chat extends BaseController
         $data = $request->get();
         $fromUserId = $data['from_user_id'] ?? null;
         $userId = $request->user['uid'];
-        @file_put_contents('quanju.txt',  $userId."-何意啊111444\r\n", 8);
-        @file_put_contents('quanju.txt',  $fromUserId."-何意啊222555\r\n", 8);
+//        @file_put_contents('quanju.txt',  $userId."-何意啊111444\r\n", 8);
+//        @file_put_contents('quanju.txt',  $fromUserId."-何意啊222555\r\n", 8);
 
         // 处理空字符串等无效值
         if ($fromUserId !== null && $fromUserId !== '' && (int)$fromUserId > 0) {

+ 20 - 5
app/model/api/ChatRecord.php

@@ -57,15 +57,17 @@ class ChatRecord extends BaseModel
     }
     
     /**
-     * 获取与某用户的聊天记录
+     * 获取与某用户的聊天记录(分页,按时间正序返回)
      * @param int $userId 当前用户ID
      * @param int $toUserId 对方用户ID
-     * @param int $page 页码
+     * @param int $page 页码,第1页为最新消息
      * @param int $limit 每页数量
      * @return array
      */
     public static function getChatRecords(int $userId, int $toUserId, int $page = 1, int $limit = 20): array
     {
+        $db = \think\facade\Db::name('chat_record');
+        
         $where = function ($query) use ($userId, $toUserId) {
             $query->where(function ($q) use ($userId, $toUserId) {
                 $q->where('user_id', $userId)->where('to_user_id', $toUserId);
@@ -74,21 +76,34 @@ class ChatRecord extends BaseModel
             });
         };
         
-        $data = self::where($where)
+        // 总记录数
+        $total = $db->where($where)->count();
+        
+        // 分页参数矫正
+        $page = max(1, $page);
+        $limit = max(1, min($limit, 100)); // 限制每页最大100条
+        
+        // 按时间倒序查最新的一批,拿到后在 PHP 层面反转成正序返回给前端
+        $data = $db->where($where)
             ->order('create_time desc')
             ->page($page, $limit)
             ->select();
         
-        $total = self::where($where)->count();
+        $list = $data ? array_reverse($data->toArray()) : [];
         
         // 标记为已读
         self::markAsRead($userId, $toUserId);
         
+        $totalPages = (int)ceil($total / $limit);
+        $hasMore = $page < $totalPages;
+        
         return [
-            'list' => $data ? array_reverse($data->toArray()) : [],
+            'list' => $list,
             'total' => $total,
             'page' => $page,
             'limit' => $limit,
+            'total_pages' => $totalPages,
+            'has_more' => $hasMore,
         ];
     }