Takecash.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. //use app\common\model\wallet;
  6. /**
  7. * 用户钱包
  8. */
  9. class Takecash extends Api
  10. {
  11. protected $noNeedLogin = [];
  12. protected $noNeedRight = ['*'];
  13. //提现记录
  14. public function take_cash_log(){
  15. $list = Db::name('take_cash')->field('id, number, status, createtime')->where(['user_id'=>$this->auth->id])->autopage()->order('id desc')->select();
  16. if ($list) {
  17. foreach ($list as &$v) {
  18. $v['title'] = '余额提现';
  19. $v['number'] = '-' . $v['number'];
  20. $v['createtime'] = date('Y.m.d H:i', $v['createtime']);
  21. }
  22. }
  23. $this->success('success',$list);
  24. }
  25. //提现金额配置
  26. public function withdrawal_config() {
  27. $show = config('site.withdrawal_show');//提现说明
  28. $list = Db::name('withdrawal_config')->where('is_show',1)->order('weight asc,id asc')->select();
  29. if ($list) {
  30. $arr = [
  31. 'id' => -1,
  32. 'money' => 0,
  33. 'real_money' => 0,
  34. 'type' => 0,
  35. 'is_show' => 1,
  36. 'weight' => 1
  37. ];
  38. array_push($list, $arr);
  39. }
  40. $return_data['show'] = $show;
  41. $return_data['withdrawal_rate'] = config('site.withdrawal_rate') > 0 ? config('site.withdrawal_rate') : 10;
  42. $return_data['min'] = config('site.withdraw_min_money');
  43. $return_data['list'] = $list;
  44. $this->success('success',$return_data);
  45. }
  46. //提现
  47. public function withdrawal() {
  48. $id = input('id', 0, 'intval');
  49. $type = input('type', 0, 'intval'); //账户类型:1=支付宝,2=银行卡
  50. $freemoney = input('freemoney', 0, 'intval'); //自定义金额
  51. if (!$id && !$freemoney) {
  52. $this->error('请选择或填写提现金额');
  53. }
  54. if (!in_array($type, [1, 2])) {
  55. $this->error('请选择提现账户~');
  56. }
  57. if ($this->auth->idcard_status != 1) {
  58. $this->error('请先完成实名认证~');
  59. }
  60. if ($this->auth->gender == 1) {
  61. $user_wallet = Db::name('user_wallet')->where('user_id',$this->auth->id)->find();
  62. $is_vip = $this->is_vip($user_wallet['vip_endtime'],$user_wallet['vip_level']);
  63. if($is_vip != 2){
  64. $this->error('SVIP才能提现');
  65. }
  66. }
  67. if ($id > 0) {
  68. $withdrawal_config = Db::name('withdrawal_config')->find($id);
  69. if (!$withdrawal_config) {
  70. $this->error('提现金额不存在~');
  71. }
  72. if ($withdrawal_config['is_show'] != 1) {
  73. $this->error('提现金额暂未开放~');
  74. }
  75. if ($withdrawal_config['money'] <= 0 || $withdrawal_config['real_money'] <= 0 || $withdrawal_config['money'] < $withdrawal_config['real_money']) {
  76. $this->error('提现金额异常~');
  77. }
  78. //扣除金额
  79. $money = $withdrawal_config['money'];
  80. //实际获得金额
  81. $real_money = $withdrawal_config['real_money'];
  82. }
  83. if ($freemoney > 0) {
  84. //扣除金额
  85. $money = floatval($freemoney);
  86. $withdraw_min_money = config('site.withdraw_min_money');
  87. if($money < $withdraw_min_money){
  88. $this->error('最低提现'.$withdraw_min_money);
  89. }
  90. //平台手续费
  91. $withdrawal_rate = config('site.withdrawal_rate') >= 0 ? config('site.withdrawal_rate') : 10;
  92. $plat_money = bcdiv(bcmul($money,$withdrawal_rate,2),100,2);
  93. //减去手续费,得实得金额
  94. $real_money = bcsub($money,$plat_money,2);
  95. }
  96. if ($money <= 0 || $real_money <= 0) {
  97. $this->error('提现金额异常');
  98. }
  99. $check = Db::name('take_cash')->where(['user_id'=>$this->auth->id,'status'=>0])->find();
  100. if($check){
  101. $this->error('您有一笔提现在审核中,待审核通过后再申请提现。');
  102. }
  103. $user_money = model('wallet')->getwallet($this->auth->id,'money');
  104. if($money > $user_money){
  105. $this->error('可提现余额不足');
  106. }
  107. $user_bank_info = Db::name('user_bank')->where(['user_id' => $this->auth->id, 'type' => $type])->find();
  108. if (!$user_bank_info) {
  109. $this->error('请先添加提现方式');
  110. }
  111. $alipay_account = $user_bank_info['banknumber'];
  112. $realname = $user_bank_info['realname'];
  113. $data = [
  114. 'user_id' => $this->auth->id,
  115. 'wallet_id' => $freemoney > 0 ? 0 : $id,
  116. 'number' => $money,
  117. 'money' => $real_money,
  118. 'alipay_account' => $alipay_account,
  119. 'status' => 0,
  120. 'createtime' => time(),
  121. 'updatetime' => time(),
  122. 'type' => $type,
  123. 'realname' => $realname
  124. ];
  125. $msg = '申请成功请等待审核';
  126. //开启事务
  127. Db::startTrans();
  128. $log_id = Db::name('take_cash')->insertGetId($data);
  129. if (!$log_id) {
  130. Db::rollback();
  131. $this->error('您的网络开小差啦~');
  132. }
  133. //扣除money
  134. $rs_wallet = model('Wallet')->lockChangeAccountRemain($this->auth->id,0,'money',-$data['number'],15,'提现(审核中)','take_cash',$log_id);
  135. if($rs_wallet['status']===false)
  136. {
  137. Db::rollback();
  138. $this->error($rs_wallet['msg']);
  139. }
  140. Db::commit();
  141. $this->success($msg);
  142. }
  143. }