Ejemplo n.º 1
0
 function oauth_callback()
 {
     session();
     if ($_SESSION['oauth_serv'] == 'sina') {
         $conf = $this->config['oauth']['weibo'];
         $oauth = new WeiboOAuth($conf['appid'], $conf['skey'], $_SESSION['oauth_keys']['oauth_token'], $_SESSION['oauth_keys']['oauth_token_secret']);
         $_SESSION['last_key'] = $oauth->getAccessToken($_REQUEST['oauth_verifier']);
         $client = new WeiboClient($conf['appid'], $conf['skey'], $_SESSION['last_key']['oauth_token'], $_SESSION['last_key']['oauth_token_secret']);
         $userinfo = $client->verify_credentials();
         if (!isset($userinfo['id'])) {
             var_dump($conf, $_SESSION);
             return "请求错误:" . var_export($userinfo, true);
         }
         $model = createModel('UserInfo');
         $username = '******' . $userinfo['id'];
         $u = $model->get($username, 'username')->get();
         //不存在,则插入数据库
         if (empty($u)) {
             $u['username'] = $username;
             $u['nickname'] = $userinfo['name'];
             $u['avatar'] = $userinfo['profile_image_url'];
             list($u['province'], $u['city']) = explode(' ', $userinfo['location']);
             //插入到表中
             $u['id'] = $model->put($u);
         }
         //写入SESSION
         $_SESSION['isLogin'] = 1;
         $_SESSION['user_id'] = $u['id'];
         $_SESSION['user'] = $u;
         $this->setLoginStat();
         $this->swoole->http->edirect(WEBROOT . "/person/index/");
     } elseif ($_SESSION['oauth_serv'] == 'qq') {
         $conf = $this->swoole->config['oauth']['qq'];
         $oauth = new QQOAuth($conf['APP_ID'], $conf['APP_KEY']);
         $oauth->getAccessToken($_GET['oauth_token'], $_SESSION['oauth_keys']['oauth_token_secret'], $_GET['oauth_vericode']);
         $username = $oauth->access_token['openid'];
         $model = createModel('UserInfo');
         $u = $model->get($username, 'username')->get();
         //不存在,则插入数据库
         if (empty($u)) {
             $user = $oauth->api_get('user/get_user_info');
             if (empty($user)) {
                 return Swoole\JS::js_back("请求错误");
             }
             $u['username'] = $username;
             $u['nickname'] = $user['nickname'];
             $u['avatar'] = $user['figureurl_2'];
             //插入到表中
             $u['id'] = $model->put($u);
         }
         //写入SESSION
         $_SESSION['isLogin'] = 1;
         $_SESSION['user_id'] = $u['id'];
         $_SESSION['user'] = $u;
         $this->setLoginStat();
         $this->swoole->http->redirect(WEBROOT . "/person/index/");
     }
 }