Login.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkAdmin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2021 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: https://thinkadmin.top
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // | 免费声明 ( https://thinkadmin.top/disclaimer )
  11. // +----------------------------------------------------------------------
  12. // | gitee 代码仓库:https://gitee.com/zoujingli/ThinkAdmin
  13. // | github 代码仓库:https://github.com/zoujingli/ThinkAdmin
  14. // +----------------------------------------------------------------------
  15. namespace app\admin\controller;
  16. use think\admin\Controller;
  17. use think\admin\extend\CodeExtend;
  18. use think\admin\service\AdminService;
  19. use think\admin\service\CaptchaService;
  20. use think\admin\service\SystemService;
  21. /**
  22. * 用户登录管理
  23. * Class Login
  24. * @package app\admin\controller
  25. */
  26. class Login extends Controller
  27. {
  28. /**
  29. * 后台登录入口
  30. * @throws \think\db\exception\DataNotFoundException
  31. * @throws \think\db\exception\DbException
  32. * @throws \think\db\exception\ModelNotFoundException
  33. */
  34. public function index()
  35. {
  36. if ($this->app->request->isGet()) {
  37. if (AdminService::instance()->isLogin()) {
  38. $this->redirect(sysuri('admin/index/index'));
  39. } else {
  40. $this->title = '系统登录';
  41. $this->captchaType = 'LoginCaptcha';
  42. $this->captchaToken = CodeExtend::uniqidDate(18);
  43. $this->devmode = SystemService::instance()->checkRunMode('dev');
  44. // 刷新当前后台域名
  45. $host = "{$this->request->scheme()}://{$this->request->host()}";
  46. if ($host !== sysconf('base.site_host')) sysconf('base.site_host', $host);
  47. // 标记登录验证令牌
  48. if (!$this->app->session->get('login_input_session_error')) {
  49. $this->app->session->set($this->captchaType, $this->captchaToken);
  50. }
  51. $this->fetch();
  52. }
  53. } else {
  54. $data = $this->_vali([
  55. 'username.require' => '登录账号不能为空!',
  56. 'username.min:4' => '登录账号不能少于4位字符!',
  57. 'password.require' => '登录密码不能为空!',
  58. 'password.min:4' => '登录密码不能少于4位字符!',
  59. 'verify.require' => '图形验证码不能为空!',
  60. 'uniqid.require' => '图形验证标识不能为空!',
  61. ]);
  62. if (!CaptchaService::instance()->check($data['verify'], $data['uniqid'])) {
  63. $this->error('图形验证码验证失败,请重新输入!');
  64. }
  65. /*! 用户信息验证 */
  66. $map = ['username' => $data['username'], 'is_deleted' => '0'];
  67. $user = $this->app->db->name('SystemUser')->where($map)->order('id desc')->find();
  68. if (empty($user)) {
  69. $this->app->session->set("login_input_session_error", true);
  70. $this->error('登录账号或密码错误,请重新输入!');
  71. }
  72. if (md5("{$user['password']}{$data['uniqid']}") !== $data['password']) {
  73. $this->app->session->set("login_input_session_error", true);
  74. $this->error('登录账号或密码错误,请重新输入!');
  75. }
  76. if (empty($user['status'])) {
  77. $this->error('账号已经被禁用,请联系管理员!');
  78. }
  79. $this->app->session->set('user', $user);
  80. $this->app->session->delete("login_input_session_error");
  81. $this->app->db->name('SystemUser')->where(['id' => $user['id']])->update([
  82. 'login_ip' => $this->app->request->ip(),
  83. 'login_at' => $this->app->db->raw('now()'),
  84. 'login_num' => $this->app->db->raw('login_num+1'),
  85. ]);
  86. sysoplog('系统用户登录', '登录系统后台成功');
  87. $this->success('登录成功', sysuri('admin/index/index'));
  88. }
  89. }
  90. /**
  91. * 生成验证码
  92. */
  93. public function captcha()
  94. {
  95. $input = $this->_vali([
  96. 'type.require' => '验证码类型不能为空!',
  97. 'token.require' => '验证码标识不能为空!',
  98. ]);
  99. $image = CaptchaService::instance()->initialize();
  100. $captcha = ['image' => $image->getData(), 'uniqid' => $image->getUniqid()];
  101. if ($this->app->session->get($input['type']) === $input['token']) {
  102. $captcha['code'] = $image->getCode();
  103. $this->app->session->delete($input['type']);
  104. }
  105. $this->success('生成验证码成功', $captcha);
  106. }
  107. /**
  108. * 退出登录
  109. */
  110. public function out()
  111. {
  112. $this->app->session->clear();
  113. $this->app->session->destroy();
  114. $this->success('退出登录成功!', sysuri('admin/login/index'));
  115. }
  116. }