Withdraw.php 4.7 KB

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