request->param(); // 使用验证器验证分页参数 $validate = new WithdrawValidate(); if (!$validate->scene('index')->check($params)) { $this->error($validate->getError()); } $user = auth_user(); // 使用验证后的参数 $page_size = isset($params['page_size']) ? (int)$params['page_size'] : 10; $withdraws = WithdrawModel::where(['user_id' => $user->id]) ->order('id desc') ->paginate($page_size)->each(function ($withdraw) { $withdraw->hidden(['withdraw_info']); }); $this->success('获取成功', $withdraws); } // 提现规则 public function rules() { $user = auth_user(); $config = (new WithdrawService($user))->config; $this->success('提现规则', $config); } // 发起提现请求 public function apply() { $user = auth_user(); $params = $this->request->param(); // 使用验证器验证提现参数:type, amount, account_id $validate = new WithdrawValidate(); if (!$validate->scene('apply')->check($params)) { $this->error($validate->getError()); } $withdrawService = new WithdrawService($user); // 申请提现 - 使用事务确保数据一致性 Db::startTrans(); try { $withdraw = $withdrawService->apply($params); Db::commit(); } catch (HttpResponseException $e) { Db::rollback(); $data = $e->getResponse()->getData(); $message = $data ? ($data['msg'] ?? '') : $e->getMessage(); $this->error($message); } catch (\Exception $e) { Db::rollback(); $this->error($e->getMessage()); } // 微信新版本商家转账 if ($withdraw->withdraw_type === 'wechat') { try { $transferData = $withdrawService->handleWechatTransfer($withdraw); } catch (HttpResponseException $e) { $data = $e->getResponse()->getData(); $message = $data ? ($data['msg'] ?? '') : $e->getMessage(); $this->error($message); } catch (\Exception $e) { $this->error($e->getMessage()); } } // 支付宝提现+自动打款 if ($withdraw->withdraw_type === 'alipay' && $withdrawService->config['auto_arrival']) { try { // 记录提现日志 $withdrawService->handleAlipayWithdraw($withdraw); } catch (HttpResponseException $e) { $data = $e->getResponse()->getData(); $message = $data ? ($data['msg'] ?? '') : $e->getMessage(); $this->error($message); } catch (\Exception $e) { $this->error($e->getMessage()); } } $this->success('申请成功', [ 'id' => $withdraw->id, 'type' => $withdraw->withdraw_type, 'withdraw_sn' => $withdraw->withdraw_sn, 'transfer_data' => $transferData ?? null, ]); } // 继续提现(仅支持微信商家转账) public function transfer() { $user = auth_user(); $params = $this->request->param(); // 使用验证器验证转账参数:type, withdraw_sn $validate = new WithdrawValidate(); if (!$validate->scene('transfer')->check($params)) { $this->error($validate->getError()); } $withdrawService = new WithdrawService($user); try { // 如果 微信提现, result 为 package_info,其他的为 withdrawModel 对象 $result = $withdrawService->retry($params); $this->success('操作成功', $result); } catch (\Exception $e) { $this->error($e->getMessage()); } } // 取消提现(仅支持微信商家转账) public function cancel() { $user = auth_user(); $params = $this->request->param(); // 使用验证器验证取消参数:type, withdraw_sn $validate = new WithdrawValidate(); if (!$validate->scene('cancel')->check($params)) { $this->error($validate->getError()); } $withdrawService = new WithdrawService($user); try { $result = $withdrawService->cancel($params); $this->success('操作成功', $result); } catch (\Exception $e) { $this->error($e->getMessage()); } } // 重试提现(仅支持微信商家转账) public function retry() { $user = auth_user(); $params = $this->request->param(); // 使用验证器验证重试参数:type, withdraw_sn $validate = new WithdrawValidate(); if (!$validate->scene('retry')->check($params)) { $this->error($validate->getError()); } $withdrawService = new WithdrawService($user); try { $withdraw = $withdrawService->retry($params); $transferData = $withdrawService->handleWechatTransfer($withdraw); $this->success('申请成功', [ 'id' => $withdraw->id, 'type' => $withdraw->withdraw_type, 'withdraw_sn' => $withdraw->withdraw_sn, 'transfer_data' => $transferData ?? null, ]); } catch (\Exception $e) { $this->error($e->getMessage()); } } }