Login.php 4.3 KB

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