Index.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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, $fixedmenu, $referermenu) = $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('fixedmenu', $fixedmenu);
  42. $this->view->assign('referermenu', $referermenu);
  43. $this->view->assign('title', __('Home'));
  44. return $this->view->fetch();
  45. }
  46. /**
  47. * 管理员登录
  48. */
  49. public function login()
  50. {
  51. $url = $this->request->get('url', 'index/index');
  52. if ($this->auth->isLogin()) {
  53. $this->success(__("You've logged in, do not login again"), $url);
  54. }
  55. if ($this->request->isPost()) {
  56. $username = $this->request->post('username');
  57. $password = $this->request->post('password');
  58. $keeplogin = $this->request->post('keeplogin');
  59. $token = $this->request->post('__token__');
  60. $rule = [
  61. 'username' => 'require|length:3,30',
  62. 'password' => 'require|length:3,30',
  63. '__token__' => 'token',
  64. ];
  65. $data = [
  66. 'username' => $username,
  67. 'password' => $password,
  68. '__token__' => $token,
  69. ];
  70. if (Config::get('fastadmin.login_captcha')) {
  71. $rule['captcha'] = 'require|captcha';
  72. $data['captcha'] = $this->request->post('captcha');
  73. }
  74. $validate = new Validate($rule, [], ['username' => __('Username'), 'password' => __('Password'), 'captcha' => __('Captcha')]);
  75. $result = $validate->check($data);
  76. if (!$result) {
  77. $this->error($validate->getError(), $url, ['token' => $this->request->token()]);
  78. }
  79. AdminLog::setTitle(__('Login'));
  80. $result = $this->auth->login($username, $password, $keeplogin ? 86400 : 0);
  81. if ($result === true) {
  82. Hook::listen("admin_login_after", $this->request);
  83. $this->success(__('Login successful'), $url, ['url' => $url, 'id' => $this->auth->id, 'username' => $username, 'avatar' => $this->auth->avatar]);
  84. } else {
  85. $msg = $this->auth->getError();
  86. $msg = $msg ? $msg : __('Username or password is incorrect');
  87. $this->error($msg, $url, ['token' => $this->request->token()]);
  88. }
  89. }
  90. // 根据客户端的cookie,判断是否可以自动登录
  91. if ($this->auth->autologin()) {
  92. $this->redirect($url);
  93. }
  94. $background = Config::get('fastadmin.login_background');
  95. $background = stripos($background, 'http') === 0 ? $background : config('site.cdnurl') . $background;
  96. $this->view->assign('background', $background);
  97. $this->view->assign('title', __('Login'));
  98. Hook::listen("admin_login_init", $this->request);
  99. return $this->view->fetch();
  100. }
  101. /**
  102. * 注销登录
  103. */
  104. public function logout()
  105. {
  106. $this->auth->logout();
  107. Hook::listen("admin_logout_after", $this->request);
  108. $this->success(__('Logout successful'), 'index/login');
  109. }
  110. }