Money.php 7.4 KB

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