Userwallet.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. use app\common\model\wallet;
  6. /**
  7. * 用户钱包
  8. *
  9. * @icon fa fa-circle-o
  10. */
  11. class Userwallet extends Backend
  12. {
  13. /**
  14. * Userwallet模型对象
  15. * @var \app\admin\model\Userwallet
  16. */
  17. protected $model = null;
  18. public function _initialize()
  19. {
  20. parent::_initialize();
  21. $this->model = new \app\admin\model\Userwallet;
  22. $VipLevelList = $this->model->getVipLevelList();
  23. $this->assign('VipLevelList',$VipLevelList);
  24. }
  25. public function import()
  26. {
  27. parent::import();
  28. }
  29. /**
  30. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  31. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  32. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  33. */
  34. /**
  35. * 查看
  36. */
  37. public function index()
  38. {
  39. //当前是否为关联查询
  40. $this->relationSearch = true;
  41. //设置过滤方法
  42. $this->request->filter(['strip_tags', 'trim']);
  43. if ($this->request->isAjax()) {
  44. //如果发送的来源是Selectpage,则转发到Selectpage
  45. if ($this->request->request('keyField')) {
  46. return $this->selectpage();
  47. }
  48. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  49. $list = $this->model
  50. ->with(['user'])
  51. ->where($where)
  52. ->order($sort, $order)
  53. ->paginate($limit);
  54. foreach ($list as $row) {
  55. $row->getRelation('user')->visible(['username','nickname','mobile']);
  56. }
  57. $result = array("total" => $list->total(), "rows" => $list->items());
  58. return json($result);
  59. }
  60. return $this->view->fetch();
  61. }
  62. /**
  63. * 充值金币
  64. */
  65. public function updategold(){
  66. $id = input('id');
  67. $info = Db::name('user_wallet')
  68. ->where('id',$id)
  69. ->find();
  70. if ($this->request->isPost()) {
  71. $user_id = input_post('user_id');
  72. $gold = input_post('gold', 0, 'intval');
  73. $user_info = Db::name('user')->find($user_id);
  74. if (!$user_info) {
  75. $this->error('用户不存在');
  76. }
  77. if ($gold > 0) {
  78. $is_pay_get = 1;
  79. } else {
  80. $is_pay_get = 0;
  81. }
  82. Db::startTrans();
  83. $rs = model('wallet')->lockChangeAccountRemain($user_id,0,'gold',$gold,1, '系统调节','',0, $is_pay_get);
  84. if($rs['status'] === false){
  85. Db::rollback();
  86. $this->error($rs['msg']);
  87. }
  88. //若有上级,给上级返利
  89. if ($user_info['intro_uid'] && $gold > 0) {
  90. //获取返利比率
  91. $agent_info = Db::name('user')->where(['id' => $user_info['intro_uid']])->field('is_agent,h_intro_recharge_rebate_rate')->find();
  92. $intro_recharge_rebate_rate = ($agent_info['is_agent'] == 1) ? $agent_info['h_intro_recharge_rebate_rate'] : (int)config('site.intro_recharge_rebate_rate');
  93. if ($intro_recharge_rebate_rate > 0 && $intro_recharge_rebate_rate <= 100) {
  94. // 添加赠送用户余额
  95. $money_to_gold = config('site.money_to_gold');
  96. $money = bcdiv($gold,$money_to_gold,2);
  97. //上级获得金额数量
  98. $intro_uid_money = number_format($money * $intro_recharge_rebate_rate / 100, 2, '.', '');
  99. if ($intro_uid_money > 0) {
  100. $intro_result = model('wallet')->lockChangeAccountRemain($user_info['intro_uid'],$user_id,'money',$intro_uid_money,65,'邀请人充值奖励');
  101. if($intro_result['status']===false)
  102. {
  103. Db::rollback();
  104. $this->error($intro_result['msg']);
  105. }
  106. }
  107. }
  108. }
  109. Db::commit();
  110. $this->success('充值完成');
  111. }
  112. $this->assign('row',$info);
  113. return $this->view->fetch();
  114. }
  115. /**
  116. * 充值收益
  117. */
  118. public function updatemoney(){
  119. $id = input('id');
  120. $info = Db::name('user_wallet')
  121. ->where('id',$id)
  122. ->find();
  123. if ($this->request->isPost()) {
  124. $user_id = input_post('user_id');
  125. $money = input_post('money', 0, 'intval');
  126. $user_info = Db::name('user')->find($user_id);
  127. if (!$user_info) {
  128. $this->error('用户不存在');
  129. }
  130. Db::startTrans();
  131. $rs = model('wallet')->lockChangeAccountRemain($user_id,0,'money',$money,2, '系统调节','',0, 0);
  132. if($rs['status'] === false){
  133. Db::rollback();
  134. $this->error($rs['msg']);
  135. }
  136. Db::commit();
  137. $this->success('充值完成');
  138. }
  139. $this->assign('row',$info);
  140. return $this->view->fetch();
  141. }
  142. /**
  143. * 充值VIP
  144. */
  145. public function updatevip(){
  146. $id = input('id');
  147. $info = Db::name('user_wallet')->where('id',$id)->find();
  148. if ($this->request->isPost()) {
  149. $user_id = input_post('user_id');
  150. $vip_level = input_post('vip_level');
  151. $vip_endtime = strtotime(input_post('vip_endtime'));
  152. Db::startTrans();
  153. $info = Db::name('user_wallet')->where('user_id',$user_id)->update([
  154. 'vip_endtime'=>$vip_endtime,
  155. 'vip_level'=>$vip_level,
  156. ]);
  157. if($info === false){
  158. Db::rollback();
  159. $this->error('修改失败');
  160. }
  161. Db::commit();
  162. $this->success('充值完成');
  163. }
  164. $this->assign('row',$info);
  165. return $this->view->fetch();
  166. }
  167. }