Index.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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'];
  12. protected $noNeedRight = ['index', 'logout'];
  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. 'addon' => ['new', 'red', 'badge'],
  27. 'auth/rule' => 'side',
  28. 'general' => ['new', 'purple'],
  29. ], $this->view->site['fixedpage']);
  30. $this->view->assign('menulist', $menulist);
  31. $this->view->assign('title', __('Home'));
  32. return $this->view->fetch();
  33. }
  34. /**
  35. * 管理员登录
  36. */
  37. public function login()
  38. {
  39. $url = $this->request->get('url', 'index/index');
  40. if ($this->auth->isLogin())
  41. {
  42. $this->error(__("You've logged in, do not login again"), $url);
  43. }
  44. if ($this->request->isPost())
  45. {
  46. $username = $this->request->post('username');
  47. $password = $this->request->post('password');
  48. $keeplogin = $this->request->post('keeplogin');
  49. $token = $this->request->post('__token__');
  50. $rule = [
  51. 'username' => 'require|length:3,30',
  52. 'password' => 'require|length:3,30',
  53. '__token__' => 'token',
  54. ];
  55. $data = [
  56. 'username' => $username,
  57. 'password' => $password,
  58. '__token__' => $token,
  59. ];
  60. $validate = new Validate($rule);
  61. $result = $validate->check($data);
  62. if (!$result)
  63. {
  64. $this->error($validate->getError(), $url, ['token' => $this->request->token()]);
  65. }
  66. \app\admin\model\AdminLog::setTitle(__('Login'));
  67. $result = $this->auth->login($username, $password, $keeplogin ? 86400 : 0);
  68. if ($result === true)
  69. {
  70. $this->success(__('Login successful'), $url, ['url' => $url, 'id' => $this->auth->id, 'username' => $username, 'avatar' => $this->auth->avatar]);
  71. }
  72. else
  73. {
  74. $this->error(__('Username or password is incorrect'), $url, ['token' => $this->request->token()]);
  75. }
  76. }
  77. // 根据客户端的cookie,判断是否可以自动登录
  78. if ($this->auth->autologin())
  79. {
  80. $this->redirect($url);
  81. }
  82. $background = cdnurl("/assets/img/loginbg.jpg");
  83. $this->view->assign('background', $background);
  84. \think\Hook::listen("login_init", $this->request);
  85. return $this->view->fetch();
  86. }
  87. /**
  88. * 注销登录
  89. */
  90. public function logout()
  91. {
  92. $this->auth->logout();
  93. $this->success(__('Logout successful'), 'index/login');
  94. }
  95. }