123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- <?php
- namespace app\api\controller;
- use think\Db;
- use think\exception\HttpResponseException;
- use app\common\Service\Withdraw as WithdrawService;
- use app\api\validate\Withdraw as WithdrawValidate;
- class Withdraw extends Base
- {
- protected $noNeedLogin = [];
- protected $noNeedRight = ['*'];
- public function index()
- {
- $params = $this->request->param();
-
- // 使用验证器验证查询参数:status, page, page_size
- $validate = new WithdrawValidate();
- if (!$validate->scene('index')->check($params)) {
- $this->error($validate->getError());
- }
-
- $user = auth_user();
- $withdrawService = new WithdrawService($user);
-
- // 通过服务获取提现记录列表
- $withdraws = $withdrawService->getWithdrawList($params);
- $this->success('获取成功', $withdraws);
- }
- // 提现规则
- public function rules()
- {
- $user = auth_user();
- $config = (new WithdrawService($user))->config;
- $this->success('提现规则', $config);
- }
- // 提现状态列表
- public function statusList()
- {
- $user = auth_user();
- $withdrawService = new WithdrawService($user);
- $statusList = $withdrawService->getStatusList();
- $this->success('状态列表', $statusList);
- }
- // 发起提现请求
- 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());
- }
- }
- }
|