WalletController.php 4.3 KB

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