Login.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 library\Controller;
  15. use think\Db;
  16. /**
  17. * 用户登录管理
  18. * Class Login
  19. * @package app\admin\controller
  20. */
  21. class Login extends Controller
  22. {
  23. /**
  24. * 用户登录
  25. * @throws \think\Exception
  26. * @throws \think\db\exception\DataNotFoundException
  27. * @throws \think\db\exception\ModelNotFoundException
  28. * @throws \think\exception\DbException
  29. * @throws \think\exception\PDOException
  30. */
  31. public function index()
  32. {
  33. $this->title = '管理登录';
  34. $this->applyCsrfToken();
  35. if ($this->request->isGet()) {
  36. session('loginskey', $this->skey = session('loginskey') ? session('loginskey') : uniqid());
  37. return $this->fetch();
  38. }
  39. $data = $this->_input([
  40. 'username' => $this->request->post('username'),
  41. 'password' => $this->request->post('password'),
  42. ], [
  43. 'username' => 'require|min:4',
  44. 'password' => 'require|min:4',
  45. ], [
  46. 'username.require' => '登录账号不能为空!',
  47. 'password.require' => '登录密码不能为空!',
  48. 'username.min' => '登录账号长度不能少于4位有效字符!',
  49. 'password.min' => '登录密码长度不能少于4位有效字符!',
  50. ]);
  51. // 用户信息验证
  52. $map = ['is_deleted' => '0', 'username' => $data['username']];
  53. $user = Db::name('SystemUser')->where($map)->order('id desc')->find();
  54. if (empty($user)) $this->error('登录账号或密码错误,请重新输入!');
  55. if (empty($user['status'])) $this->error('账号已经被禁用,请联系管理员!');
  56. // 账号锁定消息
  57. $cache = cache("user_login_{$user['username']}");
  58. if (is_array($cache) && !empty($cache['number']) && !empty($cache['time'])) {
  59. if ($cache['number'] >= 10 && ($diff = $cache['time'] + 3600 - time()) > 0) {
  60. list($m, $s, $info) = [floor($diff / 60), floor($diff % 60), ''];
  61. if ($m > 0) $info = "{$m} 分";
  62. $this->error("<strong class='color-red'>抱歉,该账号已经被锁定!</strong><p class='nowrap'>连续 10 次登录错误,请 {$info} {$s} 秒后再登录!</p>");
  63. }
  64. }
  65. if (md5($user['password'] . session('loginskey')) !== $data['password']) {
  66. if (empty($cache) || empty($cache['time']) || empty($cache['number']) || $cache['time'] + 3600 < time()) {
  67. $cache = ['time' => time(), 'number' => 1, 'geoip' => $this->request->ip()];
  68. } elseif ($cache['number'] + 1 <= 10) {
  69. $cache = ['time' => time(), 'number' => $cache['number'] + 1, 'geoip' => $this->request->ip()];
  70. }
  71. cache("user_login_{$user['username']}", $cache);
  72. if (($diff = 10 - $cache['number']) > 0) {
  73. $this->error("<strong class='color-red'>登录账号或密码错误!</strong><p class='nowrap'>还有 {$diff} 次尝试机会,将锁定一小时内禁止登录!</p>");
  74. } else {
  75. _syslog('系统管理', "账号{$user['username']}连续10次登录密码错误,请注意账号安全!");
  76. $this->error("<strong class='color-red'>登录账号或密码错误!</strong><p class='nowrap'>尝试次数达到上限,锁定一小时内禁止登录!</p>");
  77. }
  78. }
  79. // 登录成功并更新账号
  80. cache("user_login_{$user['username']}", null);
  81. Db::name('SystemUser')->where(['id' => $user['id']])->update([
  82. 'login_at' => Db::raw('now()'), 'login_ip' => $this->request->ip(), 'login_num' => Db::raw('login_num+1'),
  83. ]);
  84. session('user', $user);
  85. session('loginskey', null);
  86. _syslog('系统管理', '用户登录系统成功');
  87. empty($user['authorize']) || \app\admin\service\Auth::applyNode();
  88. $this->success('登录成功,正在进入系统...', url('@admin'));
  89. }
  90. /**
  91. * 退出登录
  92. */
  93. public function out()
  94. {
  95. \think\facade\Session::clear();
  96. \think\facade\Session::destroy();
  97. $this->success('退出登录成功!', url('@admin/login'));
  98. }
  99. }