Login.php 2.3 KB

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