Vipuser.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use Think\Db;
  5. /**
  6. * 会员管理
  7. *
  8. * @icon fa fa-user
  9. */
  10. class Vipuser extends Backend
  11. {
  12. /**
  13. * Vipuser模型对象
  14. * @var \app\admin\model\Vipuser
  15. */
  16. protected $model = null;
  17. public function _initialize()
  18. {
  19. parent::_initialize();
  20. $this->model = new \app\admin\model\Vipuser;
  21. $this->view->assign("statusList", $this->model->getStatusList());
  22. $this->view->assign("genderList", $this->model->getGenderList());
  23. }
  24. /**
  25. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  26. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  27. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  28. */
  29. /**
  30. * 查看
  31. */
  32. public function index()
  33. {
  34. $vip_id = input('vip_id', 0, 'intval'); //会员id
  35. if (!$vip_id) {
  36. $vip_id = input('ids', 0, 'intval'); //从会员列表第一次跳转过来
  37. }
  38. $this->assignconfig('vip_id', $vip_id);
  39. //当前是否为关联查询
  40. $this->relationSearch = false;
  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. ->where(['maxlevel' => ['lt', $vip_id], 'status' => 1, 'experiencetime' => ['lt', time()]])
  51. ->where($where)
  52. ->order($sort, $order)
  53. ->paginate($limit);
  54. foreach ($list as $row) {
  55. $row->visible(['id','username','nickname','mobile','avatar','money','realname','gender','birthday','idcard','passport','emergencycontact','contactmobile','outdoorduration','invite_no','growthvalue','growthlevel','experiencelevel','experiencetime','maxlevel','freenumber']);
  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 giveout() {
  66. $ids = input('ids', 0, 'intval'); //用户id
  67. $vip_id = input('vip_id', 0, 'intval'); //会员id
  68. if (!$ids || !$vip_id) {
  69. $this->error('参数缺失');
  70. }
  71. $info = Db::name('vip')->find($vip_id);
  72. if (!$info) {
  73. $this->error('会员不存在');
  74. }
  75. if ($info['id'] == 1) {
  76. $this->error('普通会员不用发放');
  77. }
  78. //查询用户信息
  79. $user_info = Db::name('user')->find($ids);
  80. if (!$user_info) {
  81. $this->error('用户不存在');
  82. }
  83. if ($user_info['status'] != 1) {
  84. $this->error('用户已被禁用');
  85. }
  86. if ($user_info['experiencetime'] >= time()) {
  87. $this->error('用户体验会员尚未到期');
  88. }
  89. if ($info['id'] <= $user_info['maxlevel']) {
  90. $this->error('该用户已拥有该会员或更高等级会员');
  91. }
  92. $data['user_id'] = $ids;
  93. $data['vip_id'] = $vip_id;
  94. $data['title'] = $info['title'];
  95. $data['level'] = $info['level'];
  96. $data['growthvalue'] = $info['growthvalue'];
  97. $data['free'] = $info['free'];
  98. $data['price'] = $info['price'];
  99. $data['endtime'] = time() + $info['day'] * 86400;
  100. $data['vipdiscount'] = $info['vipdiscount'];
  101. $data['birthdiscount'] = $info['birthdiscount'];
  102. $data['manypeople'] = $info['manypeople'];
  103. $data['createtime'] = time();
  104. //开启事务
  105. Db::startTrans();
  106. //添加开通记录
  107. $rs = Db::name('vip_log')->insertGetId($data);
  108. if (!$rs) {
  109. Db::rollback();
  110. $this->error('发放失败');
  111. }
  112. //成长值会员信息
  113. $growth_vip_info = Db::name('vip')->find($user_info['growthlevel']);
  114. //修改用户表信息
  115. $user_data['experiencelevel'] = $vip_id;
  116. $user_data['experiencetime'] = $data['endtime'];
  117. $user_data['maxlevel'] = $vip_id;
  118. $freenumber = $user_info['freenumber'] + $info['free'] - $growth_vip_info['free'];
  119. $user_data['freenumber'] = $freenumber > 0 ? $freenumber : 0;
  120. $rt = Db::name('user')->where([
  121. 'id' => $user_info['id'],
  122. 'experiencelevel' => $user_info['experiencelevel'],
  123. 'maxlevel' => $user_info['maxlevel'],
  124. 'freenumber' => $user_info['freenumber']])->setField($user_data);
  125. if (!$rt) {
  126. Db::rollback();
  127. $this->error('发放失败');
  128. }
  129. Db::commit();
  130. $this->success('发放成功');
  131. }
  132. }