UserController.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 小夏 < 449134904@qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\admin\controller;
  12. use cmf\controller\AdminBaseController;
  13. use think\Db;
  14. use think\db\Query;
  15. /**
  16. * Class UserController
  17. * @package app\admin\controller
  18. * @adminMenuRoot(
  19. * 'name' => '管理组',
  20. * 'action' => 'default',
  21. * 'parent' => 'user/AdminIndex/default',
  22. * 'display'=> true,
  23. * 'order' => 10000,
  24. * 'icon' => '',
  25. * 'remark' => '管理组'
  26. * )
  27. */
  28. class UserController extends AdminBaseController
  29. {
  30. /**
  31. * 管理员列表
  32. * @adminMenu(
  33. * 'name' => '管理员',
  34. * 'parent' => 'default',
  35. * 'display'=> true,
  36. * 'hasView'=> true,
  37. * 'order' => 10000,
  38. * 'icon' => '',
  39. * 'remark' => '管理员管理',
  40. * 'param' => ''
  41. * )
  42. * @throws \think\exception\DbException
  43. */
  44. public function index()
  45. {
  46. $content = hook_one('admin_user_index_view');
  47. if (!empty($content)) {
  48. return $content;
  49. }
  50. /**搜索条件**/
  51. $userLogin = $this->request->param('user_login');
  52. $userEmail = trim($this->request->param('user_email'));
  53. $users = Db::name('user')
  54. ->where('user_type', 1)
  55. ->where(function (Query $query) use ($userLogin, $userEmail) {
  56. if ($userLogin) {
  57. $query->where('user_login', 'like', "%$userLogin%");
  58. }
  59. if ($userEmail) {
  60. $query->where('user_email', 'like', "%$userEmail%");
  61. }
  62. })
  63. ->order("id DESC")
  64. ->paginate(10);
  65. $users->appends(['user_login' => $userLogin, 'user_email' => $userEmail]);
  66. // 获取分页显示
  67. $page = $users->render();
  68. $rolesSrc = Db::name('role')->select();
  69. $roles = [];
  70. foreach ($rolesSrc as $r) {
  71. $roleId = $r['id'];
  72. $roles["$roleId"] = $r;
  73. }
  74. $this->assign("page", $page);
  75. $this->assign("roles", $roles);
  76. $this->assign("users", $users);
  77. return $this->fetch();
  78. }
  79. /**
  80. * 管理员添加
  81. * @adminMenu(
  82. * 'name' => '管理员添加',
  83. * 'parent' => 'index',
  84. * 'display'=> false,
  85. * 'hasView'=> true,
  86. * 'order' => 10000,
  87. * 'icon' => '',
  88. * 'remark' => '管理员添加',
  89. * 'param' => ''
  90. * )
  91. */
  92. public function add()
  93. {
  94. $content = hook_one('admin_user_add_view');
  95. if (!empty($content)) {
  96. return $content;
  97. }
  98. $roles = Db::name('role')->where('status', 1)->order("id DESC")->select();
  99. $this->assign("roles", $roles);
  100. //显示类目
  101. $cates = Db::name('category')->select();
  102. $catelist = array();
  103. foreach($cates as $k=>$v){
  104. $catelist[$v['id']] = $v['catname'];
  105. }
  106. $this->assign('catelist',$catelist);
  107. //var_dump($catelist);die;
  108. return $this->fetch();
  109. }
  110. /**
  111. * 管理员添加提交
  112. * @adminMenu(
  113. * 'name' => '管理员添加提交',
  114. * 'parent' => 'index',
  115. * 'display'=> false,
  116. * 'hasView'=> false,
  117. * 'order' => 10000,
  118. * 'icon' => '',
  119. * 'remark' => '管理员添加提交',
  120. * 'param' => ''
  121. * )
  122. */
  123. public function addPost()
  124. {
  125. if ($this->request->isPost()) {
  126. if (!empty($_POST['role_id']) && is_array($_POST['role_id'])) {
  127. $role_ids = $_POST['role_id'];
  128. unset($_POST['role_id']);
  129. $result = $this->validate($this->request->param(), 'User');
  130. if ($result !== true) {
  131. $this->error($result);
  132. } else {
  133. $_POST['user_pass'] = cmf_password($_POST['user_pass']);
  134. $_POST['category'] = implode(',',$_POST['category']);
  135. // $_POST['user_type'] =$role_ids[0];
  136. $result = DB::name('user')->insertGetId($_POST);
  137. if ($result !== false) {
  138. //$role_user_model=M("RoleUser");
  139. foreach ($role_ids as $role_id) {
  140. if (cmf_get_current_admin_id() != 1 && $role_id == 1) {
  141. $this->error("为了网站的安全,非网站创建者不可创建超级管理员!");
  142. }
  143. Db::name('RoleUser')->insert(["role_id" => $role_id, "user_id" => $result]);
  144. }
  145. $this->success("添加成功!", url("user/index"));
  146. } else {
  147. $this->error("添加失败!");
  148. }
  149. }
  150. } else {
  151. $this->error("请为此用户指定角色!");
  152. }
  153. }
  154. }
  155. /**
  156. * 管理员编辑
  157. * @adminMenu(
  158. * 'name' => '管理员编辑',
  159. * 'parent' => 'index',
  160. * 'display'=> false,
  161. * 'hasView'=> true,
  162. * 'order' => 10000,
  163. * 'icon' => '',
  164. * 'remark' => '管理员编辑',
  165. * 'param' => ''
  166. * )
  167. */
  168. public function edit()
  169. {
  170. $content = hook_one('admin_user_edit_view');
  171. if (!empty($content)) {
  172. return $content;
  173. }
  174. $id = $this->request->param('id', 0, 'intval');
  175. $roles = DB::name('role')->where('status', 1)->order("id DESC")->select();
  176. $this->assign("roles", $roles);
  177. $role_ids = DB::name('RoleUser')->where("user_id", $id)->column("role_id");
  178. $this->assign("role_ids", $role_ids);
  179. $user = DB::name('user')->where("id", $id)->find();
  180. //显示类目
  181. $cates = Db::name('category')->select();
  182. $catelist = array();
  183. foreach($cates as $k=>$v){
  184. $catelist[$v['id']] = $v['catname'];
  185. }
  186. //顺便也显示用户的下的字段,所属类目字段
  187. $this->assign('catelist',$catelist);
  188. $this->assign($user);
  189. return $this->fetch();
  190. }
  191. /**
  192. * 管理员编辑提交
  193. * @adminMenu(
  194. * 'name' => '管理员编辑提交',
  195. * 'parent' => 'index',
  196. * 'display'=> false,
  197. * 'hasView'=> false,
  198. * 'order' => 10000,
  199. * 'icon' => '',
  200. * 'remark' => '管理员编辑提交',
  201. * 'param' => ''
  202. * )
  203. */
  204. public function editPost()
  205. {
  206. if ($this->request->isPost()) {
  207. if (!empty($_POST['role_id']) && is_array($_POST['role_id'])) {
  208. if (empty($_POST['user_pass'])) {
  209. unset($_POST['user_pass']);
  210. } else {
  211. $_POST['user_pass'] = cmf_password($_POST['user_pass']);
  212. }
  213. $role_ids = $this->request->param('role_id/a');
  214. unset($_POST['role_id']);
  215. $result = $this->validate($this->request->param(), 'User.edit');
  216. if ($result !== true) {
  217. // 验证失败 输出错误信息
  218. $this->error($result);
  219. } else {
  220. $_POST['category'] = implode(',',$_POST['category']);
  221. $result = DB::name('user')->update($_POST);
  222. if ($result !== false) {
  223. $uid = $this->request->param('id', 0, 'intval');
  224. DB::name("RoleUser")->where("user_id", $uid)->delete();
  225. foreach ($role_ids as $role_id) {
  226. if (cmf_get_current_admin_id() != 1 && $role_id == 1) {
  227. $this->error("为了网站的安全,非网站创建者不可创建超级管理员!");
  228. }
  229. DB::name("RoleUser")->insert(["role_id" => $role_id, "user_id" => $uid]);
  230. }
  231. $this->success("保存成功!");
  232. } else {
  233. $this->error("保存失败!");
  234. }
  235. }
  236. } else {
  237. $this->error("请为此用户指定角色!");
  238. }
  239. }
  240. }
  241. /**
  242. * 管理员个人信息修改
  243. * @adminMenu(
  244. * 'name' => '个人信息',
  245. * 'parent' => 'index',
  246. * 'display'=> false,
  247. * 'hasView'=> true,
  248. * 'order' => 10000,
  249. * 'icon' => '',
  250. * 'remark' => '管理员个人信息修改',
  251. * 'param' => ''
  252. * )
  253. */
  254. public function userInfo()
  255. {
  256. $id = cmf_get_current_admin_id();
  257. $user = Db::name('user')->where("id", $id)->find();
  258. $this->assign($user);
  259. return $this->fetch();
  260. }
  261. /**
  262. * 管理员个人信息修改提交
  263. * @adminMenu(
  264. * 'name' => '管理员个人信息修改提交',
  265. * 'parent' => 'index',
  266. * 'display'=> false,
  267. * 'hasView'=> false,
  268. * 'order' => 10000,
  269. * 'icon' => '',
  270. * 'remark' => '管理员个人信息修改提交',
  271. * 'param' => ''
  272. * )
  273. */
  274. public function userInfoPost()
  275. {
  276. if ($this->request->isPost()) {
  277. $data = $this->request->post();
  278. $data['birthday'] = strtotime($data['birthday']);
  279. $data['id'] = cmf_get_current_admin_id();
  280. $create_result = Db::name('user')->update($data);;
  281. if ($create_result !== false) {
  282. $this->success("保存成功!");
  283. } else {
  284. $this->error("保存失败!");
  285. }
  286. }
  287. }
  288. /**
  289. * 管理员删除
  290. * @adminMenu(
  291. * 'name' => '管理员删除',
  292. * 'parent' => 'index',
  293. * 'display'=> false,
  294. * 'hasView'=> false,
  295. * 'order' => 10000,
  296. * 'icon' => '',
  297. * 'remark' => '管理员删除',
  298. * 'param' => ''
  299. * )
  300. */
  301. public function delete()
  302. {
  303. $id = $this->request->param('id', 0, 'intval');
  304. if ($id == 1) {
  305. $this->error("最高管理员不能删除!");
  306. }
  307. if (Db::name('user')->delete($id) !== false) {
  308. Db::name("RoleUser")->where("user_id", $id)->delete();
  309. $this->success("删除成功!");
  310. } else {
  311. $this->error("删除失败!");
  312. }
  313. }
  314. /**
  315. * 停用管理员
  316. * @adminMenu(
  317. * 'name' => '停用管理员',
  318. * 'parent' => 'index',
  319. * 'display'=> false,
  320. * 'hasView'=> false,
  321. * 'order' => 10000,
  322. * 'icon' => '',
  323. * 'remark' => '停用管理员',
  324. * 'param' => ''
  325. * )
  326. */
  327. public function ban()
  328. {
  329. $id = $this->request->param('id', 0, 'intval');
  330. if (!empty($id)) {
  331. $result = Db::name('user')->where(["id" => $id, "user_type" => 1])->setField('user_status', '0');
  332. if ($result !== false) {
  333. $this->success("管理员停用成功!", url("user/index"));
  334. } else {
  335. $this->error('管理员停用失败!');
  336. }
  337. } else {
  338. $this->error('数据传入失败!');
  339. }
  340. }
  341. /**
  342. * 启用管理员
  343. * @adminMenu(
  344. * 'name' => '启用管理员',
  345. * 'parent' => 'index',
  346. * 'display'=> false,
  347. * 'hasView'=> false,
  348. * 'order' => 10000,
  349. * 'icon' => '',
  350. * 'remark' => '启用管理员',
  351. * 'param' => ''
  352. * )
  353. */
  354. public function cancelBan()
  355. {
  356. $id = $this->request->param('id', 0, 'intval');
  357. if (!empty($id)) {
  358. $result = Db::name('user')->where(["id" => $id, "user_type" => 1])->setField('user_status', '1');
  359. if ($result !== false) {
  360. $this->success("管理员启用成功!", url("user/index"));
  361. } else {
  362. $this->error('管理员启用失败!');
  363. }
  364. } else {
  365. $this->error('数据传入失败!');
  366. }
  367. }
  368. }