Withdrawal.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. namespace app\api\controller\invitation;
  3. use think\facade\Db;
  4. use app\common\controller\Frontend;
  5. use app\common\service\CommissionService;
  6. class Withdrawal extends Frontend
  7. {
  8. protected $commissionService;
  9. public function initialize(): void
  10. {
  11. parent::initialize();
  12. $this->commissionService = new CommissionService();
  13. }
  14. /**
  15. * 申请提现
  16. */
  17. public function apply(): void
  18. {
  19. if (!$this->request->isPost()) {
  20. $this->error('非法请求');
  21. }
  22. $userId = $this->auth->id;
  23. $amount = $this->request->post('amount', 0);
  24. $withdrawalMethod = $this->request->post('withdrawal_method', 'wechat');
  25. $accountInfo = $this->request->post('account_info', '');
  26. $remark = $this->request->post('remark', '');
  27. if ($amount <= 0) {
  28. $this->error('提现金额必须大于0');
  29. }
  30. if (!in_array($withdrawalMethod, ['wechat', 'alipay', 'bank'])) {
  31. $this->error('提现方式错误');
  32. }
  33. try {
  34. // 检查用户佣金是否足够
  35. $commission = Db::name('ba_user_commission')
  36. ->where('user_id', $userId)
  37. ->find();
  38. if (!$commission || $commission['current_commission'] < $amount) {
  39. $this->error('佣金余额不足');
  40. }
  41. // 生成提现单号
  42. $withdrawalNo = 'TX' . date('YmdHis') . str_pad(mt_rand(1, 9999), 4, '0', STR_PAD_LEFT);
  43. // 创建提现记录
  44. $data = [
  45. 'withdrawal_no' => $withdrawalNo,
  46. 'user_id' => $userId,
  47. 'amount' => $amount,
  48. 'withdrawal_method' => $withdrawalMethod,
  49. 'account_info' => $accountInfo,
  50. 'current_balance' => $commission['current_commission'],
  51. 'balance_after' => $commission['current_commission'] - $amount,
  52. 'status' => 0, // 待审核
  53. 'remark' => $remark,
  54. 'create_time' => date('Y-m-d H:i:s'),
  55. 'update_time' => date('Y-m-d H:i:s')
  56. ];
  57. $result = Db::name('ba_withdrawal_record')->insert($data);
  58. if ($result) {
  59. $this->success('提现申请提交成功,等待审核', [
  60. 'withdrawal_no' => $withdrawalNo
  61. ]);
  62. } else {
  63. $this->error('提现申请失败');
  64. }
  65. } catch (\Throwable $e) {
  66. $this->error('申请失败:' . $e->getMessage());
  67. }
  68. }
  69. /**
  70. * 获取提现记录
  71. */
  72. public function records(): void
  73. {
  74. $userId = $this->auth->id;
  75. $page = $this->request->get('page', 1);
  76. $pageSize = $this->request->get('pageSize', 10);
  77. try {
  78. $list = Db::name('ba_withdrawal_record')
  79. ->where('user_id', $userId)
  80. ->order('create_time', 'desc')
  81. ->paginate([
  82. 'page' => $page,
  83. 'list_rows' => $pageSize
  84. ]);
  85. $this->success('', [
  86. 'list' => $list->items(),
  87. 'total' => $list->total(),
  88. 'page' => $list->currentPage(),
  89. 'page_size' => $list->listRows()
  90. ]);
  91. } catch (\Throwable $e) {
  92. $this->error('获取提现记录失败');
  93. }
  94. }
  95. /**
  96. * 审核提现(管理员用)
  97. */
  98. public function audit(): void
  99. {
  100. if (!$this->auth->isLogin() || !$this->auth->isAdmin()) {
  101. $this->error('权限不足');
  102. }
  103. $withdrawalId = $this->request->post('withdrawal_id', 0);
  104. $status = $this->request->post('status', 0);
  105. $auditRemark = $this->request->post('audit_remark', '');
  106. if ($withdrawalId <= 0) {
  107. $this->error('参数错误');
  108. }
  109. if (!in_array($status, [1, 2])) { // 1=通过,2=拒绝
  110. $this->error('状态错误');
  111. }
  112. try {
  113. if ($status == 1) {
  114. // 审核通过,处理提现
  115. $result = $this->commissionService->processWithdrawal($withdrawalId);
  116. } else {
  117. // 审核拒绝
  118. $result = Db::name('ba_withdrawal_record')
  119. ->where('id', $withdrawalId)
  120. ->update([
  121. 'status' => 2, // 审核拒绝
  122. 'audit_time' => date('Y-m-d H:i:s'),
  123. 'audit_remark' => $auditRemark,
  124. 'update_time' => date('Y-m-d H:i:s')
  125. ]);
  126. $result = $result !== false;
  127. }
  128. if ($result) {
  129. $this->success('审核操作成功');
  130. } else {
  131. $this->error('审核操作失败');
  132. }
  133. } catch (\Throwable $e) {
  134. $this->error('审核失败:' . $e->getMessage());
  135. }
  136. }
  137. }