Takecash.php 4.8 KB

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