Index.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use think\Validate;
  5. /**
  6. * 后台首页
  7. * @internal
  8. */
  9. class Index extends Backend
  10. {
  11. protected $noNeedLogin = ['login', 'logout'];
  12. protected $noNeedRight = ['index'];
  13. protected $layout = '';
  14. public function _initialize()
  15. {
  16. parent::_initialize();
  17. }
  18. /**
  19. * 后台首页
  20. */
  21. public function index()
  22. {
  23. //
  24. $menulist = $this->auth->getSidebar([
  25. 'dashboard' => 'hot',
  26. 'auth' => ['new', 'red', 'badge'],
  27. 'auth/admin' => 12,
  28. 'auth/rule' => 4,
  29. 'general' => ['18', 'purple'],
  30. ]);
  31. $this->view->assign('menulist', $menulist);
  32. $this->view->assign('title', __('Home'));
  33. return $this->view->fetch();
  34. }
  35. /**
  36. * 管理员登录
  37. */
  38. public function login()
  39. {
  40. $url = $this->request->get('url', 'index/index');
  41. if ($this->auth->isLogin())
  42. {
  43. $this->error(__("You've logged in, do not login again"), $url);
  44. return;
  45. }
  46. if ($this->request->isPost())
  47. {
  48. $username = $this->request->post('username');
  49. $password = $this->request->post('password');
  50. $keeplogin = $this->request->post('keeplogin');
  51. $token = $this->request->post('__token__');
  52. $rule = [
  53. 'username' => 'require|length:3,30',
  54. 'password' => 'require|length:3,30',
  55. '__token__' => 'token',
  56. ];
  57. $data = [
  58. 'username' => $username,
  59. 'password' => $password,
  60. '__token__' => $token,
  61. ];
  62. $validate = new Validate($rule);
  63. $result = $validate->check($data);
  64. if (!$result)
  65. {
  66. $this->error($validate->getError());
  67. return;
  68. }
  69. $result = $this->auth->login($username, $password, $keeplogin ? 86400 : 0);
  70. if ($result === true)
  71. {
  72. $this->success(__('Login successful'), $url);
  73. return;
  74. }
  75. else
  76. {
  77. $this->error(__('Username or password is incorrect'), $url);
  78. }
  79. return;
  80. }
  81. // 根据客户端的cookie,判断是否可以自动登录
  82. if ($this->auth->autologin())
  83. {
  84. $this->redirect($url);
  85. }
  86. $this->view->assign('title', __('Login'));
  87. return $this->view->fetch();
  88. }
  89. /**
  90. * 注销登录
  91. */
  92. public function logout()
  93. {
  94. $this->auth->logout();
  95. $this->success(__('Logout successful'), 'index/login');
  96. return;
  97. }
  98. }