Index.php 2.9 KB

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