Adminlogin.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. /**
  3. * User: 开发者
  4. * QQ: 1123416584
  5. * web: blog.hnh117.com
  6. */
  7. namespace app\admin\controller;
  8. use addons\adminlogin\library\Service;
  9. use app\admin\model\AdminLog;
  10. use app\common\controller\Backend;
  11. use think\Cache;
  12. use think\Config;
  13. use think\Hook;
  14. use think\Lang;
  15. use think\Request;
  16. use think\Session;
  17. use think\Validate;
  18. class Adminlogin extends Backend
  19. {
  20. protected $noNeedLogin = ['index', 'login'];
  21. public function __construct(Request $request = null)
  22. {
  23. $addonConfig = get_addon_config('adminlogin');
  24. if ($addonConfig['is_origin']) {
  25. $this->redirect('index/login');
  26. }
  27. parent::__construct($request);
  28. }
  29. public function login()
  30. {
  31. if (!$this->request->isPost()) {
  32. $this->error(404);
  33. }
  34. AdminLog::setTitle(__('Login'));
  35. $url = Service::getUrl();
  36. $addonConfig = get_addon_config('adminlogin');
  37. if (false == $addonConfig['dev']) {
  38. if ($this->auth->isLogin()) {
  39. $this->success(__("You've logged in, do not login again"), $url);
  40. }
  41. }
  42. // 必须带上i用cache来缓存信息,不能用session
  43. $cacheKey = 'addon_adminlogin_error_'.request()->ip();
  44. // 错误多少次需要输入验证码
  45. $maxError = $addonConfig['num'];
  46. $errorNum = Cache::tag('adminlogin')->get($cacheKey) ?: 0;
  47. $hasCaptcha = $errorNum >= $maxError;
  48. $nextHasCaptcha = $errorNum >= $maxError-1;
  49. if ($this->request->isPost()) {
  50. $username = $this->request->post('username');
  51. $password = $this->request->post('password');
  52. $keeplogin = $this->request->post('keeplogin');
  53. $token = $this->request->post('__token__');
  54. $rule = [
  55. 'username' => 'require|length:3,30',
  56. 'password' => 'require|length:3,30',
  57. '__token__' => 'require|token',
  58. ];
  59. $data = [
  60. 'username' => $username,
  61. 'password' => $password,
  62. '__token__' => $token,
  63. ];
  64. if (Config::get('fastadmin.login_captcha') && $hasCaptcha) {
  65. $rule['captcha'] = 'require|captcha';
  66. $data['captcha'] = $this->request->post('captcha');
  67. }
  68. $validate = new Validate($rule, [], ['username' => __('Username'), 'password' => __('Password'), 'captcha' => __('Captcha')]);
  69. $result = $validate->check($data);
  70. if (!$result) {
  71. $this->error($validate->getError(), $url, ['token' => $this->request->token()]);
  72. }
  73. AdminLog::setTitle(__('Login'));
  74. $result = $this->auth->login($username, $password, $keeplogin ? 86400 : 0);
  75. if ($result === true) {
  76. Cache::tag('adminlogin')->set($cacheKey, 0);
  77. Hook::listen("admin_login_after", $this->request);
  78. $this->success(__('Login successful'), $url, ['url' => $url, 'id' => $this->auth->id, 'username' => $username, 'avatar' => $this->auth->avatar]);
  79. } else {
  80. // 记录密码错误次数
  81. Cache::tag('adminlogin')->set($cacheKey, $errorNum+1);
  82. $msg = $this->auth->getError();
  83. $msg = $msg ? $msg : __('Username or password is incorrect');
  84. $this->error($msg, $url, ['token' => $this->request->token(), 'has_captcha' => $nextHasCaptcha]);
  85. }
  86. }
  87. }
  88. /**
  89. * 管理员登录
  90. */
  91. public function index()
  92. {
  93. $url = Service::getUrl();
  94. $addonConfig = get_addon_config('adminlogin');
  95. if (false == $addonConfig['dev']) {
  96. if ($this->auth->isLogin()) {
  97. $this->success(__("You've logged in, do not login again"), $url);
  98. }
  99. }
  100. $templateType = $addonConfig['type'];
  101. if (true == $addonConfig['dev']) {
  102. $templateType = input('type', $templateType);
  103. }
  104. // 必须带上i用cache来缓存信息,不能用session
  105. $cacheKey = 'addon_adminlogin_error_'.request()->ip();
  106. // 错误多少次需要输入验证码
  107. $maxError = $addonConfig['num'];
  108. $errorNum = Cache::tag('adminlogin')->get($cacheKey) ?: 0;
  109. $hasCaptcha = $errorNum >= $maxError;
  110. if ($this->request->isPost()) {
  111. $this->error(404);
  112. }
  113. // 根据客户端的cookie,判断是否可以自动登录
  114. if ($this->auth->autologin() && false == $addonConfig['dev']) {
  115. Session::delete("referer");
  116. $this->redirect($url);
  117. }
  118. $background = Config::get('fastadmin.login_background');
  119. $background = $background ? (stripos($background, 'http') === 0 ? $background : config('site.cdnurl') . $background) : '';
  120. $this->view->assign('background', $background);
  121. $this->view->assign('title', __('Login'));
  122. Hook::listen("admin_login_init", $this->request);
  123. $this->view->assign('hasCaptcha', $hasCaptcha);
  124. $template = 'login';
  125. if ($templateType > 1) {
  126. $template = $template.$templateType;
  127. }
  128. $templateTypeList = [];
  129. if (true == $addonConfig['dev']) {
  130. $templateTypeList = get_addon_fullconfig('adminlogin')[0]['content'];
  131. $i = 1;
  132. foreach ($templateTypeList as &$item) {
  133. $item = "{$i}、{$item}";
  134. $i++;
  135. }
  136. }
  137. $this->assign('templateTypeList', $templateTypeList);
  138. $this->assignconfig('hasCaptcha', $hasCaptcha);
  139. return $this->view->fetch($template);
  140. }
  141. }