controller(); // AJAX请求 不验证授权 if($this->request->isAjax()){ $this->userInfo = session('userInfo'); $debug = config('weixin.debug'); if(empty($this->userInfo) && $debug){ // 调试模式 $curOpenId = session('openid'); $openid = $curOpenId? $curOpenId : config('weixin.openid'); session('openid', $openid); $field = 'id,openid,user_login,mobile,real_name,user_nickname,avatar'; $this->userInfo = Member::getInfo(['openid'=> $openid,'user_type'=> 2],$field); session('userInfo', $this->userInfo); } $this->userId = isset($this->userInfo['id'])? intval($this->userInfo['id']) : 0; $this->userType = isset($this->userInfo['user_type'])? intval($this->userInfo['user_type']) : 2; if(empty($this->userId) && strtolower($controller) != 'register'){ showJson('login','请先登录',['url'=> url('/weixin/login/index')]); } return true; } // 调用微信授权校验 $debug = config('weixin.debug'); if(empty($debug)){ $this->userInfo = session('userInfo'); $this->userId = isset($this->userInfo['id'])? intval($this->userInfo['id']) : 0; $openid = session('openid'); if(empty($openid)){ Wechat::auth(); } if(empty($this->userInfo)){ $url = strtolower($controller)=='register'? '/weixin/register/index' : '/weixin/login/index'; $this->redirect($url); } }else{ $this->userInfo = session('userInfo'); if(empty($this->userInfo)){ $curOpenId = session('openid'); $openid = $curOpenId? $curOpenId : config('weixin.openid'); session('openid', $openid); $field = 'id,openid,user_login,mobile,real_name,user_nickname,avatar'; $this->userInfo = Member::getInfo(['openid'=> $openid,'user_type'=> 2],$field); session('userInfo', $this->userInfo); } $this->userId = isset($this->userInfo['id'])? $this->userInfo['id'] : 0; } $this->userType = isset($this->userInfo['user_type'])? intval($this->userInfo['user_type']) : 2; } /** * 验证是否绑定手机号和实名 * @return bool */ private function checkBind(){ $userInfo = session('userInfo'); $this->userId = isset($userInfo['id'])? intval($userInfo['id']) : 0; $mobile = isset($userInfo['mobile'])? trim($userInfo['mobile']) : ''; $realName = isset($userInfo['real_name'])? trim($userInfo['real_name']) : ''; if(empty($this->userId)){ Wechat::init(); $this->checkBind(); } if($mobile && $realName){ return true; } $field = 'id,openid,user_login,mobile,real_name,user_nickname,avatar'; $userInfo = db('user')->field($field)->where(['id'=> $this->userId,'user_type'=> 2])->find(); $mobile = isset($userInfo['mobile'])? trim($userInfo['mobile']) : ''; $realName = isset($userInfo['real_name'])? trim($userInfo['real_name']) : ''; // 若未绑定手机号码 if(empty($mobile)){ session('mealUrl', request()->domain().request()->url()); saveLogCache('Meal:'.$this->userId,request()->domain().request()->url()); $this->redirect(url('/weixin/bind/bindMobile')); exit; } // 未实名 if(empty($realName)){ $this->redirect(url('/weixin/bind/bindName')); exit; } } /** * 验证状态 */ public function checkUser(){ $userStatus = isset($this->userInfo['user_status'])? intval($this->userInfo['user_status']) : 0; if($this->userInfo && $userStatus != 1){ showJson(1006, 2102); } } } ?>