Withdraw.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. use app\api\validate\Withdraw as WithdrawValidate;
  8. class Withdraw extends Base
  9. {
  10. protected $noNeedLogin = [];
  11. protected $noNeedRight = ['*'];
  12. public function index()
  13. {
  14. $params = $this->request->param();
  15. // 使用验证器验证分页参数
  16. $validate = new WithdrawValidate();
  17. if (!$validate->scene('index')->check($params)) {
  18. $this->error($validate->getError());
  19. }
  20. $user = auth_user();
  21. // 使用验证后的参数
  22. $page_size = isset($params['page_size']) ? (int)$params['page_size'] : 10;
  23. $withdraws = WithdrawModel::where(['user_id' => $user->id])
  24. ->order('id desc')
  25. ->paginate($page_size)->each(function ($withdraw) {
  26. $withdraw->hidden(['withdraw_info']);
  27. });
  28. $this->success('获取成功', $withdraws);
  29. }
  30. // 提现规则
  31. public function rules()
  32. {
  33. $user = auth_user();
  34. $config = (new WithdrawService($user))->config;
  35. $this->success('提现规则', $config);
  36. }
  37. // 发起提现请求
  38. public function apply()
  39. {
  40. $user = auth_user();
  41. $params = $this->request->param();
  42. // 使用验证器验证提现参数:type, amount, account_id
  43. $validate = new WithdrawValidate();
  44. if (!$validate->scene('apply')->check($params)) {
  45. $this->error($validate->getError());
  46. }
  47. $withdrawService = new WithdrawService($user);
  48. // 申请提现 - 使用事务确保数据一致性
  49. Db::startTrans();
  50. try {
  51. $withdraw = $withdrawService->apply($params);
  52. Db::commit();
  53. } catch (HttpResponseException $e) {
  54. Db::rollback();
  55. $data = $e->getResponse()->getData();
  56. $message = $data ? ($data['msg'] ?? '') : $e->getMessage();
  57. $this->error($message);
  58. } catch (\Exception $e) {
  59. Db::rollback();
  60. $this->error($e->getMessage());
  61. }
  62. // 微信新版本商家转账
  63. if ($withdraw->withdraw_type === 'wechat') {
  64. try {
  65. $transferData = $withdrawService->handleWechatTransfer($withdraw);
  66. } catch (HttpResponseException $e) {
  67. $data = $e->getResponse()->getData();
  68. $message = $data ? ($data['msg'] ?? '') : $e->getMessage();
  69. $this->error($message);
  70. } catch (\Exception $e) {
  71. $this->error($e->getMessage());
  72. }
  73. }
  74. // 支付宝提现+自动打款
  75. if ($withdraw->withdraw_type === 'alipay' && $withdrawService->config['auto_arrival']) {
  76. try {
  77. // 记录提现日志
  78. $withdrawService->handleAlipayWithdraw($withdraw);
  79. } catch (HttpResponseException $e) {
  80. $data = $e->getResponse()->getData();
  81. $message = $data ? ($data['msg'] ?? '') : $e->getMessage();
  82. $this->error($message);
  83. } catch (\Exception $e) {
  84. $this->error($e->getMessage());
  85. }
  86. }
  87. $this->success('申请成功', [
  88. 'id' => $withdraw->id,
  89. 'type' => $withdraw->withdraw_type,
  90. 'withdraw_sn' => $withdraw->withdraw_sn,
  91. 'transfer_data' => $transferData ?? null,
  92. ]);
  93. }
  94. // 继续提现(仅支持微信商家转账)
  95. public function transfer()
  96. {
  97. $user = auth_user();
  98. $params = $this->request->param();
  99. // 使用验证器验证转账参数:type, withdraw_sn
  100. $validate = new WithdrawValidate();
  101. if (!$validate->scene('transfer')->check($params)) {
  102. $this->error($validate->getError());
  103. }
  104. $withdrawService = new WithdrawService($user);
  105. try {
  106. // 如果 微信提现, result 为 package_info,其他的为 withdrawModel 对象
  107. $result = $withdrawService->retry($params);
  108. $this->success('操作成功', $result);
  109. } catch (\Exception $e) {
  110. $this->error($e->getMessage());
  111. }
  112. }
  113. // 取消提现(仅支持微信商家转账)
  114. public function cancel()
  115. {
  116. $user = auth_user();
  117. $params = $this->request->param();
  118. // 使用验证器验证取消参数:type, withdraw_sn
  119. $validate = new WithdrawValidate();
  120. if (!$validate->scene('cancel')->check($params)) {
  121. $this->error($validate->getError());
  122. }
  123. $withdrawService = new WithdrawService($user);
  124. try {
  125. $result = $withdrawService->cancel($params);
  126. $this->success('操作成功', $result);
  127. } catch (\Exception $e) {
  128. $this->error($e->getMessage());
  129. }
  130. }
  131. // 重试提现(仅支持微信商家转账)
  132. public function retry()
  133. {
  134. $user = auth_user();
  135. $params = $this->request->param();
  136. // 使用验证器验证重试参数:type, withdraw_sn
  137. $validate = new WithdrawValidate();
  138. if (!$validate->scene('retry')->check($params)) {
  139. $this->error($validate->getError());
  140. }
  141. $withdrawService = new WithdrawService($user);
  142. try {
  143. $withdraw = $withdrawService->retry($params);
  144. $transferData = $withdrawService->handleWechatTransfer($withdraw);
  145. $this->success('申请成功', [
  146. 'id' => $withdraw->id,
  147. 'type' => $withdraw->withdraw_type,
  148. 'withdraw_sn' => $withdraw->withdraw_sn,
  149. 'transfer_data' => $transferData ?? null,
  150. ]);
  151. } catch (\Exception $e) {
  152. $this->error($e->getMessage());
  153. }
  154. }
  155. }