Takecash.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. //提现before
  13. public function take_cash_before(){
  14. $freemoney = input('freemoney',0);
  15. if(!$freemoney){
  16. $this->error('请填写金额');
  17. }
  18. $money = floatval($freemoney);
  19. if($money<=0)
  20. {
  21. $this->error('金额必须大于0');
  22. }
  23. $min = config('site.min_takecash_money');
  24. $max = config('site.max_takecash_money');
  25. if($money < $min){
  26. $this->error('提现金额不能小于'.$min);
  27. }
  28. if($money > $max){
  29. $this->error('提现金额不能大于'.$max);
  30. }
  31. $user_money = model('wallet')->getwallet($this->auth->id,'money');
  32. if($money > $user_money){
  33. $this->error('提现金额不能大于可提现余额');
  34. }
  35. //平台手续费
  36. $plat_bilv = config('site.takecash_plat_bili');
  37. $plat_money = bcdiv(bcmul($money,$plat_bilv,2),100,2);
  38. //减去手续费,得实得金额
  39. $get_money = bcsub($money,$plat_money,2);
  40. $data = [
  41. 'money' => $money,
  42. 'plat_bilv' => $plat_bilv,
  43. 'plat_money' => $plat_money,
  44. 'get_money' => $get_money,
  45. ];
  46. $this->success(1,$data);
  47. }
  48. //提现
  49. public function take_cash(){
  50. $freemoney = input('freemoney',0);
  51. // $type = input('type',1);
  52. $type = 2;
  53. if(!$freemoney){
  54. $this->error('请填写金额');
  55. }
  56. if (!in_array($type,[1,2,3])) {
  57. $this->error('未知的提现类型');
  58. }
  59. //赋值money
  60. /*if($rc_id){
  61. $recharge_config = Db::name('take_cash_config')->where('id',$rc_id)->find();
  62. $money = $recharge_config['money'] ?: 0;
  63. }*/
  64. //自由输入覆盖
  65. if(!empty($freemoney)){
  66. $rc_id = 0;
  67. $money = floatval($freemoney);
  68. }
  69. //
  70. if($money<=0)
  71. {
  72. $this->error('金额必须大于0');
  73. }
  74. $min = config('site.min_takecash_money');
  75. $max = config('site.max_takecash_money');
  76. if($money < $min){
  77. $this->error('提现金额不能小于'.$min);
  78. }
  79. if($money > $max){
  80. $this->error('提现金额不能大于'.$max);
  81. }
  82. $check = Db::name('user_take_cash')->where(['user_id'=>$this->auth->id,'status'=>0])->find();
  83. if($check){
  84. $this->error('您已经申请了提现,请等待审核');
  85. }
  86. $user_money = model('wallet')->getwallet($this->auth->id,'money');
  87. if($money > $user_money){
  88. $this->error('提现金额不能大于可提现余额');
  89. }
  90. if($type == 1){
  91. $table_name = 'user_alipay';
  92. $account_json = Db::name($table_name)->where('user_id',$this->auth->id)->find();
  93. if(empty($account_json)){
  94. $this->error('未绑定对应的提现账号');
  95. }
  96. }elseif($type == 2){
  97. $table_name = 'user_bank';
  98. $account_json = Db::name($table_name)->where('user_id',$this->auth->id)->find();
  99. if(empty($account_json)){
  100. $this->error('未绑定对应的提现账号');
  101. }
  102. }elseif($type == 3){
  103. //微信支付
  104. $table_name = 'user_wechat';
  105. $account_json = Db::name($table_name)->where('user_id',$this->auth->id)->find();
  106. if(empty($account_json)){
  107. $this->error('未绑定对应的提现账号');
  108. }
  109. }
  110. //平台手续费
  111. $plat_bilv = config('site.takecash_plat_bili');
  112. $plat_money = bcdiv(bcmul($money,$plat_bilv,2),100,2);
  113. //减去手续费,得实得金额
  114. $get_money = bcsub($money,$plat_money,2);
  115. $data = [
  116. 'user_id' => $this->auth->id,
  117. 'money' => $money,
  118. 'plat_bilv' => $plat_bilv,
  119. 'plat_money' => $plat_money,
  120. 'get_money' => $get_money,
  121. 'type' => $type,
  122. 'acount_json' => json_encode($account_json),
  123. 'createtime' => time(),
  124. 'updatetime' => time(),
  125. 'status' => 0,
  126. ];
  127. Db::startTrans();
  128. $log_id = Db::name('user_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,'money',-$money,21,'提现(审核中)','user_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('申请成功请等待审核');
  142. }
  143. //提现记录
  144. public function take_cash_log(){
  145. $list = Db::name('user_take_cash')->field('id,money,get_money,type,createtime,status')->where(['user_id'=>$this->auth->id])->autopage()->select();
  146. foreach($list as $key => &$val){
  147. $val['remark'] = '';
  148. if($val['type'] == 1){
  149. $val['remark'] = '支付宝提现';
  150. }elseif($val['type'] == 2){
  151. $val['remark'] = '银行卡提现';
  152. }else{
  153. $val['remark'] = '微信提现';
  154. }
  155. }
  156. $this->success('success',$list);
  157. }
  158. /////////////////////////没用到/////////////////////
  159. //提现配置
  160. public function take_cash_config(){
  161. $config = Db::name('take_cash_config')->order('weigh asc,id asc')->select();
  162. $plat_bilv = config('site.takecash_plat_bili');
  163. foreach($config as $key => &$val){
  164. $val['get_money'] = bcdiv(bcmul($val['money'],(100-$plat_bilv),2),100,2);
  165. }
  166. $data = [
  167. 'config' => $config,
  168. 'wallet' => model('wallet')->getwallet($this->auth->id),
  169. 'min' => config('site.min_takecash_money'),
  170. 'max' => config('site.max_takecash_money'),
  171. 'plat_bilv' => $plat_bilv,
  172. 'user_bank' => Db::name('user_bank')->where('user_id',$this->auth->id)->find(),
  173. 'user_alipay' => Db::name('user_alipay')->where('user_id',$this->auth->id)->find(),
  174. 'remark' => config('site.take_cash_rule'),
  175. ];
  176. $this->success('success',$data);
  177. }
  178. }