Transfer.php 6.9 KB

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