/** * [getUserInfo 获取用户信息] * @return [type] [description] */ public function getUserInfo() { // get authorization code from query parameters $code = $this->getCode(); // check whether it is a CSRF attack request if ($code && $code != $this->store->get('code')) { p(1); die; $oauth2 = $this->getBaiduOAuth2Service(); $session = $oauth2->getAccessTokenByAuthorizationCode($code); if ($session) { $this->store->set('code', $code); $this->setSession($session); $apiClient = new BaiduApiClient($this->clientId, $session['access_token']); $user = $apiClient->api('passport/users/getInfo'); if ($user) { $session = array_merge($session, $user); $this->setSession($session); } return $session; } $this->store->removeAll(); return false; } // as a fallback, just return whatever is in the storage $session = $this->store->get('session'); $this->setSession($session); if ($session) { $apiClient = new BaiduApiClient($this->clientId, $session['access_token']); $user = $apiClient->api('passport/users/getInfo'); $user['sex'] = $user['sex'] == 1 ? '男' : '女'; $user['nikename'] = $user['username']; $user['head'] = 'http://tb.himg.baidu.com/sys/portraitn/item/' . $user['portrait']; // if ($user) { // $session = array_merge($session,$user); // $this->setSession($session); // } } return $user; }
/** * Get session info from Baidu server or from the store in app server side. * * @return array|false */ protected function doGetSession() { // get authorization code from query parameters $code = $this->getCode(); // check whether it is a CSRF attack request if ($code && $code != $this->store->get('code')) { $oauth2 = $this->getBaiduOAuth2Service(); $session = $oauth2->getAccessTokenByAuthorizationCode($code); if ($session) { $this->store->set('code', $code); $this->setSession($session); $apiClient = new BaiduApiClient($this->clientId, $session['access_token']); $user = $apiClient->api('passport/users/getLoggedInUser'); if ($user) { $session = array_merge($session, $user); $this->setSession($session); } return $session; } // code was bogus, so everything based on it should be invalidated. $this->store->removeAll(); return false; } // as a fallback, just return whatever is in the storage $session = $this->store->get('session'); $this->setSession($session); if ($session && !isset($session['uid'])) { $apiClient = new BaiduApiClient($this->clientId, $session['access_token']); $user = $apiClient->api('passport/users/getLoggedInUser'); if ($user) { $session = array_merge($session, $user); $this->setSession($session); } } return $session; }