Takecash.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. /**
  6. * 提现
  7. */
  8. class Takecash extends Api
  9. {
  10. protected $noNeedLogin = [];
  11. protected $noNeedRight = ['*'];
  12. //提现
  13. public function take_cash(){
  14. $freemoney = input('freemoney',0);
  15. $type = input('type',1);
  16. if(!$freemoney){
  17. $this->error('请填写金额');
  18. }
  19. if (!in_array($type,[1,2,3])) {
  20. $this->error('未知的提现类型');
  21. }
  22. if(!$this->user_auth_limit()){
  23. $this->error('请先完成实名认证');
  24. }
  25. //赋值money
  26. /*if($rc_id){
  27. $recharge_config = Db::name('take_cash_config')->where('id',$rc_id)->find();
  28. $money = $recharge_config['money'] ?: 0;
  29. }*/
  30. //自由输入覆盖
  31. if(!empty($freemoney)){
  32. $rc_id = 0;
  33. $money = floatval($freemoney);
  34. }
  35. //
  36. if($money<=0)
  37. {
  38. $this->error('金额必须大于0');
  39. }
  40. $min = config('site.min_takecash_money');
  41. $max = config('site.max_takecash_money');
  42. if($money < $min){
  43. $this->error('提现金额不能小于'.$min);
  44. }
  45. if($money > $max){
  46. $this->error('提现金额不能大于'.$max);
  47. }
  48. $check = Db::name('user_take_cash')->where(['user_id'=>$this->auth->id,'status'=>0])->find();
  49. if($check){
  50. $this->error('您已经申请了提现,请等待审核');
  51. }
  52. $user_money = model('wallet')->getwallet($this->auth->id,'money');
  53. if($money > $user_money){
  54. $this->error('提现金额不能大于可提现余额');
  55. }
  56. if($type == 1){
  57. $table_name = 'user_alipay';
  58. $account_json = Db::name($table_name)->where('user_id',$this->auth->id)->find();
  59. if(empty($account_json)){
  60. $this->error('未绑定对应的提现账号');
  61. }
  62. }elseif($type == 2){
  63. $table_name = 'user_bank';
  64. $account_json = Db::name($table_name)->where('user_id',$this->auth->id)->find();
  65. if(empty($account_json)){
  66. $this->error('未绑定对应的提现账号');
  67. }
  68. }elseif($type == 3){
  69. //微信支付
  70. $table_name = 'user_wechat';
  71. $account_json = Db::name($table_name)->where('user_id',$this->auth->id)->find();
  72. if(empty($account_json)){
  73. $this->error('未绑定对应的提现账号');
  74. }
  75. }
  76. //平台手续费
  77. $plat_bilv = config('site.takecash_plat_bili');
  78. $plat_money = bcdiv(bcmul($money,$plat_bilv,2),100,2);
  79. //减去手续费,得实得金额
  80. $get_money = bcsub($money,$plat_money,2);
  81. $data = [
  82. 'user_id' => $this->auth->id,
  83. 'money' => $money,
  84. 'plat_bilv' => $plat_bilv,
  85. 'plat_money' => $plat_money,
  86. 'get_money' => $get_money,
  87. 'type' => $type,
  88. 'acount_json' => json_encode($account_json),
  89. 'createtime' => time(),
  90. 'updatetime' => time(),
  91. 'status' => 0,
  92. ];
  93. Db::startTrans();
  94. $log_id = Db::name('user_take_cash')->insertGetId($data);
  95. if(!$log_id){
  96. Db::rollback();
  97. $this->error('提现失败');
  98. }
  99. //扣除money
  100. $rs_wallet = model('Wallet')->lockChangeAccountRemain($this->auth->id,'money',-$money,21,'提现(审核中)','user_take_cash',$log_id);
  101. if($rs_wallet['status']===false)
  102. {
  103. Db::rollback();
  104. $this->error($rs_wallet['msg']);
  105. }
  106. Db::commit();
  107. $this->success('申请成功请等待审核');
  108. }
  109. //提现记录
  110. public function take_cash_log(){
  111. $list = Db::name('user_take_cash')->field('id,money,get_money,type,createtime,status')->where(['user_id'=>$this->auth->id])->autopage()->select();
  112. foreach($list as $key => &$val){
  113. $val['remark'] = '';
  114. if($val['type'] == 1){
  115. $val['remark'] = '支付宝提现';
  116. }elseif($val['type'] == 2){
  117. $val['remark'] = '银行卡提现';
  118. }else{
  119. $val['remark'] = '微信提现';
  120. }
  121. }
  122. $this->success('success',$list);
  123. }
  124. //////////////////////////////////////////////
  125. //提现配置
  126. public function take_cash_config(){
  127. $config = Db::name('take_cash_config')->order('weigh asc,id asc')->select();
  128. $plat_bilv = config('site.takecash_plat_bili');
  129. foreach($config as $key => &$val){
  130. $val['get_money'] = bcdiv(bcmul($val['money'],(100-$plat_bilv),2),100,2);
  131. }
  132. $data = [
  133. 'config' => $config,
  134. 'wallet' => model('wallet')->getwallet($this->auth->id),
  135. 'min' => config('site.min_takecash_money'),
  136. 'max' => config('site.max_takecash_money'),
  137. 'plat_bilv' => $plat_bilv,
  138. 'user_bank' => Db::name('user_bank')->where('user_id',$this->auth->id)->find(),
  139. 'user_alipay' => Db::name('user_alipay')->where('user_id',$this->auth->id)->find(),
  140. 'remark' => config('site.take_cash_rule'),
  141. ];
  142. $this->success('success',$data);
  143. }
  144. }