Admin.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: 中闽 < 1464674022@qq.com >
  5. * Date: 2023/02/05
  6. * Time: 20:33
  7. */
  8. namespace app\admin\controller;
  9. use app\admin\controller\base\Permissions;
  10. use app\admin\model\Admin as adminModel;
  11. use think\Db;
  12. use think\Session;
  13. class Admin extends Permissions
  14. {
  15. /**
  16. * 管理员列表
  17. * @return mixed
  18. */
  19. public function index()
  20. {
  21. $model = new adminModel();
  22. if ($this->request->isAjax()) {
  23. $post = $this->request->param();
  24. $where = [];
  25. if (isset($post['keywords']) and !empty($post['keywords'])) {
  26. $where['nickname'] = ['like', '%' . $post['keywords'] . '%'];
  27. }
  28. if (isset($post['admin_cate_id']) and $post['admin_cate_id'] > 0) {
  29. $where['admin_cate_id'] = $post['admin_cate_id'];
  30. }
  31. if (isset($post['create_time']) and !empty($post['create_time'])) {
  32. $min_time = strtotime($post['create_time']);
  33. $max_time = $min_time + 24 * 60 * 60;
  34. $where['create_time'] = [['>=', $min_time], ['<=', $max_time]];
  35. }
  36. $count = $model->where($where)->count();
  37. $data = $model->where($where)->page($post['page']??0, $post['limit']??15)->order('admin_cate_id desc')->select();
  38. foreach ($data as $k => $v) {
  39. $v['cate_name'] = $v->admincate->name;
  40. $v['head_pic'] = geturl($v->thumb, '/static/public/images/tx.jpg');
  41. $data[$k] = $v;
  42. }
  43. return array('code' => 0, 'count' => $count, 'data' => $data);
  44. } else {
  45. $this->assign('cate', Db::name('admin_cate')->select());
  46. return $this->fetch();
  47. }
  48. }
  49. /**
  50. * 管理员的添加及修改
  51. * @return mixed
  52. */
  53. public function publish()
  54. {
  55. $id = $this->request->param('id', 0, 'intval');
  56. $model = new adminModel();
  57. if ($id > 0) {
  58. if ($this->request->isPost()) {
  59. $post = $this->request->post();
  60. $validate = new \think\Validate([
  61. ['admin_cate_id', 'require', '请选择角色'],
  62. ]);
  63. if (!$validate->check($post)) {
  64. $this->error('提交失败:' . $validate->getError());
  65. }
  66. //验证昵称是否存在
  67. $nickname = $model->where(['nickname' => $post['nickname'], 'id' => ['neq', $post['id']]])->select();
  68. if (!empty($nickname)) {
  69. $this->error('提交失败:该昵称已被占用');
  70. }
  71. if (false == $model->allowField(true)->save($post, ['id' => $id])) {
  72. $this->error('修改失败');
  73. } else {
  74. $this->success('修改管理员信息成功', 'admin/admin/index');
  75. }
  76. } else {
  77. $info['admin'] = $model->where('id', $id)->find();
  78. $info['admin_cate'] = Db::name('admin_cate')->select();
  79. $this->assign('info', $info);
  80. return $this->fetch();
  81. }
  82. } else {
  83. //是新增操作
  84. if ($this->request->isPost()) {
  85. $post = $this->request->post();
  86. $validate = new \think\Validate([
  87. ['name|账号', 'require|alphaDash'],
  88. ['password', 'require|confirm', '密码不能为空|两次密码不一致'],
  89. ['password_confirm', 'require', '重复密码不能为空'],
  90. ['admin_cate_id', 'require', '请选择角色'],
  91. ]);
  92. if (!$validate->check($post)) {
  93. $this->error('提交失败:' . $validate->getError());
  94. }
  95. //验证用户名是否存在
  96. $name = $model->where('name', $post['name'])->select();
  97. if (!empty($name)) {
  98. $this->error('提交失败:该用户名已被注册');
  99. }
  100. //验证昵称是否存在
  101. $nickname = $model->where('nickname', $post['nickname'])->select();
  102. if (!empty($nickname)) {
  103. $this->error('提交失败:该昵称已被占用');
  104. }
  105. //密码处理
  106. $post['password'] = password($post['password']);
  107. if (false == $model->allowField(true)->save($post)) {
  108. $this->error('添加管理员失败');
  109. } else {
  110. $this->success('添加管理员成功', 'admin/admin/index');
  111. }
  112. } else {
  113. $info['admin_cate'] = Db::name('admin_cate')->select();
  114. $this->assign('info', $info);
  115. return $this->fetch();
  116. }
  117. }
  118. }
  119. /**
  120. * 管理员删除
  121. */
  122. public function delete()
  123. {
  124. if ($this->request->isAjax()) {
  125. $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
  126. if ($id == 1) {
  127. $this->error('网站所有者不能被删除');
  128. }
  129. if ($id == Session::get(self::ADMIN_ID)) {
  130. $this->error('自己不能删除自己');
  131. }
  132. if (false == Db::name('admin')->where('id', $id)->delete()) {
  133. $this->error('删除失败');
  134. } else {
  135. $this->success('删除成功', 'admin/admin/index');
  136. }
  137. }
  138. }
  139. //重置密码
  140. public function resetpass()
  141. {
  142. if ($this->request->isAjax()) {
  143. $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
  144. if ($id == 1) {
  145. $this->error('admin不能被重置');
  146. }
  147. if ($id == Session::get(self::ADMIN_ID)) {
  148. $this->error('不能重置自己账号');
  149. }
  150. if (false == Db::name('admin')->where('id', $id)->update(['password' => password(123456)])) {
  151. $this->error('重置失败');
  152. } else {
  153. $this->success('重置成功', 'admin/admin/index');
  154. }
  155. }
  156. }
  157. /**
  158. * 管理员个人资料修改
  159. * @return mixed
  160. */
  161. public function personal()
  162. {
  163. $id = Session::get(self::ADMIN_ID);
  164. $model = new adminModel();
  165. if ($id > 0) {
  166. $admin = $model->where('id', $id)->find();
  167. if ($this->request->isPost()) {
  168. $thumb = $this->request->post('thumb');
  169. $nickname = $this->request->post('nickname');
  170. if (false == $admin->save(['thumb' => $thumb, 'nickname' => $nickname])) {
  171. $this->error('修改失败');
  172. } else {
  173. $this->success('修改个人信息成功', 'admin/admin/personal');
  174. }
  175. } else {
  176. $this->assign('info', $admin);
  177. return $this->fetch();
  178. }
  179. } else {
  180. $this->error('id不正确');
  181. }
  182. }
  183. /**
  184. * 修改密码
  185. * @return mixed
  186. */
  187. public function editPassword()
  188. {
  189. if ($this->request->isPost()) {
  190. $id = Session::get(self::ADMIN_ID);
  191. $post = $this->request->post();
  192. $validate = new \think\Validate([
  193. ['password', 'require', '原密码不能为空'],
  194. ['password', 'require|confirm', '新密码不能为空|确认密码不一致'],
  195. ['password_confirm', 'require', '确认密码不能为空'],
  196. ]);
  197. if (!$validate->check($post)) {
  198. $this->error('提交失败:' . $validate->getError());
  199. }
  200. $admin = Db::name('admin')->where('id', $id)->find();
  201. if (password($post['password_old']) == $admin['password']) {
  202. if (false == Db::name('admin')->where('id', $id)->update(['password' => password($post['password'])])) {
  203. $this->error('修改失败');
  204. } else {
  205. $this->success('修改成功', 'admin/main/index');
  206. }
  207. } else {
  208. $this->error('原密码错误');
  209. }
  210. } else {
  211. return $this->fetch();
  212. }
  213. }
  214. }