Exemplo n.º 1
0
 /**
  * 第三方登录回调处理方法
  *
  * @param OAuth $client OAuth客户端对象
  */
 public function tplSuccessCallback($client)
 {
     $mTplogin = new UserThirdPartyLogin();
     $id = $client->getId();
     $key = $client->openid();
     $result = '';
     $session = Yii::$app->getSession();
     $userAttrs = $client->getUserAttributes();
     $userProfile = $client->getUserInfo();
     $openid = $userAttrs[$key];
     $data = ['type' => $id, 'open_id' => $openid, 'profile_info' => json_encode($userProfile)];
     if (isset($userAttr['expire_in'])) {
         $data['expire_time'] = $userAttr['expire_in'];
     }
     if ($session->has(UserBaseInfo::SESSION_KEY_USER)) {
         $userOperateLog = new UserOperateLog();
         $res = $this->thirdBind($client);
         $logOperate = '第三方绑定标识';
         $operateType = '第三方' . $id . '绑定';
         if ($res) {
             $userOperateLog->store($operateType, $logOperate, $openid);
             echo $res;
             die;
         }
         $userOperateLog->store($operateType, $logOperate, $openid, true);
         return $this->redirect(Url::toRoute('user/third-bind'));
     }
     try {
         $result = $mTplogin->checkBindByOpenid($openid, UserThirdPartyLogin::BIND_WEB_CHANNEL);
         if (!$result) {
             $this->setTplSessionToUser(['type' => $id, 'open_id' => $openid, 'profile' => $userProfile]);
             /*
             if ($userAr = $mTplogin->bind($data)) {
                 $userAr['profile'] = $userProfile;
                 $this->setTplSessionToUser($userAr);
             } else {
                 // TODO: 绑定失败
             }
             */
         } else {
             if ($result === TRUE) {
                 $this->setTplSessionToUser(['type' => $id, 'open_id' => $openid, 'profile' => $userProfile]);
             } else {
                 if (is_array($result)) {
                     $this->setTplSessionToUser(['type' => $id, 'open_id' => $openid, 'profile' => $userProfile]);
                     return;
                 }
                 //登录状态
                 $mUser = new UserBaseInfo();
                 if ($mUser->autoLoginById($result, true)) {
                     $this->redirect(Url::toRoute('ucenter/index'));
                 } else {
                     // XXX: key后面可以去除“硬编码”
                     if (isset($session['tpl_user'])) {
                         unset($session['tpl_user']);
                     }
                     $this->redirect(Url::toRoute('user/login'));
                 }
             }
         }
     } catch (Exception $e) {
         // TODO: 登录异常
     }
 }