Login.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. $this->title = '系统登录';
  42. $this->captchaType = 'LoginCaptcha';
  43. $this->captchaToken = CodeExtend::uniqidDate(18);
  44. $this->developMode = SystemService::instance()->checkRunMode();
  45. $this->backgrounds = strtr(sysconf('login_image') ?: '', '|', ',');
  46. // 刷新当前后台域名
  47. $host = "{$this->request->scheme()}://{$this->request->host()}";
  48. if ($host !== sysconf('base.site_host')) sysconf('base.site_host', $host);
  49. // 标记登录验证令牌
  50. if (!$this->app->session->get('LoginInputSessionError')) {
  51. $this->app->session->set($this->captchaType, $this->captchaToken);
  52. }
  53. $this->fetch();
  54. }
  55. } else {
  56. $data = $this->_vali([
  57. 'username.require' => '登录账号不能为空!',
  58. 'username.min:4' => '登录账号不能少于4位字符!',
  59. 'password.require' => '登录密码不能为空!',
  60. 'password.min:4' => '登录密码不能少于4位字符!',
  61. 'verify.require' => '图形验证码不能为空!',
  62. 'uniqid.require' => '图形验证标识不能为空!',
  63. ]);
  64. if (!CaptchaService::instance()->check($data['verify'], $data['uniqid'])) {
  65. $this->error('图形验证码验证失败,请重新输入!');
  66. }
  67. /*! 用户信息验证 */
  68. $map = ['username' => $data['username'], 'is_deleted' => 0];
  69. $user = SystemUser::mk()->where($map)->find();
  70. if (empty($user)) {
  71. $this->app->session->set("LoginInputSessionError", true);
  72. $this->error('登录账号或密码错误,请重新输入!');
  73. }
  74. if (empty($user['status'])) {
  75. $this->app->session->set("LoginInputSessionError", true);
  76. $this->error('账号已经被禁用,请联系管理员!');
  77. }
  78. if (md5("{$user['password']}{$data['uniqid']}") !== $data['password']) {
  79. $this->app->session->set("LoginInputSessionError", true);
  80. $this->error('登录账号或密码错误,请重新输入!');
  81. }
  82. $this->app->session->set('user', $user->toArray());
  83. $this->app->session->delete("LoginInputSessionError");
  84. $user->save([
  85. 'login_ip' => $this->app->request->ip(),
  86. 'login_at' => $this->app->db->raw('now()'),
  87. 'login_num' => $this->app->db->raw('login_num+1'),
  88. ]);
  89. sysoplog('系统用户登录', '登录系统后台成功');
  90. $this->success('登录成功', sysuri('admin/index/index'));
  91. }
  92. }
  93. /**
  94. * 生成验证码
  95. */
  96. public function captcha()
  97. {
  98. $input = $this->_vali([
  99. 'type.require' => '验证码类型不能为空!',
  100. 'token.require' => '验证码标识不能为空!',
  101. ]);
  102. $image = CaptchaService::instance()->initialize();
  103. $captcha = ['image' => $image->getData(), 'uniqid' => $image->getUniqid()];
  104. if ($this->app->session->get($input['type']) === $input['token']) {
  105. $captcha['code'] = $image->getCode();
  106. $this->app->session->delete($input['type']);
  107. }
  108. $this->success('生成验证码成功', $captcha);
  109. }
  110. /**
  111. * 退出登录
  112. */
  113. public function out()
  114. {
  115. $this->app->session->clear();
  116. $this->app->session->destroy();
  117. $this->success('退出登录成功!', sysuri('admin/login/index'));
  118. }
  119. }