123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- <?php
- namespace App\Http\Controllers\Client;
- use App\Http\Controllers\Controller;
- use App\Services\Client\WalletService;
- use Auth;
- use Illuminate\Http\Request;
- /**
- * @group 用户端
- *
- * 钱包相关的API接口
- */
- class WalletController extends Controller
- {
- protected WalletService $service;
- public function __construct(WalletService $service)
- {
- $this->service = $service;
- }
- /**
- * [钱包管理]获取钱包明细
- *
- * 获取钱包明细,支持分页查询
- *
- * @authenticated
- *
- * @queryParam page int 当前页码. Example: 1
- * @queryParam per_page int 每页数量,默认10条. Example: 10
- *
- * @response {
- * "code": 200,
- * "message": "获取成功",
- * "data": {
- * "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(Request $request)
- {
- $perPage = $request->input('per_page', 10);
- return $this->success(
- $this->service->getWalletRecords(Auth::user()->id, $perPage)
- );
- }
- /**
- * [钱包管理]获取用户钱包
- *
- * 获取当前用户的钱包信息
- *
- * @authenticated
- *
- * @response {
- * "code": 200,
- * "message": "获取成功",
- * "data": {
- * "balance": 100.00,
- * "freeze": 0.00
- * }
- * }
- */
- public function wallet()
- {
- return $this->success(
- $this->service->getUserWallet(Auth::user()->id)
- );
- }
- /**
- * [钱包]申请提现
- *
- * @description 用户申请提现到指定账户
- *
- * @authenticated
- *
- * @bodyParam amount numeric required 提现金额(最低100元) Example: 100.00
- * @bodyParam withdraw_type integer required 提现方式(1:微信 2:支付宝 3:银行卡) Example: 1
- * @bodyParam withdraw_account string required 提现账号 Example: example@alipay.com
- * @bodyParam withdraw_account_name string required 提现账户名称 Example: 张三
- *
- * @response {
- * "message": "提现申请已提交",
- * "trans_no": "W202403211234567890",
- * "amount": "100.00",
- * "status": "处理中"
- * }
- * @response 422 {
- * "message": "可提现余额不足"
- * }
- */
- public function withdraw(Request $request)
- {
- // 验证基本参数
- $data = $request->validate([
- 'amount' => 'required|numeric|min:100',
- 'withdraw_type' => 'required|integer|in:1,2,3',
- 'withdraw_account' => 'required|string|max:100',
- 'withdraw_account_name' => 'required|string|max:50',
- ]);
- // 根据提现方式验证账号格式
- switch ($data['withdraw_type']) {
- case 1: // 微信
- $request->validate([
- 'withdraw_account' => [
- 'required',
- 'string',
- 'regex:/^[a-zA-Z][a-zA-Z\d_-]{5,19}$/',
- 'max:20',
- ],
- ], [
- 'withdraw_account.regex' => '微信号格式不正确',
- ]);
- break;
- case 2: // 支付宝
- $request->validate([
- 'withdraw_account' => [
- 'required',
- 'string',
- function ($attribute, $value, $fail) {
- if (! filter_var($value, FILTER_VALIDATE_EMAIL) && ! preg_match('/^1[3-9]\d{9}$/', $value)) {
- $fail('支付宝账号必须是有效的邮箱或手机号');
- }
- },
- ],
- ]);
- break;
- case 3: // 银行卡
- $request->validate([
- 'withdraw_account' => [
- 'required',
- 'string',
- 'regex:/^\d{16,19}$/',
- ],
- ], [
- 'withdraw_account.regex' => '银行卡号格式不正确',
- ]);
- break;
- }
- // 验证提现账户名称
- abort_if(mb_strlen($data['withdraw_account_name']) < 2, 422, '提现账户名称至少2个字符');
- abort_if(! preg_match('/^[\x{4e00}-\x{9fa5}a-zA-Z\s]+$/u', $data['withdraw_account_name']), 422, '提现账户名称只能包含中文、英文字母和空格');
- return $this->success($this->service->withdraw(Auth::user()->id, $data));
- }
- }
|