Login.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkAdmin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2022 广州楚才信息科技有限公司 [ 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\model\SystemUser;
  19. use think\admin\service\AdminService;
  20. use think\admin\service\CaptchaService;
  21. use think\admin\service\SystemService;
  22. /**
  23. * 用户登录管理
  24. * Class Login
  25. * @package app\admin\controller
  26. */
  27. class Login extends Controller
  28. {
  29. /**
  30. * 后台登录入口
  31. * @throws \think\db\exception\DataNotFoundException
  32. * @throws \think\db\exception\DbException
  33. * @throws \think\db\exception\ModelNotFoundException
  34. */
  35. public function index()
  36. {
  37. if ($this->app->request->isGet()) {
  38. if (AdminService::instance()->isLogin()) {
  39. $this->redirect(sysuri('admin/index/index'));
  40. } else {
  41. // 登录验证码
  42. $this->captchaType = 'LoginCaptcha';
  43. $this->captchaToken = CodeExtend::uniqidDate(18);
  44. // 当前运行模式
  45. $system = SystemService::instance();
  46. $this->developMode = $system->checkRunMode();
  47. // 后台背景处理
  48. $images = str2arr(sysconf('login_image') ?: '', '|') ?: [
  49. $system->uri('/static/theme/img/login/bg1.jpg'),
  50. $system->uri('/static/theme/img/login/bg2.jpg'),
  51. ];
  52. $this->loginStyle = sprintf('style="background-image:url(%s)" data-bg-transition="%s"', $images[0], join(',', $images));
  53. // 更新后台域名
  54. $host = "{$this->request->scheme()}://{$this->request->host()}";
  55. if ($host !== sysconf('base.site_host')) sysconf('base.site_host', $host);
  56. // 标记验证令牌
  57. if (!$this->app->session->get('LoginInputSessionError')) {
  58. $this->app->session->set($this->captchaType, $this->captchaToken);
  59. }
  60. // 加载登录模板
  61. $this->title = '系统登录';
  62. $this->fetch();
  63. }
  64. } else {
  65. $data = $this->_vali([
  66. 'username.require' => '登录账号不能为空!',
  67. 'username.min:4' => '登录账号不能少于4位字符!',
  68. 'password.require' => '登录密码不能为空!',
  69. 'password.min:4' => '登录密码不能少于4位字符!',
  70. 'verify.require' => '图形验证码不能为空!',
  71. 'uniqid.require' => '图形验证标识不能为空!',
  72. ]);
  73. if (!CaptchaService::instance()->check($data['verify'], $data['uniqid'])) {
  74. $this->error('图形验证码验证失败,请重新输入!');
  75. }
  76. /*! 用户信息验证 */
  77. $map = ['username' => $data['username'], 'is_deleted' => 0];
  78. $user = SystemUser::mk()->where($map)->findOrEmpty();
  79. if ($user->isEmpty()) {
  80. $this->app->session->set("LoginInputSessionError", true);
  81. $this->error('登录账号或密码错误,请重新输入!');
  82. }
  83. if (empty($user['status'])) {
  84. $this->app->session->set("LoginInputSessionError", true);
  85. $this->error('账号已经被禁用,请联系管理员!');
  86. }
  87. if (md5("{$user['password']}{$data['uniqid']}") !== $data['password']) {
  88. $this->app->session->set("LoginInputSessionError", true);
  89. $this->error('登录账号或密码错误,请重新输入!');
  90. }
  91. $this->app->session->set('user', $user->toArray());
  92. $this->app->session->delete("LoginInputSessionError");
  93. $user->inc('login_num')->update([
  94. 'login_at' => date('Y-m-d H:i:s'),
  95. 'login_ip' => $this->app->request->ip(),
  96. ]);
  97. sysoplog('系统用户登录', '登录系统后台成功');
  98. $this->success('登录成功', sysuri('admin/index/index'));
  99. }
  100. }
  101. /**
  102. * 生成验证码
  103. */
  104. public function captcha()
  105. {
  106. $input = $this->_vali([
  107. 'type.require' => '验证码类型不能为空!',
  108. 'token.require' => '验证码标识不能为空!',
  109. ]);
  110. $image = CaptchaService::instance()->initialize();
  111. $captcha = ['image' => $image->getData(), 'uniqid' => $image->getUniqid()];
  112. if ($this->app->session->get($input['type']) === $input['token']) {
  113. $captcha['code'] = $image->getCode();
  114. $this->app->session->delete($input['type']);
  115. }
  116. $this->success('生成验证码成功', $captcha);
  117. }
  118. /**
  119. * 退出登录
  120. */
  121. public function out()
  122. {
  123. $this->app->session->clear();
  124. $this->app->session->destroy();
  125. $this->success('退出登录成功!', sysuri('admin/login/index'));
  126. }
  127. }