Login.php 5.1 KB

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