bianjunhui vor 1 Woche
Ursprung
Commit
c88cc4ae2c

+ 19 - 1
packages/Longyi/RewardPoints/src/Helpers/ApiResponse.php

@@ -1,6 +1,7 @@
 <?php
 
 namespace Longyi\RewardPoints\Helpers;
+use Illuminate\Pagination\LengthAwarePaginator;
 
 class ApiResponse
 {
@@ -15,7 +16,24 @@ class ApiResponse
             'data' => $data
         ], $code);
     }
-
+    /**
+     * 带分页的成功响应
+     */
+    public static function paginated($data, LengthAwarePaginator $paginator, string $message = 'Success', int $code = 200)
+    {
+        return response()->json([
+            'success' => true,
+            'message' => $message,
+            'data' => $data,
+            'pagination' => [
+                'current_page' => $paginator->currentPage(),
+                'per_page' => $paginator->perPage(),
+                'total' => $paginator->total(),
+                'last_page' => $paginator->lastPage(),
+                'has_more' => $paginator->hasMorePages()
+            ]
+        ], $code);
+    }
     /**
      * 错误响应
      */

+ 7 - 12
packages/Longyi/RewardPoints/src/Http/Controllers/RewardPointsController.php

@@ -226,18 +226,13 @@ class RewardPointsController extends Controller
                 'created_at' => Carbon::parse($item->transaction_time)->format('Y-m-d H:i:s'),
             ];
         });
-
-        return ApiResponse::success([
-            'current_points' => $this->rewardPointRepository->getCustomerPoints($customer->id),
-            'history' => $transformedHistory,
-            'pagination' => [
-                'current_page' => $history->currentPage(),
-                'per_page' => $history->perPage(),
-                'total' => $history->total(),
-                'last_page' => $history->lastPage(),
-                'has_more' => $history->hasMorePages()
-            ]
-        ]);
+        return ApiResponse::paginated(
+            [
+                'current_points' => $this->rewardPointRepository->getCustomerPoints($customer->id),
+                'history' => $transformedHistory
+            ],
+            $history
+        );
     }
 
     /**