Withdraw.php 6.2 KB

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