Index.php 4.0 KB

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