WalletController.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. namespace App\Http\Controllers\Client;
  3. use App\Http\Controllers\Controller;
  4. use App\Services\Client\WalletService;
  5. use Auth;
  6. use Illuminate\Http\Request;
  7. /**
  8. * @group 用户端
  9. *
  10. * 钱包相关的API接口
  11. */
  12. class WalletController extends Controller
  13. {
  14. protected WalletService $service;
  15. public function __construct(WalletService $service)
  16. {
  17. $this->service = $service;
  18. }
  19. /**
  20. * [钱包管理]获取钱包明细
  21. *
  22. * 获取钱包明细,支持分页查询
  23. *
  24. * @authenticated
  25. *
  26. * @queryParam page int 当前页码. Example: 1
  27. * @queryParam per_page int 每页数量,默认10条. Example: 10
  28. *
  29. * @response {
  30. * "code": 200,
  31. * "message": "获取成功",
  32. * "data": {
  33. * "items": [
  34. * {
  35. * "id": 1,
  36. * "type": "withdraw",
  37. * "amount": "-100.00",
  38. * "balance": "900.00",
  39. * "remark": "提现",
  40. * "created_at": "2024-03-21 10:00:00"
  41. * }
  42. * ],
  43. * "total": 1
  44. * }
  45. * }
  46. */
  47. public function records(Request $request)
  48. {
  49. $perPage = $request->input('per_page', 10);
  50. return $this->success(
  51. $this->service->getWalletRecords(Auth::user()->id, $perPage)
  52. );
  53. }
  54. /**
  55. * [钱包管理]获取用户钱包
  56. *
  57. * 获取当前用户的钱包信息
  58. *
  59. * @authenticated
  60. *
  61. * @response {
  62. * "code": 200,
  63. * "message": "获取成功",
  64. * "data": {
  65. * "balance": 100.00,
  66. * "freeze": 0.00
  67. * }
  68. * }
  69. */
  70. public function wallet()
  71. {
  72. return $this->success(
  73. $this->service->getUserWallet(Auth::user()->id)
  74. );
  75. }
  76. /**
  77. * [钱包]申请提现
  78. *
  79. * @description 用户申请提现到指定账户
  80. *
  81. * @authenticated
  82. *
  83. * @bodyParam amount numeric required 提现金额(最低100元) Example: 100.00
  84. * @bodyParam withdraw_type integer required 提现方式(1:微信 2:支付宝 3:银行卡) Example: 1
  85. * @bodyParam withdraw_account string required 提现账号 Example: example@alipay.com
  86. * @bodyParam withdraw_account_name string required 提现账户名称 Example: 张三
  87. *
  88. * @response {
  89. * "message": "提现申请已提交",
  90. * "trans_no": "W202403211234567890",
  91. * "amount": "100.00",
  92. * "status": "处理中"
  93. * }
  94. * @response 422 {
  95. * "message": "可提现余额不足"
  96. * }
  97. */
  98. public function withdraw(Request $request)
  99. {
  100. // 验证基本参数
  101. $data = $request->validate([
  102. 'amount' => 'required|numeric|min:100',
  103. 'withdraw_type' => 'required|integer|in:1,2,3',
  104. 'withdraw_account' => 'required|string|max:100',
  105. 'withdraw_account_name' => 'required|string|max:50',
  106. ]);
  107. // 根据提现方式验证账号格式
  108. switch ($data['withdraw_type']) {
  109. case 1: // 微信
  110. $request->validate([
  111. 'withdraw_account' => [
  112. 'required',
  113. 'string',
  114. 'regex:/^[a-zA-Z][a-zA-Z\d_-]{5,19}$/',
  115. 'max:20',
  116. ],
  117. ], [
  118. 'withdraw_account.regex' => '微信号格式不正确',
  119. ]);
  120. break;
  121. case 2: // 支付宝
  122. $request->validate([
  123. 'withdraw_account' => [
  124. 'required',
  125. 'string',
  126. function ($attribute, $value, $fail) {
  127. if (! filter_var($value, FILTER_VALIDATE_EMAIL) && ! preg_match('/^1[3-9]\d{9}$/', $value)) {
  128. $fail('支付宝账号必须是有效的邮箱或手机号');
  129. }
  130. },
  131. ],
  132. ]);
  133. break;
  134. case 3: // 银行卡
  135. $request->validate([
  136. 'withdraw_account' => [
  137. 'required',
  138. 'string',
  139. 'regex:/^\d{16,19}$/',
  140. ],
  141. ], [
  142. 'withdraw_account.regex' => '银行卡号格式不正确',
  143. ]);
  144. break;
  145. }
  146. // 验证提现账户名称
  147. abort_if(mb_strlen($data['withdraw_account_name']) < 2, 422, '提现账户名称至少2个字符');
  148. abort_if(! preg_match('/^[\x{4e00}-\x{9fa5}a-zA-Z\s]+$/u', $data['withdraw_account_name']), 422, '提现账户名称只能包含中文、英文字母和空格');
  149. return $this->success($this->service->withdraw(Auth::user()->id, $data));
  150. }
  151. }