Withdraw.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. $user_money = model('wallet')->getwallet($this->auth->id,'agentjewel');
  55. if($agentjewel > $user_money){
  56. $this->error('提现金额不能大于可提现余额');
  57. }
  58. if($type == 1){
  59. $table_name = 'user_alipay';
  60. $account_json = Db::name($table_name)->where('user_id',$this->auth->id)->find();
  61. if(empty($account_json)){
  62. $this->error('未绑定对应的提现账号');
  63. }
  64. }else{
  65. $table_name = 'user_bank';
  66. $account_json = Db::name($table_name)->where('user_id',$this->auth->id)->find();
  67. if(empty($account_json)){
  68. $this->error('未绑定对应的提现账号');
  69. }
  70. }
  71. //除汇率,得人民币
  72. $huilv = config('agentjewel_to_money') ?: 10;
  73. $money = bcdiv($agentjewel,$huilv,2);
  74. //平台手续费
  75. $plat_bilv = config('site.withdrawal_plat_bili');
  76. $plat_money = bcdiv(bcmul($money,$plat_bilv,2),100,2);
  77. //减去手续费,得实得金额
  78. $get_money = bcsub($money,$plat_money,2);
  79. $data = [
  80. 'user_id' => $this->auth->id,
  81. 'agentjewel' => $agentjewel,
  82. 'huilv' => $huilv,
  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('withdraw')->insertGetId($data);
  95. if(!$log_id){
  96. Db::rollback();
  97. $this->error('提现失败');
  98. }
  99. //扣除money
  100. $rs_wallet = model('Wallet')->lockChangeAccountRemain($this->auth->id,'agentjewel',-$agentjewel,55,'提现(审核中)','withdraw',$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 withdraw_log(){
  111. $list = Db::name('withdraw')->field('id,money,type,createtime')->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. }