Login.php 5.2 KB

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