소스 검색

fixed:用户端-钱包明细(分页格式)

刘学玺 4 달 전
부모
커밋
fba99aa16d
2개의 변경된 파일27개의 추가작업 그리고 8개의 파일을 삭제
  1. 19 4
      app/Http/Controllers/Client/WalletController.php
  2. 8 4
      app/Services/Client/WalletService.php

+ 19 - 4
app/Http/Controllers/Client/WalletController.php

@@ -24,22 +24,37 @@ class WalletController extends Controller
     /**
      * [钱包管理]获取钱包明细
      *
-     * 获取钱包明细
+     * 获取钱包明细,支持分页查询
      *
      * @authenticated
      *
+     * @queryParam page int 当前页码. Example: 1
+     * @queryParam per_page int 每页数量,默认10条. Example: 10
+     *
      * @response {
      *   "code": 200,
      *   "message": "获取成功",
      *   "data": {
-     *     "records": []
+     *     "items": [
+     *       {
+     *         "id": 1,
+     *         "type": "withdraw",
+     *         "amount": "-100.00",
+     *         "balance": "900.00",
+     *         "remark": "提现",
+     *         "created_at": "2024-03-21 10:00:00"
+     *       }
+     *     ],
+     *     "total": 1
      *   }
      * }
      */
-    public function records()
+    public function records(Request $request)
     {
+        $perPage = $request->input('per_page', 10);
+
         return $this->success(
-            $this->service->getWalletRecords(Auth::user()->id, request('per_page', 10))
+            $this->service->getWalletRecords(Auth::user()->id, $perPage)
         );
     }
 

+ 8 - 4
app/Services/Client/WalletService.php

@@ -18,7 +18,7 @@ class WalletService
      *
      * @param  int  $userId  用户ID
      * @param  int  $perPage  每页记录数
-     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
+     * @return array 包含 items 和 total 的数组
      *
      * @throws \Exception
      */
@@ -34,11 +34,15 @@ class WalletService
             abort_if($user->state != UserStatus::OPEN->value, 400, '用户状态异常');
 
             // 获取钱包交易记录
-            $records = $user->wallet->transRecords()
+            $paginator = $user->wallet->transRecords()
                 ->orderBy('created_at', 'desc')
                 ->paginate($perPage);
 
-            return $records;
+            // 转换为统一的分页格式
+            return [
+                'items' => $paginator->items(),
+                'total' => $paginator->total(),
+            ];
         } catch (\Exception $e) {
             // 记录错误日志
             \Log::error('获取钱包明细失败', [
@@ -103,7 +107,7 @@ class WalletService
                 abort_if($amount > $wallet->available_balance, 422, '可提现余额不足');
 
                 // 生成交易流水号
-                $transNo = 'CW' . date('YmdHis') . mt_rand(1000, 9999);
+                $transNo = 'CW'.date('YmdHis').mt_rand(1000, 9999);
 
                 // 创建提现记录
                 $withdraw = WalletWithdrawRecord::create([