Login.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\model\system\SystemAdmin;
  4. use service\CacheService;
  5. use service\UtilService;
  6. use think\Request;
  7. use think\Response;
  8. use think\Session;
  9. use think\Url;
  10. /**
  11. * 登录验证控制器
  12. * Class Login
  13. * @package app\admin\controller
  14. */
  15. class Login extends SystemBasic
  16. {
  17. public function index()
  18. {
  19. return $this->fetch();
  20. }
  21. /**
  22. * 登录验证 + 验证码验证
  23. */
  24. public function verify(Request $request)
  25. {
  26. if(!$request->isPost()) return $this->failed('请登陆!');
  27. list($account,$pwd,$verify) = UtilService::postMore([
  28. 'account','pwd','verify'
  29. ],$request,true);
  30. //检验验证码
  31. if(!captcha_check($verify)) return $this->failed('验证码错误,请重新输入');
  32. $error = Session::get('login_error')?:['num'=>0,'time'=>time()];
  33. $error['num'] = 0;
  34. if($error['num'] >=5 && $error['time'] > strtotime('- 5 minutes'))
  35. return $this->failed('错误次数过多,请稍候再试!');
  36. //检验帐号密码
  37. $res = SystemAdmin::login($account,$pwd);
  38. if($res){
  39. Session::set('login_error',null);
  40. return $this->redirect(Url::build('Index/index'));
  41. }else{
  42. $error['num'] += 1;
  43. $error['time'] = time();
  44. Session::set('login_error',$error);
  45. return $this->failed(SystemAdmin::getErrorInfo('用户名错误,请重新输入'));
  46. }
  47. }
  48. public function captcha()
  49. {
  50. ob_clean();
  51. $captcha = new \think\captcha\Captcha([
  52. 'codeSet'=>'0123456789',
  53. 'length'=>4,
  54. 'fontSize'=>30
  55. ]);
  56. return $captcha->entry();
  57. }
  58. /**
  59. * 退出登陆
  60. */
  61. public function logout()
  62. {
  63. SystemAdmin::clearLoginInfo();
  64. $this->redirect('Login/index');
  65. }
  66. }