UserController.php 12 KB

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