Login.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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::isLogin()) {
  39. $this->redirect(sysuri('admin/index/index'));
  40. } else {
  41. // 当前运行模式
  42. $this->developMode = SystemService::checkRunMode();
  43. // 后台背景处理
  44. $images = str2arr(sysconf('login_image') ?: '', '|') ?: [
  45. SystemService::uri('/static/theme/img/login/bg1.jpg'), SystemService::uri('/static/theme/img/login/bg2.jpg'),
  46. ];
  47. $this->loginStyle = sprintf('style="background-image:url(%s)" data-bg-transition="%s"', $images[0], join(',', $images));
  48. // 登录验证令牌
  49. $this->captchaType = 'LoginCaptcha';
  50. $this->captchaToken = CodeExtend::uniqidDate(18);
  51. if (!$this->app->session->get('LoginInputSessionError')) {
  52. $this->app->session->set($this->captchaType, $this->captchaToken);
  53. }
  54. // 更新后台域名
  55. if ($this->request->domain(true) !== sysconf('base.site_host')) {
  56. sysconf('base.site_host', $this->request->domain(true));
  57. }
  58. // 加载登录模板
  59. $this->title = '系统登录';
  60. $this->fetch();
  61. }
  62. } else {
  63. $data = $this->_vali([
  64. 'username.require' => '登录账号不能为空!',
  65. 'username.min:4' => '登录账号不能少于4位字符!',
  66. 'password.require' => '登录密码不能为空!',
  67. 'password.min:4' => '登录密码不能少于4位字符!',
  68. 'verify.require' => '图形验证码不能为空!',
  69. 'uniqid.require' => '图形验证标识不能为空!',
  70. ]);
  71. if (!CaptchaService::instance()->check($data['verify'], $data['uniqid'])) {
  72. $this->error('图形验证码验证失败,请重新输入!');
  73. }
  74. /*! 用户信息验证 */
  75. $map = ['username' => $data['username'], 'is_deleted' => 0];
  76. $user = SystemUser::mk()->where($map)->findOrEmpty();
  77. if ($user->isEmpty()) {
  78. $this->app->session->set('LoginInputSessionError', true);
  79. $this->error('登录账号或密码错误,请重新输入!');
  80. }
  81. if (empty($user['status'])) {
  82. $this->app->session->set('LoginInputSessionError', true);
  83. $this->error('账号已经被禁用,请联系管理员!');
  84. }
  85. if (md5("{$user['password']}{$data['uniqid']}") !== $data['password']) {
  86. $this->app->session->set('LoginInputSessionError', true);
  87. $this->error('登录账号或密码错误,请重新输入!');
  88. }
  89. $this->app->session->set('user', $user->toArray());
  90. $this->app->session->delete('LoginInputSessionError');
  91. $user->inc('login_num')->update([
  92. 'login_at' => date('Y-m-d H:i:s'),
  93. 'login_ip' => $this->app->request->ip(),
  94. ]);
  95. sysoplog('系统用户登录', '登录系统后台成功');
  96. $this->success('登录成功', sysuri('admin/index/index'));
  97. }
  98. }
  99. /**
  100. * 生成验证码
  101. */
  102. public function captcha()
  103. {
  104. $input = $this->_vali([
  105. 'type.require' => '验证码类型不能为空!',
  106. 'token.require' => '验证码标识不能为空!',
  107. ]);
  108. $image = CaptchaService::instance()->initialize();
  109. $captcha = ['image' => $image->getData(), 'uniqid' => $image->getUniqid()];
  110. if ($this->app->session->get($input['type']) === $input['token']) {
  111. $captcha['code'] = $image->getCode();
  112. $this->app->session->delete($input['type']);
  113. }
  114. $this->success('生成验证码成功', $captcha);
  115. }
  116. /**
  117. * 退出登录
  118. */
  119. public function out()
  120. {
  121. $this->app->session->clear();
  122. $this->app->session->destroy();
  123. $this->success('退出登录成功!', sysuri('admin/login/index'));
  124. }
  125. }