Transfer.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. namespace app\data\controller\api\auth;
  3. use app\data\controller\api\Auth;
  4. use app\data\model\DataUserTransfer;
  5. use app\data\service\UserRebateService;
  6. use app\data\service\UserTransferService;
  7. use think\admin\extend\CodeExtend;
  8. /**
  9. * 用户提现接口
  10. * Class Transfer
  11. * @package app\data\controller\api\auth
  12. */
  13. class Transfer extends Auth
  14. {
  15. /**
  16. * 提交提现处理
  17. * @throws \think\db\exception\DataNotFoundException
  18. * @throws \think\db\exception\DbException
  19. * @throws \think\db\exception\ModelNotFoundException
  20. */
  21. public function add()
  22. {
  23. // 检查用户状态
  24. $this->checkUserStatus();
  25. // 接收输入数据
  26. $data = $this->_vali([
  27. 'type.require' => '提现方式不能为空!',
  28. 'amount.require' => '提现金额不能为空!',
  29. 'remark.default' => '用户提交提现申请!',
  30. ]);
  31. $state = UserTransferService::config('status');
  32. if (empty($state)) $this->error('提现还没有开启!');
  33. $transfers = UserTransferService::config('transfer');
  34. if (empty($transfers[$data['type']]['state'])) $this->error('提现方式已停用!');
  35. // 提现数据补充
  36. $data['uuid'] = $this->uuid;
  37. $data['date'] = date('Y-m-d');
  38. $data['code'] = CodeExtend::uniqidDate(20, 'T');
  39. // 提现状态处理
  40. if (empty($transfers[$data['type']]['state']['audit'])) {
  41. $data['status'] = 1;
  42. $data['audit_status'] = 0;
  43. } else {
  44. $data['status'] = 3;
  45. $data['audit_status'] = 1;
  46. $data['audit_remark'] = '提现免审核';
  47. $data['audit_datetime'] = date('Y-m-d H:i:s');
  48. }
  49. // 扣除手续费
  50. $chargeRate = floatval(UserTransferService::config('charge'));
  51. $data['charge_rate'] = $chargeRate;
  52. $data['charge_amount'] = $chargeRate * $data['amount'] / 100;
  53. // 检查可提现余额
  54. [$total, $count] = UserRebateService::amount($this->uuid);
  55. if ($total - $count < $data['amount']) $this->error('可提现余额不足!');
  56. // 提现方式处理
  57. if ($data['type'] == 'alipay_account') {
  58. $data = array_merge($data, $this->_vali([
  59. 'alipay_user.require' => '开户姓名不能为空!',
  60. 'alipay_code.require' => '支付账号不能为空!',
  61. ]));
  62. } elseif (in_array($data['type'], ['wechat_qrcode', 'alipay_qrcode'])) {
  63. $data = array_merge($data, $this->_vali([
  64. 'qrcode.require' => '收款码不能为空!',
  65. ]));
  66. } elseif (in_array($data['type'], ['wechat_banks', 'transfer_banks'])) {
  67. $data = array_merge($data, $this->_vali([
  68. 'bank_wseq.require' => '银行编号不能为空!',
  69. 'bank_name.require' => '银行名称不能为空!',
  70. 'bank_user.require' => '开户账号不能为空!',
  71. 'bank_bran.require' => '银行分行不能为空!',
  72. 'bank_code.require' => '银行卡号不能为空!',
  73. ]));
  74. } elseif ($data['type'] != 'wechat_wallet') {
  75. $this->error('转账方式不存在!');
  76. }
  77. // 当日提现次数限制
  78. $map = ['uuid' => $this->uuid, 'type' => $data['type'], 'date' => $data['date']];
  79. $count = DataUserTransfer::mk()->where($map)->count();
  80. if ($count >= $transfers[$data['type']]['dayNumber']) $this->error("当日提现次数受限");
  81. // 提现金额范围控制
  82. if ($transfers[$data['type']]['minAmount'] > $data['amount']) {
  83. $this->error("不能少于{$transfers[$data['type']]['minAmount']}元");
  84. }
  85. if ($transfers[$data['type']]['maxAmount'] < $data['amount']) {
  86. $this->error("不能大于{$transfers[$data['type']]['maxAmount']}元");
  87. }
  88. // 写入用户提现数据
  89. if (DataUserTransfer::mk()->insert($data) !== false) {
  90. UserRebateService::amount($this->uuid);
  91. $this->success('提现申请成功');
  92. } else {
  93. $this->error('提现申请失败');
  94. }
  95. }
  96. /**
  97. * 用户提现记录
  98. * @throws \think\db\exception\DataNotFoundException
  99. * @throws \think\db\exception\DbException
  100. * @throws \think\db\exception\ModelNotFoundException
  101. */
  102. public function get()
  103. {
  104. $query = DataUserTransfer::mQuery()->where(['uuid' => $this->uuid]);
  105. $result = $query->like('date,code')->in('status')->order('id desc')->page(true, false, false, 10);
  106. // 统计历史数据
  107. $map = [['uuid', '=', $this->uuid], ['status', '>', 0]];
  108. [$total, $count, $locks] = UserRebateService::amount($this->uuid);
  109. $this->success('获取提现成功', array_merge($result, [
  110. 'total' => [
  111. '锁定' => $locks,
  112. '可提' => $total - $count,
  113. '上月' => DataUserTransfer::mk()->where($map)->whereLike('date', date("Y-m-%", strtotime('-1 month')))->sum('amount'),
  114. '本月' => DataUserTransfer::mk()->where($map)->whereLike('date', date("Y-m-%"))->sum('amount'),
  115. '全年' => DataUserTransfer::mk()->where($map)->whereLike('date', date("Y-%"))->sum('amount'),
  116. ],
  117. ]));
  118. }
  119. /**
  120. * 用户取消提现
  121. */
  122. public function cancel()
  123. {
  124. $data = $this->_vali(['uuid.value' => $this->uuid, 'code.require' => '单号不能为空!']);
  125. DataUserTransfer::mk()->where($data)->whereIn('status', [1, 2, 3])->update([
  126. 'status' => 0, 'change_time' => date("Y-m-d H:i:s"), 'change_desc' => '用户主动取消提现',
  127. ]);
  128. UserRebateService::amount($this->uuid);
  129. $this->success('取消提现成功');
  130. }
  131. /**
  132. * 用户确认提现
  133. */
  134. public function confirm()
  135. {
  136. $data = $this->_vali(['uuid.value' => $this->uuid, 'code.require' => '单号不能为空!']);
  137. DataUserTransfer::mk()->where($data)->whereIn('status', [4])->update([
  138. 'status' => 5, 'change_time' => date("Y-m-d H:i:s"), 'change_desc' => '用户主动确认收款',
  139. ]);
  140. UserRebateService::amount($this->uuid);
  141. $this->success('确认收款成功');
  142. }
  143. /**
  144. * 获取用户提现配置
  145. * @throws \think\db\exception\DataNotFoundException
  146. * @throws \think\db\exception\DbException
  147. * @throws \think\db\exception\ModelNotFoundException
  148. */
  149. public function config()
  150. {
  151. $data = UserTransferService::config();
  152. $data['banks'] = UserTransferService::instance()->banks();
  153. $this->success('获取用户提现配置', $data);
  154. }
  155. }