Money.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. /**
  6. * 会员中心
  7. */
  8. class Money extends Api
  9. {
  10. protected $noNeedLogin = [];
  11. protected $noNeedRight = '*';
  12. //配置
  13. public function withdraw_config(){
  14. //部分提现,下单收益+直推返佣
  15. $map = [
  16. 'user_id' => $this->auth->id,
  17. 'log_type' => 3,
  18. 'withdraw_id' => 0,
  19. ];
  20. $bufenscore = Db::name('user_score_log')->where($map)->sum('change_value');
  21. $data = [
  22. 'score' => model('wallet')->getWallet($this->auth->id,'score'),
  23. 'score_bufen' => $bufenscore,
  24. 'min_withdrawal_money' => config('site.min_withdrawal_money'),
  25. 'max_withdrawal_money' => config('site.max_withdrawal_money'),
  26. 'type_1' => Db::name('user_withdraw')->where('user_id',$this->auth->id)->where('type',1)->where('status',1)->field('realname,banknumber,bankname')->find(),
  27. 'type_2' => Db::name('user_withdraw')->where('user_id',$this->auth->id)->where('type',2)->where('status',1)->field('realname,banknumber,bankname')->find(),
  28. 'type_3' => Db::name('user_withdraw')->where('user_id',$this->auth->id)->where('type',3)->where('status',1)->field('realname,banknumber,bankname')->find(),
  29. ];
  30. $this->success(1,$data);
  31. }
  32. //提现
  33. public function scorewithdraw() {
  34. $type = input('type', 0, 'intval'); //类型:1=支付宝,2=微信,3=银行
  35. $from = input('from','all');
  36. if(!in_array($type,[1,2,3])){
  37. $this->error('参数错误');
  38. }
  39. if(!in_array($from,['all','bufen'])){
  40. $this->error('参数错误');
  41. }
  42. $check = Db::name('user_withdraw')->where('user_id',$this->auth->id)->where('status',0)->find();
  43. if($check){
  44. $this->error('目前还有兑换在审核中,请稍后在兑换');
  45. }
  46. //余额查询
  47. if($from == 'all'){
  48. $user_money = model('wallet')->getWallet($this->auth->id,'score');
  49. }else{
  50. $map = [
  51. 'user_id' => $this->auth->id,
  52. 'log_type' => 3,
  53. 'withdraw_id' => 0,
  54. ];
  55. $user_money = Db::name('user_score_log')->where($map)->sum('change_value');
  56. //记录id,等会修改状态
  57. $user_score_log_ids = Db::name('user_score_log')->where($map)->column('id');
  58. }
  59. if ($user_money <= 0) {
  60. $this->error('积分不足');
  61. }
  62. $data['order_no'] = createUniqueNo('T',$this->auth->id);
  63. $data['user_id'] = $this->auth->id;
  64. $data['score'] = $user_money;
  65. $data['type'] = $type;
  66. $data['realname'] = input('realname','');
  67. $data['banknumber'] = input('banknumber','');
  68. $data['bankname'] = input('bankname','');
  69. $data['createtime'] = time();
  70. $data['status'] = 0;
  71. //开启事务
  72. Db::startTrans();
  73. //添加提现记录
  74. $log_id = Db::name('user_withdraw')->insertGetId($data);
  75. if (!$log_id) {
  76. Db::rollback();
  77. $this->error('申请兑换失败');
  78. }
  79. //积分日志改状态
  80. if($from == 'bufen'){
  81. $rs_bufen = Db::name('user_score_log')->where('id','IN',$user_score_log_ids)->update(['withdraw_id'=>$log_id]);
  82. if ($rs_bufen === false) {
  83. Db::rollback();
  84. $this->error('申请兑换失败');
  85. }
  86. }
  87. //扣积分
  88. $rs_wallet = model('wallet')->lockChangeAccountRemain($this->auth->id,'score',-$data['score'],2,'积分兑换','user_withdraw',$log_id);
  89. if ($rs_wallet['status'] == false) {
  90. $this->error($rs_wallet['msg']);
  91. Db::rollback();
  92. }
  93. Db::commit();
  94. $this->success('申请兑换成功,请等待审核');
  95. }
  96. //废弃的提现
  97. public function old_scorewithdraw() {
  98. $type = input('type', 0, 'intval'); //类型:1=支付宝,2=微信,3=银行
  99. /*$money = input('score', '', 'intval');
  100. if ($money <= 0) {
  101. $this->error('请输入正确兑换积分');
  102. }*/
  103. $check = Db::name('user_withdraw')->where('user_id',$this->auth->id)->where('status',0)->find();
  104. if($check){
  105. $this->error('目前还有兑换在审核中,请稍后在兑换');
  106. }
  107. //余额查询
  108. $user_money = model('wallet')->getWallet($this->auth->id,'score');
  109. /*if ($user_money < $money) {
  110. $this->error('余额不足');
  111. }*/
  112. if ($user_money <= 0) {
  113. $this->error('积分不足');
  114. }
  115. //查询最低最高提现金额
  116. /*$min_withdrawal_money = config('site.min_withdrawal_money') ? config('site.min_withdrawal_money') : 1;
  117. $max_withdrawal_money = config('site.max_withdrawal_money') ? config('site.max_withdrawal_money') : 50000;
  118. if ($money < $min_withdrawal_money) {
  119. $this->error('最低提现金额' . $min_withdrawal_money . '元');
  120. }
  121. if ($money > $max_withdrawal_money) {
  122. $this->error('最高提现金额' . $max_withdrawal_money . '元');
  123. }*/
  124. $data['order_no'] = createUniqueNo('T',$this->auth->id);
  125. $data['user_id'] = $this->auth->id;
  126. // $data['score'] = $money;
  127. $data['score'] = $user_money;
  128. $data['type'] = $type;
  129. $data['realname'] = input('realname','');
  130. $data['banknumber'] = input('banknumber','');
  131. $data['bankname'] = input('bankname','');
  132. $data['createtime'] = time();
  133. $data['status'] = 0;
  134. //开启事务
  135. Db::startTrans();
  136. //添加提现记录
  137. $log_id = Db::name('user_withdraw')->insertGetId($data);
  138. if (!$log_id) {
  139. Db::rollback();
  140. $this->error('申请兑换失败');
  141. }
  142. $rs_wallet = model('wallet')->lockChangeAccountRemain($this->auth->id,'score',-$data['score'],2,'积分兑换','user_withdraw',$log_id);
  143. if ($rs_wallet['status'] == false) {
  144. $this->error($rs_wallet['msg']);
  145. Db::rollback();
  146. }
  147. Db::commit();
  148. $this->success('申请兑换成功,请等待审核');
  149. }
  150. //用户钱包流水
  151. public function scorelog(){
  152. $list = Db::name('user_score_log')->field('id,change_value,log_type,createtime')->where('user_id',$this->auth->id)->autopage()->order('id desc')->select();
  153. foreach($list as $key => &$val){
  154. $val['log_type_text'] = model('wallet')->getlogtype($val['log_type']);
  155. }
  156. $this->success('success',$list);
  157. }
  158. }