User.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: 中闽 < 1464674022@qq.com >
  5. * Date: 2019/12/5
  6. * Time: 17:44
  7. */
  8. namespace app\admin\controller;
  9. use app\admin\controller\base\Permissions;
  10. use app\common\model\PointLog as pointLogModel;
  11. use app\common\model\User as userModel;
  12. use think\Db;
  13. class User extends Permissions
  14. {
  15. public function index()
  16. {
  17. if ($this->request->isAjax()) {
  18. $post = $this->request->param();
  19. $where = [];
  20. if (isset($post['ids']) and !empty($post['ids'])) {
  21. $where['id'] = ['in', $post['ids']];
  22. }
  23. if (isset($post['pid']) and !empty($post['pid'])) {
  24. $where['pid'] = intval($post['pid']);
  25. }
  26. if (isset($post["level"]) and "" != $post["level"]) {
  27. $where["level"] = $post["level"];
  28. }
  29. if (isset($post['nickname']) and !empty($post['nickname'])) {
  30. $where['nickname'] = ['like', '%' . $post['nickname'] . '%'];
  31. }
  32. if (isset($post['user_type']) and "" != $post['user_type']) {
  33. $where['user_type'] = $post['user_type'];
  34. }
  35. if (isset($post['user_cate']) and "" != $post['user_cate']) {
  36. $where['user_cate'] = $post['user_cate'];
  37. }
  38. if (isset($post['ip']) and !empty($post['ip'])) {
  39. $where['ip'] = $post['ip'];
  40. }
  41. if (isset($post['create_time']) and !empty($post['create_time'])) {
  42. $timerang = explode(' - ', $post['create_time']);
  43. $min_time = strtotime($timerang[0]);
  44. $max_time = strtotime($timerang[1]);
  45. $where['create_time'] = [['>=', $min_time], ['<=', $max_time]];
  46. }
  47. $model = new userModel();
  48. $count = $model->where($where)->count();
  49. $data = $model->where($where)->page($post['page']??0, $post['limit']??15)->order('id desc')->select();
  50. foreach ($data as $k => $v) {
  51. $v['user_cate_text'] = $v->user_cate_text;
  52. $v['user_type_text'] = $v->user_type_text;
  53. $v['invite_code'] = $v->invite_code;
  54. $v['level_text'] = $v->level_text;
  55. $v['status_text'] = $v->status_text;
  56. $v['parent_name'] = $v->parent ? $v->parent->nickname : '';
  57. $v['child_count'] = $v->child_count;
  58. $v['stop_time_text'] = $v->stop_time_text;
  59. $data[$k] = $v;
  60. }
  61. return array('code' => 0, 'count' => $count, 'data' => $data);
  62. } else {
  63. $this->assign('user_cates', userModel::USER_CATES);
  64. $this->assign('user_types', userModel::USER_TYPES);
  65. $this->assign('vips', \app\common\model\User::GRADE);
  66. return $this->fetch();
  67. }
  68. }
  69. public function publish()
  70. {
  71. $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
  72. $model = new userModel();
  73. $post = $this->request->post();
  74. if (!$this->request->isPost()) {
  75. $this->assign('user_cates', userModel::USER_CATES);
  76. $this->assign('user_types', userModel::USER_TYPES);
  77. } else {
  78. $validate = new \think\Validate([
  79. ['passport', 'require', '账号不能为空'],
  80. ['passport', 'max:50', '账号长度要小于50'],
  81. ['passport', 'unique:user', '该账号已经存在'],
  82. ]);
  83. if (!$validate->check($post)) {
  84. $this->error('提交失败:' . $validate->getError());
  85. }
  86. }
  87. if ($id > 0) {
  88. //修改操作
  89. if ($this->request->isPost()) {
  90. $user = $model->where('id', $id)->find();
  91. if (empty($user)) {
  92. $this->error('id不正确');
  93. }
  94. unset($post['passport']);
  95. if (false == $model->allowField(true)->save($post, ['id' => $id])) {
  96. $this->error('修改失败' . $model->getError());
  97. } else {
  98. $this->success('修改成功', 'index');
  99. }
  100. } else {
  101. $user = $model->where('id', $id)->find();
  102. if (!empty($user)) {
  103. $this->assign('user', $user);
  104. return $this->fetch();
  105. } else {
  106. $this->error('id不正确');
  107. }
  108. }
  109. } else {
  110. //新增操作
  111. if ($this->request->isPost()) {
  112. $post = $this->request->post();
  113. $post['ip'] = $this->request->ip();
  114. $post['create_time'] = time();
  115. $post['login_time'] = time();
  116. if (false == $model->allowField(true)->save($post)) {
  117. $this->error('添加失败');
  118. } else {
  119. $this->success('添加成功', 'index');
  120. }
  121. } else {
  122. return $this->fetch();
  123. }
  124. }
  125. }
  126. public function delete()
  127. {
  128. if ($this->request->isAjax()) {
  129. $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
  130. if (false == Db::name('user')->where('id', $id)->delete()) {
  131. $this->error('删除失败');
  132. } else {
  133. $this->success('删除成功', 'index');
  134. }
  135. }
  136. }
  137. public function status()
  138. {
  139. if ($this->request->isPost()) {
  140. $post = $this->request->post();
  141. if (false == Db::name('user')->where('id', $post['id'])->update(['status' => $post['status']])) {
  142. $this->error('设置失败');
  143. } else {
  144. $this->success('设置成功', 'index');
  145. }
  146. }
  147. }
  148. public function setPoint()
  149. {
  150. if ($this->request->isAjax()) {
  151. $post = $this->request->param();
  152. $ids = $post['ids'];
  153. $value = abs($this->request->param('value', 0, 'intval'));
  154. if (empty($value)) {
  155. $this->error("请输入积分数量");
  156. }
  157. $type = $post['type']??pointLogModel::TYPE_ADMIN_UPDATE;
  158. $action = $post['action']??1;
  159. $model = new userModel();
  160. $users = $model->where('id', 'in', $ids)->select();
  161. // 启动事务
  162. Db::startTrans();
  163. try {
  164. foreach ($users as $user) {
  165. $pointLog = new pointLogModel();
  166. $final = $action == 1 ? $user->point + $value : $user->point - $value;
  167. $log = [
  168. "user_id" => $user->id,
  169. "before" => $user->point,
  170. "symbol" => $action == 1 ? '+' : '-',
  171. "change" => $value,
  172. "final" => $final,
  173. "type" => $type,
  174. "remark" => "管理员" . session(self::ADMIN_ID) . ':' . session(self::ADMIN_NAME) . "操作",
  175. ];
  176. if (!$pointLog->save($log)) {
  177. throw new \Exception("insert point_log fail");
  178. }
  179. $user->point = $final;
  180. $user->save();
  181. }
  182. // 提交事务
  183. Db::commit();
  184. } catch (\Exception $e) {
  185. Db::rollback();
  186. $this->error("修改失败" . $e->getMessage());
  187. }
  188. $this->success('修改成功');
  189. }
  190. }
  191. public function setVips()
  192. {
  193. if ($this->request->isAjax()) {
  194. $post = $this->request->param();
  195. $ids = $post['ids'];
  196. $vip_id = $this->request->param("vip", 0);
  197. $model = new userModel();
  198. $users = $model->where('id', 'in', $ids)->select();
  199. /** @var userModel $user */
  200. foreach ($users as $user) {
  201. $user->save([
  202. 'level' => $vip_id,
  203. 'stop_time' => $vip_id ? $user->createStopTime($vip_id) : 0
  204. ]);
  205. }
  206. $this->success('修改成功');
  207. }
  208. }
  209. //重置密码
  210. public function resetpass()
  211. {
  212. if ($this->request->isAjax()) {
  213. $id = $this->request->param('id', 0, 'intval');
  214. if (false == Db::name('user')->where('id', $id)->update(['password' => password(123456)])) {
  215. $this->error('重置失败');
  216. } else {
  217. $this->success('重置成功', 'index');
  218. }
  219. }
  220. }
  221. }