Withdraw.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. namespace app\api\controller;
  3. use think\Db;
  4. use think\exception\HttpResponseException;
  5. use app\common\model\Withdraw as WithdrawModel;
  6. use app\common\Service\Withdraw as WithdrawService;
  7. class Withdraw extends Base
  8. {
  9. protected $noNeedLogin = [];
  10. protected $noNeedRight = ['*'];
  11. public function index()
  12. {
  13. $params = $this->request->param();
  14. // 验证分页参数
  15. $this->validate($params, 'Withdraw.index');
  16. $user = auth_user();
  17. $withdraws = WithdrawModel::where(['user_id' => $user->id])
  18. ->order('id desc')
  19. ->paginate($this->request->param('page_size', 10))->each(function ($withdraw) {
  20. $withdraw->hidden(['withdraw_info']);
  21. });
  22. $this->success('获取成功', $withdraws);
  23. }
  24. // 提现规则
  25. public function rules()
  26. {
  27. $user = auth_user();
  28. $config = (new WithdrawService($user))->config;
  29. $this->success('提现规则', $config);
  30. }
  31. // 发起提现请求
  32. public function apply()
  33. {
  34. $user = auth_user();
  35. $params = $this->request->param();
  36. $this->validate($params, 'Withdraw.apply');
  37. $withdrawService = new WithdrawService($user);
  38. // 申请提现
  39. Db::startTrans();
  40. try {
  41. $withdraw = $withdrawService->apply($params);
  42. Db::commit();
  43. } catch (HttpResponseException $e) {
  44. $data = $e->getResponse()->getData();
  45. $message = $data ? ($data['msg'] ?? '') : $e->getMessage();
  46. $this->error($message);
  47. } catch (\Exception $e) {
  48. Db::rollback();
  49. $this->error($e->getMessage());
  50. }
  51. // 微信新版本商家转账
  52. if ($withdraw->withdraw_type === 'wechat') {
  53. try {
  54. $transferData = $withdrawService->handleWechatTransfer($withdraw);
  55. } catch (HttpResponseException $e) {
  56. $data = $e->getResponse()->getData();
  57. $message = $data ? ($data['msg'] ?? '') : $e->getMessage();
  58. $this->error($message);
  59. } catch (\Exception $e) {
  60. $this->error($e->getMessage());
  61. }
  62. }
  63. // 支付宝提现+自动打款
  64. if ($withdraw->withdraw_type === 'alipay' && $withdrawService->config['auto_arrival']) {
  65. try {
  66. // 记录提现日志
  67. $withdrawService->handleAlipayWithdraw($withdraw);
  68. } catch (HttpResponseException $e) {
  69. $data = $e->getResponse()->getData();
  70. $message = $data ? ($data['msg'] ?? '') : $e->getMessage();
  71. $this->error($message);
  72. } catch (\Exception $e) {
  73. $this->error($e->getMessage());
  74. }
  75. }
  76. $this->success('申请成功', [
  77. 'id' => $withdraw->id,
  78. 'type' => $withdraw->withdraw_type,
  79. 'withdraw_sn' => $withdraw->withdraw_sn,
  80. 'transfer_data' => $transferData ?? null,
  81. ]);
  82. }
  83. // 继续提现(仅支持微信商家转账)
  84. public function transfer()
  85. {
  86. $user = auth_user();
  87. $params = $this->request->param();
  88. $this->validate($params, 'Withdraw.transfer');
  89. $withdrawService = new WithdrawService($user);
  90. // 如果 微信提现, result 为 package_info,其他的为 withdrawModel 对象
  91. $result = $withdrawService->retry($params);
  92. $this->success('操作成功', $result);
  93. }
  94. // 取消提现(仅支持微信商家转账)
  95. public function cancel()
  96. {
  97. $user = auth_user();
  98. $params = $this->request->param();
  99. $this->validate($params, 'Withdraw.cancel');
  100. $withdrawService = new WithdrawService($user);
  101. try {
  102. $result = $withdrawService->cancel($params);
  103. } catch (\Exception $e) {
  104. $this->error($e->getMessage());
  105. }
  106. $this->success('操作成功', $result);
  107. }
  108. // 取消提现(仅支持微信商家转账)
  109. public function retry()
  110. {
  111. $user = auth_user();
  112. $params = $this->request->param();
  113. $this->validate($params, 'Withdraw.retry');
  114. $withdrawService = new WithdrawService($user);
  115. try {
  116. $withdraw = $withdrawService->retry($params);
  117. $transferData = $withdrawService->handleWechatTransfer($withdraw);
  118. } catch (\Exception $e) {
  119. $this->error($e->getMessage());
  120. }
  121. $this->success('申请成功', [
  122. 'id' => $withdraw->id,
  123. 'type' => $withdraw->withdraw_type,
  124. 'withdraw_sn' => $withdraw->withdraw_sn,
  125. 'transfer_data' => $transferData ?? null,
  126. ]);
  127. }
  128. }