Login.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkAdmin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2019 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://demo.thinkadmin.top
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | gitee 代码仓库:https://gitee.com/zoujingli/ThinkAdmin
  12. // | github 代码仓库:https://github.com/zoujingli/ThinkAdmin
  13. // +----------------------------------------------------------------------
  14. namespace app\admin\controller;
  15. use app\admin\service\NodeService;
  16. use library\Controller;
  17. use think\Db;
  18. use think\facade\Request;
  19. /**
  20. * 用户登录管理
  21. * Class Login
  22. * @package app\admin\controller
  23. */
  24. class Login extends Controller
  25. {
  26. /**
  27. * 后台登录入口
  28. * @throws \think\Exception
  29. * @throws \think\db\exception\DataNotFoundException
  30. * @throws \think\db\exception\ModelNotFoundException
  31. * @throws \think\exception\DbException
  32. * @throws \think\exception\PDOException
  33. */
  34. public function index()
  35. {
  36. if (Request::isGet()) {
  37. if (NodeService::islogin()) {
  38. $this->redirect('@admin');
  39. } else {
  40. $this->title = '系统登录';
  41. $this->domain = Request::host(true);
  42. if (!($this->loginskey = session('loginskey'))) session('loginskey', $this->loginskey = uniqid());
  43. $this->devmode = in_array($this->domain, ['127.0.0.1', 'localhost']) || is_numeric(stripos($this->domain, 'thinkadmin.top'));
  44. $this->fetch();
  45. }
  46. } else {
  47. $data = $this->_input([
  48. 'username' => input('username'), 'password' => input('password'),
  49. ], [
  50. 'username' => 'require|min:4', 'password' => 'require|min:4',
  51. ], [
  52. 'username.require' => '登录账号不能为空!',
  53. 'password.require' => '登录密码不能为空!',
  54. 'username.min' => '登录账号长度不能少于4位有效字符!',
  55. 'password.min' => '登录密码长度不能少于4位有效字符!',
  56. ]);
  57. // 用户信息验证
  58. $map = ['is_deleted' => '0', 'username' => $data['username']];
  59. $user = Db::name('SystemUser')->where($map)->order('id desc')->find();
  60. if (empty($user)) $this->error('登录账号或密码错误,请重新输入!');
  61. if (empty($user['status'])) $this->error('账号已经被禁用,请联系管理员!');
  62. // 账号锁定消息
  63. $cache = cache("user_login_{$user['username']}");
  64. if (is_array($cache) && !empty($cache['number']) && !empty($cache['time'])) {
  65. if ($cache['number'] >= 10 && ($diff = $cache['time'] + 3600 - time()) > 0) {
  66. list($m, $s, $info) = [floor($diff / 60), floor($diff % 60), ''];
  67. if ($m > 0) $info = "{$m} 分";
  68. $this->error("<strong class='color-red'>抱歉,该账号已经被锁定!</strong><p class='nowrap'>连续 10 次登录错误,请 {$info} {$s} 秒后再登录!</p>");
  69. }
  70. }
  71. if (md5($user['password'] . session('loginskey')) !== $data['password']) {
  72. if (empty($cache) || empty($cache['time']) || empty($cache['number']) || $cache['time'] + 3600 < time()) {
  73. $cache = ['time' => time(), 'number' => 1, 'geoip' => $this->request->ip()];
  74. } elseif ($cache['number'] + 1 <= 10) {
  75. $cache = ['time' => time(), 'number' => $cache['number'] + 1, 'geoip' => $this->request->ip()];
  76. }
  77. cache("user_login_{$user['username']}", $cache);
  78. if (($diff = 10 - $cache['number']) > 0) {
  79. $this->error("<strong class='color-red'>登录账号或密码错误!</strong><p class='nowrap'>还有 {$diff} 次尝试机会,将锁定一小时内禁止登录!</p>");
  80. } else {
  81. sysoplog('系统管理', "账号{$user['username']}连续10次登录密码错误,请注意账号安全!");
  82. $this->error("<strong class='color-red'>登录账号或密码错误!</strong><p class='nowrap'>尝试次数达到上限,锁定一小时内禁止登录!</p>");
  83. }
  84. }
  85. // 登录成功并更新账号
  86. cache("user_login_{$user['username']}", null);
  87. Db::name('SystemUser')->where(['id' => $user['id']])->update([
  88. 'login_at' => Db::raw('now()'),
  89. 'login_ip' => $this->request->ip(),
  90. 'login_num' => Db::raw('login_num+1'),
  91. ]);
  92. session('loginskey', null);
  93. session('admin_user', $user);
  94. NodeService::applyUserAuth(true);
  95. sysoplog('系统管理', '用户登录系统成功');
  96. $this->success('登录成功', url('@admin'));
  97. }
  98. }
  99. /**
  100. * 退出登录
  101. */
  102. public function out()
  103. {
  104. \think\facade\Session::clear();
  105. \think\facade\Session::destroy();
  106. $this->success('退出登录成功!', url('@admin/login'));
  107. }
  108. }