Index.php 5.1 KB

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