UserController.php 12 KB

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