Login.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. /**
  3. *
  4. * @author: xaboy<365615158@qq.com>
  5. * @day: 2018/01/15
  6. */
  7. namespace app\wap\controller;
  8. use app\wap\model\user\User;
  9. use app\wap\model\user\WechatUser;
  10. use basic\WapBasic;
  11. use service\UtilService;
  12. use service\WechatService;
  13. use think\Cookie;
  14. use think\Request;
  15. use think\Session;
  16. use think\Url;
  17. class Login extends WapBasic
  18. {
  19. public function index($ref = '')
  20. {
  21. Cookie::set('is_bg',1);
  22. $ref && $ref=htmlspecialchars(base64_decode($ref));
  23. if(UtilService::isWechatBrowser()){
  24. $this->_logout();
  25. $openid = $this->oauth();
  26. Cookie::delete('_oen');
  27. exit($this->redirect(empty($ref) ? Url::build('Index/index') : $ref));
  28. }
  29. $this->assign('ref',$ref);
  30. return $this->fetch();
  31. }
  32. public function check(Request $request)
  33. {
  34. list($account,$pwd,$ref) = UtilService::postMore(['account','pwd','ref'],$request,true);
  35. if(!$account || !$pwd) return $this->failed('请输入登录账号');
  36. if(!$pwd) return $this->failed('请输入登录密码');
  37. if(!User::be(['account'=>$account])) return $this->failed('登陆账号不存在!');
  38. $userInfo = User::where('account',$account)->find();
  39. $errorInfo = Session::get('login_error_info','wap')?:['num'=>0];
  40. $now = time();
  41. if($errorInfo['num'] > 5 && $errorInfo['time'] < ($now - 900))
  42. return $this->failed('错误次数过多,请稍候再试!');
  43. if($userInfo['pwd'] != md5($pwd)){
  44. Session::set('login_error_info',['num'=>$errorInfo['num']+1,'time'=>$now],'wap');
  45. return $this->failed('账号或密码输入错误!');
  46. }
  47. if(!$userInfo['status']) return $this->failed('账号已被锁定,无法登陆!');
  48. $this->_logout();
  49. Session::set('loginUid',$userInfo['uid'],'wap');
  50. $userInfo['last_time'] = time();
  51. $userInfo['last_ip'] = $request->ip();
  52. $userInfo->save();
  53. Session::delete('login_error_info','wap');
  54. Cookie::set('is_login',1);
  55. exit($this->redirect(empty($ref) ? Url::build('Index/index') : $ref));
  56. }
  57. public function logout()
  58. {
  59. $this->_logout();
  60. $this->successful('退出登陆成功',Url::build('Index/index'));
  61. }
  62. private function _logout()
  63. {
  64. Session::clear('wap');
  65. Cookie::delete('is_login');
  66. }
  67. }