Admin.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\AdminBaseController;
  4. use app\common\model\AdminModel;
  5. use app\common\model\MenuModel;
  6. use app\admin\validate\AdminValidate;
  7. use think\exception\ValidateException;
  8. class Admin extends AdminBaseController
  9. {
  10. public function index()
  11. {
  12. return view('', [
  13. 'role_list' => AdminModel::ROLE,
  14. ]);
  15. }
  16. public function adminForm()
  17. {
  18. $id = input('id/d, 0');
  19. $info = AdminModel::findOrEmpty($id);
  20. $menulist = MenuModel::where(['mtype' => 'admin', 'status' => 1])->order(['pid' => 'asc', 'priority' => 'asc', 'id' => 'asc'])->select()->toArray();
  21. $powerarr = [];
  22. $poweridsarr = ($info->powerids == null || empty($info->powerids)) ? [] : explode(",", $info->powerids);
  23. if (!empty($menulist)) {
  24. foreach ($menulist as $k => $v) {
  25. if ($v['pid'] == 0) {
  26. $v['checked'] = false;
  27. $v['children'] = [];
  28. $v['spread'] = true;
  29. $powerarr[$v['id']] = $v;
  30. } else {
  31. $v['checked'] = in_array($v['id'], $poweridsarr);
  32. $powerarr[$v['pid']]['children'][] = $v;
  33. }
  34. }
  35. }
  36. return view('', [
  37. 'info' => $info,
  38. 'powerarr' => json_encode(array_values($powerarr)),
  39. ]);
  40. }
  41. public function editAdmin()
  42. {
  43. $id = input('id/d');
  44. $vdata = [
  45. 'id' => $id,
  46. 'admin_name' => input('admin_name/s'),
  47. 'realname' => input('realname/s'),
  48. 'mobile' => input('mobile/s'),
  49. ];
  50. try {
  51. validate(AdminValidate::class)->check($vdata);
  52. } catch (ValidateException $e) {
  53. ajax_return(1, $e->getError());
  54. }
  55. $password = input('password/s');
  56. $role = input('role/d', 2);
  57. $powerids = input('powerids/s', "");
  58. if ($role == 1) {
  59. $idsarr = MenuModel::where(['mtype' => 'admin', 'status' => 1])->order(['pid' => 'asc', 'priority' => 'asc', 'id' => 'asc'])->column('id');
  60. $powerids = implode(",", $idsarr);
  61. }
  62. $data = [
  63. 'role' => $role,
  64. 'admin_name' => input('admin_name/s', ""),
  65. 'realname' => input('realname/s', ""),
  66. 'mobile' => input('mobile/s', ""),
  67. 'status' => input('status/d') == 1 ? 1 : 2,
  68. 'powerids' => $powerids,
  69. 'remark' => input('remark', ""),
  70. ];
  71. if (empty($id)) {
  72. $data['password'] = empty($password) ? md5("123456789") : md5($password);
  73. $data['join_date'] = time();
  74. $data['join_ip'] = $_SERVER['SERVER_ADDR'];
  75. $data['last_date'] = time();
  76. $data['last_ip'] = $_SERVER['SERVER_ADDR'];
  77. AdminModel::create($data);
  78. } else {
  79. if (!empty($password)) {
  80. $data['password'] = md5($password);
  81. }
  82. AdminModel::update($data, ['id' => $id]);
  83. }
  84. ajax_return();
  85. }
  86. // 删除管理员
  87. public function delAdmin()
  88. {
  89. $access_admin = session('access_admin');
  90. $password = input('password');
  91. if ($access_admin['password'] !== md5($password)) {
  92. ajax_return(1, '操作密码验证失败');
  93. }
  94. $id_arr = input('id_arr/a');
  95. if (in_array(1, $id_arr)) {
  96. ajax_return(1, '无法删除超级管理员');
  97. }
  98. AdminModel::destroy($id_arr);
  99. ajax_return();
  100. }
  101. public function listAdmin()
  102. {
  103. $limit = input('limit');
  104. $page = input('page');
  105. $map = [];
  106. $admin_name = input('admin_name');
  107. if (!empty($admin_name)) {
  108. $map['admin_name'] = $admin_name;
  109. }
  110. $realname = input('realname');
  111. if (!empty($realname)) {
  112. $map['realname'] = $realname;
  113. }
  114. $mobile = input('mobile');
  115. if (!empty($mobile)) {
  116. $map['mobile'] = $mobile;
  117. }
  118. $role = input('role');
  119. if (!empty($role)) {
  120. $map['role'] = $role;
  121. }
  122. $list = AdminModel::where($map)->order('id', 'asc')->limit($limit)->page($page)->append(['status_text', 'role_text'])->select();
  123. $count = AdminModel::where($map)->count();
  124. if ($count == 0) {
  125. ajax_return(1, '未查询到数据');
  126. }
  127. list_return($list, $count);
  128. }
  129. // 个人信息
  130. public function myInfo()
  131. {
  132. $access_admin = session('access_admin');
  133. $admin = AdminModel::find($access_admin['id']);
  134. return view('', [
  135. 'admin' => $admin,
  136. ]);
  137. }
  138. public function editMyInfo()
  139. {
  140. $access_admin = session('access_admin');
  141. AdminModel::update(['realname' => input('realname'), 'mobile' => input('mobile'), 'remark' => input('remark')], ['id' => $access_admin['id']]);
  142. ajax_return();
  143. }
  144. public function myPassword()
  145. {
  146. return view('');
  147. }
  148. public function editMyPassword()
  149. {
  150. $access_admin = session('access_admin');
  151. $oldpassword = input('oldpassword');
  152. if ($access_admin['password'] !== md5($oldpassword)) {
  153. ajax_return(1, '当前密码不正确');
  154. }
  155. $password = input('password');
  156. $repassword = input('repassword');
  157. if ($password !== $repassword) {
  158. ajax_return(1, '两次输入的新密码不一致');
  159. }
  160. AdminModel::update(['password' => md5($password)], ['id' => $access_admin['id']]);
  161. session('access_admin', null);
  162. ajax_return();
  163. }
  164. }