User.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: edison
  5. * Date: 2018/3/26
  6. * Time: 上午11:50
  7. */
  8. namespace app\admin\controller;
  9. use think\Config;
  10. use think\Db;
  11. use tree\Tree;
  12. class User extends Admin
  13. {
  14. public function _initialize() {
  15. parent::_initialize();
  16. $this->adminModel = Db::name('cy_admin');
  17. $this->adminRoleModel = Db::name('nw_admin_role');
  18. $this->adminRoleUserModel = Db::name('nw_admin_role_user');
  19. }
  20. public function index()
  21. {
  22. $role_id = 0;
  23. $data = $this->adminRoleModel->where('status',1)->field('id,parent_id,name')->order('id')->select();
  24. $tree = new Tree();
  25. $tree->init($data);
  26. $where = [];
  27. //非超级管理员时
  28. if(!in_array(session('ADMIN_ID') ,[1,19])){
  29. $role_id = $this->adminRoleUserModel->where(['user_id'=>session('ADMIN_ID')])->value('role_id');
  30. $childrenIds = $tree->getChildrenIds($role_id,false);
  31. $where['ru.role_id'] = ['in',$childrenIds];
  32. }
  33. $childrenlist = $tree->getTreeList($tree->getTreeArrayO($role_id));
  34. /**搜索条件**/
  35. $username = input('username');
  36. $mobile = input('mobile');
  37. $status = input('status');
  38. $role_id = input('role_id');
  39. $start_time = input('start_time');
  40. $end_time = input('end_time');
  41. if ($username) {
  42. $where['a.username'] = ['like', "%$username%"];
  43. }
  44. if ($mobile) {
  45. $where['a.mobile'] = $mobile;
  46. }
  47. if ($role_id) {
  48. $where['ru.role_id'] = $role_id;
  49. }
  50. if ($status != '') {
  51. $where['a.status'] = $status;
  52. }
  53. if ($start_time &&$end_time ) {
  54. $where['a.create_time'] = [
  55. ['>=', strtotime($start_time)],
  56. ['<=', strtotime($end_time . ' 23:59:59')],
  57. ];
  58. }elseif($start_time){
  59. $where['a.create_time'] = ['>=', strtotime($start_time)];
  60. }elseif($end_time) {
  61. $where['a.create_time'] = ['<=', strtotime($end_time . ' 23:59:59')];
  62. }
  63. $where['a.type']= config('ADMIN');
  64. $where['a.id'] = ['<>', 1];
  65. $param = input('request.'); //分页带条件
  66. $users = $this->adminModel->alias('a')
  67. ->join('nw_admin_role_user ru','a.id=ru.user_id','left')
  68. ->join('nw_admin_role ro','ru.role_id=ro.id','left')
  69. ->field('a.id,a.username,a.mobile,a.status,ro.name,a.create_time')
  70. ->where($where)
  71. ->order("a.id DESC")
  72. ->paginate(10,false,array('query' => $param));
  73. // 获取分页显示
  74. $page = $users->render();
  75. $this->assign("page", $page);
  76. $this->assign("roles", $childrenlist);
  77. $this->assign("users", $users);
  78. return $this->fetch();
  79. }
  80. public function add()
  81. {
  82. $role_id = 0;
  83. //非超级管理员时
  84. if(session('ADMIN_ID') != 1){
  85. $role_id = $this->adminRoleUserModel->where(['user_id'=>session('ADMIN_ID')])->value('role_id');
  86. }
  87. $data = $this->adminRoleModel->where('status',1)->field('id,parent_id,name')->order('id')->select();
  88. $tree = new Tree();
  89. $tree->init($data);
  90. $childrenlist = $tree->getTreeList($tree->getTreeArrayO($role_id));
  91. $this->assign("roles", $childrenlist);
  92. return $this->fetch();
  93. }
  94. public function addPost()
  95. {
  96. if ($this->request->isPost()) {
  97. if (!empty($_POST['role_id'])) {
  98. $role_id = $_POST['role_id'];
  99. unset($_POST['role_id']);
  100. $result = $this->validate($this->request->param(), 'User');
  101. if ($result !== true) {
  102. $this->error($result);
  103. } else {
  104. $_POST['password'] = mg_password($_POST['password']);
  105. $_POST['status'] = 1;
  106. $_POST['type'] = config('ADMIN');
  107. $_POST['create_time'] = time();
  108. $_POST['password_update_time'] = time();
  109. $_POST['username'] = trim($_POST['username']);
  110. $userInfo = $this->adminModel->field('id')->where(['username' => $_POST['username']])->find();
  111. if ($userInfo) {
  112. $this->error("用户名已存在! ");
  113. }
  114. $channelInfo = model('channel')->where(['name' => $_POST['username']])->find();
  115. if ( $channelInfo ) {
  116. $this->error("跟渠道用户重名! ");
  117. }
  118. $result = $this->adminModel->insertGetId($_POST);
  119. if ($result !== false) {
  120. if (mg_get_current_admin_id() != 1 && $role_id == 1) {
  121. $this->error("为了网站的安全,非网站创建者不可创建超级管理员!");
  122. }
  123. $this->adminRoleUserModel->insert(["role_id" => $role_id, "user_id" => $result]);
  124. // 记录日志
  125. $LogStr = "新增账号:" .$_POST['username'];
  126. $this->insertLog($this->current_node,$LogStr,161);
  127. $this->success("添加成功!", url("user/index"));
  128. } else {
  129. $this->error("添加失败!");
  130. }
  131. }
  132. } else {
  133. $this->error("请为此用户指定角色!");
  134. }
  135. }
  136. }
  137. public function edit()
  138. {
  139. $id = input('id', 0, 'intval');
  140. $role_id = 0;
  141. //非超级管理员时
  142. if(session('ADMIN_ID') != 1){
  143. $role_id = $this->adminRoleUserModel->where(['user_id'=>session('ADMIN_ID')])->value('role_id');
  144. }
  145. $data = $this->adminRoleModel->where('status',1)->field('id,parent_id,name')->order('id')->select();
  146. $tree = new Tree();
  147. $tree->init($data);
  148. $childrenlist = $tree->getTreeList($tree->getTreeArrayO($role_id));
  149. $this->assign("roles", $childrenlist);
  150. $role_ids = $this->adminRoleUserModel->where(["user_id" => $id])->value("role_id");
  151. $this->assign("role_ids", $role_ids);
  152. $user = $this->adminModel->where(["id" => $id])->find();
  153. $this->assign('user', $user);
  154. return $this->fetch();
  155. }
  156. public function editPost()
  157. {
  158. if ($this->request->isPost()) {
  159. if (!empty($_POST['role_id'])) {
  160. if (empty($_POST['password'])) {
  161. unset($_POST['password']);
  162. } else {
  163. $result = $this->validate(['password' => $_POST['password']],
  164. [
  165. ['password', 'require|length:6,30|passwordStrength:2', '请输入新密码|新密码长度为 6 - 30|新密码强度不足,至少要包含字母、数字、符号中的两种'],
  166. ]);
  167. if (true !== $result) {
  168. $this->error($result);
  169. }
  170. $_POST['password'] = mg_password($_POST['password']);
  171. $_POST['password_update_time'] = NOW_TIMESTAMP;
  172. }
  173. $role_id = $this->request->param('role_id');
  174. unset($_POST['role_id']);
  175. $result = $this->validate($this->request->param(), 'User.edit');
  176. if ($result !== true) {
  177. // 验证失败 输出错误信息
  178. $this->error($result);
  179. } else {
  180. $result = $this->adminModel->update($_POST);
  181. if ($result !== false) {
  182. $uid = $this->request->param('id', 0, 'intval');
  183. $this->adminRoleUserModel->where(["user_id" => $uid])->delete();
  184. if (mg_get_current_admin_id() != 1 && $role_id == 1) {
  185. $this->error("为了网站的安全,非网站创建者不可创建超级管理员!");
  186. }
  187. $this->adminRoleUserModel->insert(["role_id" => $role_id, "user_id" => $uid]);
  188. $roleName = $this->adminRoleModel->where('id',$role_id)->value('name');
  189. $username = $this->adminModel->where('id',$uid)->value('username');
  190. // 记录日志
  191. $LogStr = "编辑账号:".$username.",手机号:".$_POST['mobile'].",角色:" .$roleName;
  192. $this->insertLog($this->current_node,$LogStr,161);
  193. $this->success("保存成功!",url("user/index"));
  194. } else {
  195. $this->error("保存失败!");
  196. }
  197. }
  198. } else {
  199. $this->error("请为此用户指定角色!");
  200. }
  201. }
  202. }
  203. public function delete()
  204. {
  205. $id = $this->request->param('id', 0, 'intval');
  206. if ($id == 1) {
  207. $this->error("最高管理员不能删除!");
  208. }
  209. $username = $this->adminModel->where('id',$id)->value('username');
  210. if ($this->adminModel->delete($id) !== false) {
  211. $this->adminRoleUserModel->where(["user_id" => $id])->delete();
  212. // 记录日志
  213. $LogStr = "删除账号:" . $username;
  214. $this->insertLog($this->current_node,$LogStr,161);
  215. $this->success("删除成功!");
  216. } else {
  217. $this->error("删除失败!");
  218. }
  219. }
  220. public function ban()
  221. {
  222. $id = $this->request->param('id', 0, 'intval');
  223. if (!empty($id)) {
  224. $result = $this->adminModel->where(["id" => $id])->setField('status', '0');
  225. if ($result !== false) {
  226. $username = $this->adminModel->where('id',$id)->value('username');
  227. // 记录日志
  228. $LogStr = "冻结账号:" . $username;
  229. $this->insertLog($this->current_node,$LogStr,161);
  230. $this->success("管理员停用成功!", url("user/index"));
  231. } else {
  232. $this->error('管理员停用失败!');
  233. }
  234. } else {
  235. $this->error('数据传入失败!');
  236. }
  237. }
  238. public function cancelBan()
  239. {
  240. $id = $this->request->param('id', 0, 'intval');
  241. if (!empty($id)) {
  242. $result = $this->adminModel->where(["id" => $id])->setField('status', '1');
  243. if ($result !== false) {
  244. $username = $this->adminModel->where('id',$id)->value('username');
  245. // 记录日志
  246. $LogStr = "启用账号:" . $username;
  247. $this->insertLog($this->current_node,$LogStr,161);
  248. $this->success("管理员启用成功!", url("user/index"));
  249. } else {
  250. $this->error('管理员启用失败!');
  251. }
  252. } else {
  253. $this->error('数据传入失败!');
  254. }
  255. }
  256. // 修改密码页
  257. public function modifyPassword()
  258. {
  259. return $this->fetch('modify_password');
  260. }
  261. // 修改密码处理
  262. public function updatePwd()
  263. {
  264. $old_password = input('old_password');
  265. $new_password = input('new_password');
  266. $confirm_new_password = input('confirm_new_password');
  267. $data = [
  268. 'new_password' => $new_password,
  269. 'old_password' => $old_password
  270. ];
  271. $result = $this->validate($data, [
  272. ['new_password', 'require|length:6,50', '请输入新密码|新密码长度错误'],
  273. ['old_password', 'require|length:6,50', '请输入旧密码|旧密码长度错误'],
  274. ]);
  275. if (true !== $result) {
  276. $this->error($result);
  277. }
  278. $adminInfo = $this->adminModel->where([
  279. 'id' => session('ADMIN_ID'),
  280. 'type' => 1,
  281. 'password' => mg_password($old_password)
  282. ])->find();
  283. if (empty($adminInfo)) {
  284. $this->error('账号或密码错误!');
  285. }
  286. elseif ($new_password != $confirm_new_password) {
  287. $this->error('两次密码不一致');
  288. }
  289. $ret = $this->adminModel->where(["id" => session('ADMIN_ID'),'type'=> 1])->update(['password_update_time'=>NOW_TIMESTAMP,'password'=>mg_password($new_password)]);
  290. if ($ret) {
  291. $this->success('密码修改成功!','index/index');
  292. } else {
  293. $this->error('密码修改失败!');
  294. }
  295. }
  296. }