protected function _checkUser() { if (fnGet($this->input, 'access_token') == '') { $this->_ajaxReturn(array('error_code' => '600020', 'error_msg' => '参数[access_token]不能为空'), 400); } // 设置当前用户和客户端 $this->session->setUser($user = new User())->setClient($client = new Client()); $passportConfig = $this->config->get("api.passport"); // 尝试从缓存获取 userInfo if ($this->_userInfo = S($cacheKey = 'access_token_info.' . fnGet($this->input, 'access_token'))) { $user->find(fnGet($this->_userInfo, 'user_id')); $client->find(fnGet($this->_userInfo, 'client_id')); return; } // 向 passport 请求 userInfo $time = time(); $url = str_replace('internal-resource/user-info?', '', $passportConfig->passportUrl) . 'internal-resource/user-info'; $params = array('access_token' => fnGet($this->input, 'access_token'), 'app' => $passportConfig->passportApp, 'time' => $time); $sign = md5(implode('', $params) . $passportConfig->passportSecret); $params['sign'] = $sign; $http = new HttpClient(); $response = $http->request($url, $params); $data = json_decode($response, true); if (fnGet($data, 'id')) { //检测用户是否已经保存 $user->getByUsername($username = fnGet($data, 'username')); if (!($userId = $user->getId()) || !$user->getData('passport_id') || $user->getData('mobile') != fnGet($data, 'mobile')) { $user->addData(array('username' => $username, 'email' => fnGet($data, 'email'), 'mobile' => fnGet($data, 'mobile'), 'passport_id' => fnGet($data, 'passport_id'), 'avatar' => fnGet($data, 'avatar'), 'nickname' => fnGet($data, 'nickname'))); $user->save(); $userId = $user->getId(); } //检测客户端是否已经保存 $client->getByAppId($appId = fnGet($data, 'client_info/id')); if (!($clientId = $client->getId()) || $client->getScopes() != fnGet($data, 'client_info/scopes')) { $client->addData(array('client' => $appId, 'name' => fnGet($data, 'client_info/name'), 'app_secret' => fnGet($data, 'client_info/secret'), 'developerurl' => fnGet($data, 'client_info/endpoint'), 'scopes' => fnGet($data, 'client_info/scopes'))); $client->save(); $clientId = $client->getId(); } $this->_userInfo = array('user_id' => $userId, 'client_id' => $clientId, 'username' => $username, 'session_data' => fnGet($data, 'session_data')); S($cacheKey, $this->_userInfo, 3600); return; } $this->_ajaxReturn(array('error_code' => '600020', 'error_msg' => '用户无效'), 400); }