WalletController.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. * @response {
  27. * "code": 200,
  28. * "message": "获取成功",
  29. * "data": {
  30. * "records": []
  31. * }
  32. * }
  33. */
  34. public function records()
  35. {
  36. return $this->service->getWalletRecords(Auth::user()->id, request('per_page', 10));
  37. }
  38. /**
  39. * [钱包管理]获取用户钱包
  40. *
  41. * 获取当前用户的钱包信息
  42. *
  43. * @authenticated
  44. *
  45. * @response {
  46. * "code": 200,
  47. * "message": "获取成功",
  48. * "data": {
  49. * "balance": 100.00,
  50. * "freeze": 0.00
  51. * }
  52. * }
  53. */
  54. public function wallet()
  55. {
  56. return $this->service->getUserWallet(Auth::user()->id);
  57. }
  58. /**
  59. * [钱包]申请提现
  60. *
  61. * @description 用户申请提现到指定账户
  62. *
  63. * @authenticated
  64. *
  65. * @bodyParam amount numeric required 提现金额(最低100元) Example: 100.00
  66. * @bodyParam withdraw_type integer required 提现方式(1:微信 2:支付宝 3:银行卡) Example: 1
  67. * @bodyParam withdraw_account string required 提现账号 Example: example@alipay.com
  68. * @bodyParam withdraw_account_name string required 提现账户名称 Example: 张三
  69. *
  70. * @response {
  71. * "message": "提现申请已提交",
  72. * "trans_no": "W202403211234567890",
  73. * "amount": "100.00",
  74. * "status": "处理中"
  75. * }
  76. * @response 422 {
  77. * "message": "可提现余额不足"
  78. * }
  79. */
  80. public function withdraw(Request $request)
  81. {
  82. // 验证基本参数
  83. $data = $request->validate([
  84. 'amount' => 'required|numeric|min:100',
  85. 'withdraw_type' => 'required|integer|in:1,2,3',
  86. 'withdraw_account' => 'required|string|max:100',
  87. 'withdraw_account_name' => 'required|string|max:50',
  88. ]);
  89. // 根据提现方式验证账号格式
  90. switch ($data['withdraw_type']) {
  91. case 1: // 微信
  92. $request->validate([
  93. 'withdraw_account' => [
  94. 'required',
  95. 'string',
  96. 'regex:/^[a-zA-Z][a-zA-Z\d_-]{5,19}$/',
  97. 'max:20',
  98. ],
  99. ], [
  100. 'withdraw_account.regex' => '微信号格式不正确',
  101. ]);
  102. break;
  103. case 2: // 支付宝
  104. $request->validate([
  105. 'withdraw_account' => [
  106. 'required',
  107. 'string',
  108. function ($attribute, $value, $fail) {
  109. if (! filter_var($value, FILTER_VALIDATE_EMAIL) && ! preg_match('/^1[3-9]\d{9}$/', $value)) {
  110. $fail('支付宝账号必须是有效的邮箱或手机号');
  111. }
  112. },
  113. ],
  114. ]);
  115. break;
  116. case 3: // 银行卡
  117. $request->validate([
  118. 'withdraw_account' => [
  119. 'required',
  120. 'string',
  121. 'regex:/^\d{16,19}$/',
  122. ],
  123. ], [
  124. 'withdraw_account.regex' => '银行卡号格式不正确',
  125. ]);
  126. break;
  127. }
  128. // 验证提现账户名称
  129. abort_if(mb_strlen($data['withdraw_account_name']) < 2, 422, '提现账户名称至少2个字符');
  130. abort_if(! preg_match('/^[\x{4e00}-\x{9fa5}a-zA-Z\s]+$/u', $data['withdraw_account_name']), 422, '提现账户名称只能包含中文、英文字母和空格');
  131. return $this->service->withdraw(Auth::user()->id, $data);
  132. }
  133. }