UserController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. <?php
  2. // +—————————————————————————————————————————————————————————————————————
  3. // | Created by Yunbao
  4. // +—————————————————————————————————————————————————————————————————————
  5. // | Copyright (c) 2013~2022 http://www.yunbaokj.com All rights reserved.
  6. // +—————————————————————————————————————————————————————————————————————
  7. // | Author: https://gitee.com/yunbaokeji
  8. // +—————————————————————————————————————————————————————————————————————
  9. // | Date: 2022-02-17
  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. return $this->fetch();
  101. }
  102. /**
  103. * 管理员添加提交
  104. * @adminMenu(
  105. * 'name' => '管理员添加提交',
  106. * 'parent' => 'index',
  107. * 'display'=> false,
  108. * 'hasView'=> false,
  109. * 'order' => 10000,
  110. * 'icon' => '',
  111. * 'remark' => '管理员添加提交',
  112. * 'param' => ''
  113. * )
  114. */
  115. public function addPost()
  116. {
  117. if ($this->request->isPost()) {
  118. if (!empty($_POST['role_id']) && is_array($_POST['role_id'])) {
  119. $role_ids = $_POST['role_id'];
  120. unset($_POST['role_id']);
  121. $result = $this->validate($this->request->param(), 'User');
  122. if ($result !== true) {
  123. $this->error($result);
  124. } else {
  125. $_POST['user_pass'] = cmf_password($_POST['user_pass']);
  126. $result = DB::name('user')->insertGetId($_POST);
  127. if ($result !== false) {
  128. //$role_user_model=M("RoleUser");
  129. foreach ($role_ids as $role_id) {
  130. if (cmf_get_current_admin_id() != 1 && $role_id == 1) {
  131. $this->error("为了网站的安全,非网站创建者不可创建超级管理员!");
  132. }
  133. Db::name('RoleUser')->insert(["role_id" => $role_id, "user_id" => $result]);
  134. }
  135. $this->success("添加成功!", url("user/index"));
  136. } else {
  137. $this->error("添加失败!");
  138. }
  139. }
  140. } else {
  141. $this->error("请为此用户指定角色!");
  142. }
  143. }
  144. }
  145. /**
  146. * 管理员编辑
  147. * @adminMenu(
  148. * 'name' => '管理员编辑',
  149. * 'parent' => 'index',
  150. * 'display'=> false,
  151. * 'hasView'=> true,
  152. * 'order' => 10000,
  153. * 'icon' => '',
  154. * 'remark' => '管理员编辑',
  155. * 'param' => ''
  156. * )
  157. */
  158. public function edit()
  159. {
  160. $content = hook_one('admin_user_edit_view');
  161. if (!empty($content)) {
  162. return $content;
  163. }
  164. $id = $this->request->param('id', 0, 'intval');
  165. $roles = DB::name('role')->where('status', 1)->order("id DESC")->select();
  166. $this->assign("roles", $roles);
  167. $role_ids = DB::name('RoleUser')->where("user_id", $id)->column("role_id");
  168. $this->assign("role_ids", $role_ids);
  169. $user = DB::name('user')->where("id", $id)->find();
  170. $this->assign($user);
  171. return $this->fetch();
  172. }
  173. /**
  174. * 管理员编辑提交
  175. * @adminMenu(
  176. * 'name' => '管理员编辑提交',
  177. * 'parent' => 'index',
  178. * 'display'=> false,
  179. * 'hasView'=> false,
  180. * 'order' => 10000,
  181. * 'icon' => '',
  182. * 'remark' => '管理员编辑提交',
  183. * 'param' => ''
  184. * )
  185. */
  186. public function editPost()
  187. {
  188. if ($this->request->isPost()) {
  189. if (!empty($_POST['role_id']) && is_array($_POST['role_id'])) {
  190. if (empty($_POST['user_pass'])) {
  191. unset($_POST['user_pass']);
  192. } else {
  193. $_POST['user_pass'] = cmf_password($_POST['user_pass']);
  194. }
  195. $role_ids = $this->request->param('role_id/a');
  196. unset($_POST['role_id']);
  197. $result = $this->validate($this->request->param(), 'User.edit');
  198. if ($result !== true) {
  199. // 验证失败 输出错误信息
  200. $this->error($result);
  201. } else {
  202. $result = DB::name('user')->update($_POST);
  203. if ($result !== false) {
  204. $uid = $this->request->param('id', 0, 'intval');
  205. DB::name("RoleUser")->where("user_id", $uid)->delete();
  206. foreach ($role_ids as $role_id) {
  207. if (cmf_get_current_admin_id() != 1 && $role_id == 1) {
  208. $this->error("为了网站的安全,非网站创建者不可创建超级管理员!");
  209. }
  210. DB::name("RoleUser")->insert(["role_id" => $role_id, "user_id" => $uid]);
  211. }
  212. $this->success("保存成功!");
  213. } else {
  214. $this->error("保存失败!");
  215. }
  216. }
  217. } else {
  218. $this->error("请为此用户指定角色!");
  219. }
  220. }
  221. }
  222. /**
  223. * 管理员个人信息修改
  224. * @adminMenu(
  225. * 'name' => '个人信息',
  226. * 'parent' => 'index',
  227. * 'display'=> false,
  228. * 'hasView'=> true,
  229. * 'order' => 10000,
  230. * 'icon' => '',
  231. * 'remark' => '管理员个人信息修改',
  232. * 'param' => ''
  233. * )
  234. */
  235. public function userInfo()
  236. {
  237. $id = cmf_get_current_admin_id();
  238. $user = Db::name('user')->where("id", $id)->find();
  239. $this->assign($user);
  240. return $this->fetch();
  241. }
  242. /**
  243. * 管理员个人信息修改提交
  244. * @adminMenu(
  245. * 'name' => '管理员个人信息修改提交',
  246. * 'parent' => 'index',
  247. * 'display'=> false,
  248. * 'hasView'=> false,
  249. * 'order' => 10000,
  250. * 'icon' => '',
  251. * 'remark' => '管理员个人信息修改提交',
  252. * 'param' => ''
  253. * )
  254. */
  255. public function userInfoPost()
  256. {
  257. if ($this->request->isPost()) {
  258. $data = $this->request->post();
  259. $data['birthday'] = strtotime($data['birthday']);
  260. $data['id'] = cmf_get_current_admin_id();
  261. $create_result = Db::name('user')->update($data);
  262. if ($create_result !== false) {
  263. $this->success("保存成功!");
  264. } else {
  265. $this->error("保存失败!");
  266. }
  267. }
  268. }
  269. /**
  270. * 管理员删除
  271. * @adminMenu(
  272. * 'name' => '管理员删除',
  273. * 'parent' => 'index',
  274. * 'display'=> false,
  275. * 'hasView'=> false,
  276. * 'order' => 10000,
  277. * 'icon' => '',
  278. * 'remark' => '管理员删除',
  279. * 'param' => ''
  280. * )
  281. */
  282. public function delete()
  283. {
  284. $id = $this->request->param('id', 0, 'intval');
  285. if ($id == 1) {
  286. $this->error("最高管理员不能删除!");
  287. }
  288. if (Db::name('user')->delete($id) !== false) {
  289. Db::name("RoleUser")->where("user_id", $id)->delete();
  290. $this->success("删除成功!");
  291. } else {
  292. $this->error("删除失败!");
  293. }
  294. }
  295. /**
  296. * 停用管理员
  297. * @adminMenu(
  298. * 'name' => '停用管理员',
  299. * 'parent' => 'index',
  300. * 'display'=> false,
  301. * 'hasView'=> false,
  302. * 'order' => 10000,
  303. * 'icon' => '',
  304. * 'remark' => '停用管理员',
  305. * 'param' => ''
  306. * )
  307. */
  308. public function ban()
  309. {
  310. $id = $this->request->param('id', 0, 'intval');
  311. if (!empty($id)) {
  312. $result = Db::name('user')->where(["id" => $id, "user_type" => 1])->setField('user_status', '0');
  313. if ($result !== false) {
  314. $this->success("管理员停用成功!", url("user/index"));
  315. } else {
  316. $this->error('管理员停用失败!');
  317. }
  318. } else {
  319. $this->error('数据传入失败!');
  320. }
  321. }
  322. /**
  323. * 启用管理员
  324. * @adminMenu(
  325. * 'name' => '启用管理员',
  326. * 'parent' => 'index',
  327. * 'display'=> false,
  328. * 'hasView'=> false,
  329. * 'order' => 10000,
  330. * 'icon' => '',
  331. * 'remark' => '启用管理员',
  332. * 'param' => ''
  333. * )
  334. */
  335. public function cancelBan()
  336. {
  337. $id = $this->request->param('id', 0, 'intval');
  338. if (!empty($id)) {
  339. $result = Db::name('user')->where(["id" => $id, "user_type" => 1])->setField('user_status', '1');
  340. if ($result !== false) {
  341. $this->success("管理员启用成功!", url("user/index"));
  342. } else {
  343. $this->error('管理员启用失败!');
  344. }
  345. } else {
  346. $this->error('数据传入失败!');
  347. }
  348. }
  349. }