/** * 处理callback */ protected function callback() { //不在开启的登陆方式内直接返回 if (!$this->allowConnect($this->auth['type'])) { $this->response->redirect(Typecho_Common::url('/login', $this->options->index)); } if (empty($this->auth['code'])) { $this->response->redirect($this->options->index); } $callback_url = Typecho_Common::url('/user/oauth?type=' . $this->auth['type'], $this->options->index); $this->auth['openid'] = ''; require_once 'Connect.php'; //换取access_token $this->auth['token'] = Connect::getToken($this->auth['type'], $callback_url, $this->auth['code']); if (empty($this->auth['token'])) { $this->response->redirect($this->options->index); } //获取openid $this->auth['openid'] = Connect::getOpenId($this->auth['type']); if (empty($this->auth['openid'])) { $this->response->redirect($this->options->index); } //使用openid登录 $this->autoLogin($this->auth['openid'], $this->auth['type']); //获取用户昵称 $this->auth['nickname'] = Connect::getNickName($this->auth['type'], $this->auth['openid']); if (empty($this->auth['nickname'])) { $this->auth['nickname'] = '关注者' . substr(str_shuffle($this->auth['openid']), 0, 4); } Typecho_Cookie::set('__user_auth', serialize($this->auth)); $this->response->redirect($this->___bindUrl()); }
public function callback() { if (!isset($_SESSION)) { session_start(); if (isset($_SESSION['__typecho_auth'])) { $this->auth = $_SESSION['__typecho_auth']; } } if ($this->request->isPost()) { $do = $this->request->get('do'); if (!in_array($do, array('bind', 'reg'))) { $this->widget('Widget_Notice')->set(array('错误数据!'), 'error'); $this->response->goBack(); } if (!isset($this->auth['openid']) || !isset($this->auth['type'])) { $this->response->redirect($this->options->index); } $func = 'doCallback' . ucfirst($do); $this->{$func}(); unset($_SESSION['__typecho_auth']); $this->response->redirect($this->options->index); } $options = TeConnect_Plugin::options(); if (empty($this->auth)) { $this->auth['type'] = $this->request->get('type', ''); $this->auth['code'] = $this->request->get('code', ''); //不在开启的登陆方式内直接返回 if (!isset($options[$this->auth['type']])) { $this->response->redirect($this->options->index); } if (empty($this->auth['code'])) { $this->response->redirect($this->options->index); } $callback_url = Typecho_Common::url('/oauth_callback?type=' . $this->auth['type'], $this->options->index); $this->auth['openid'] = ''; require_once 'Connect.php'; //换取access_token $this->auth['token'] = Connect::getToken($this->auth['type'], $callback_url, $this->auth['code']); if (empty($this->auth['token'])) { $this->response->redirect($this->options->index); } //获取openid $this->auth['openid'] = Connect::getOpenId($this->auth['type']); if (empty($this->auth['openid'])) { $this->response->redirect($this->options->index); } $this->auth['nickname'] = Connect::getNickName($this->auth['type'], $this->auth['openid']); } //已经登录,重新绑定 if ($this->user->hasLogin()) { /** 绑定用户 */ $this->bindUser($this->user->uid, $this->auth['openid'], $this->auth['type']); //提示绑定成功,并跳转 // add 跳转提示 $this->widget('Widget_Notice')->set(array('成功绑定账号!')); $this->response->redirect($this->options->index); } //已经绑定,直接登录 $isConnect = $this->findConnectUser($this->auth['openid'], $this->auth['type']); if ($isConnect) { $this->useUidLogin($isConnect['uid']); // add 跳转提示 $this->widget('Widget_Notice')->set(array('已成功登陆!')); $this->response->redirect($this->options->index); } $custom = $this->options->plugin('TeConnect')->custom; if (!$custom && !empty($this->auth['nickname'])) { $dataStruct = array('screenName' => $this->auth['nickname'], 'created' => $this->options->gmtTime, 'group' => 'subscriber'); $uid = $this->regConnectUser($dataStruct); if ($uid) { $this->widget('Widget_Notice')->set(array('已成功注册并登陆!')); } else { $this->widget('Widget_Notice')->set(array('注册用户失败!'), 'error'); } $this->response->redirect($this->options->index); } else { if (!isset($_SESSION['__typecho_auth'])) { $_SESSION['__typecho_auth'] = $this->auth; } //未绑定,显示界面 $this->render('callback.php'); } }