當前位置: 首頁>>代碼示例>>PHP>>正文


PHP UserIdentity::authWechat方法代碼示例

本文整理匯總了PHP中UserIdentity::authWechat方法的典型用法代碼示例。如果您正苦於以下問題:PHP UserIdentity::authWechat方法的具體用法?PHP UserIdentity::authWechat怎麽用?PHP UserIdentity::authWechat使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在UserIdentity的用法示例。


在下文中一共展示了UserIdentity::authWechat方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。

示例1: run

 public function run()
 {
     if (Yii::app()->user->isGuest) {
         if (Assist::isWeixin()) {
             $code = Yii::app()->request->getParam('code');
             $state = Yii::app()->request->getParam('state');
             if (empty($code)) {
                 throw new CException(Yii::t('yii', '授權碼缺失'));
             } else {
                 $wechat = Yii::app()->params['partner']['wechat'];
                 $params = array('appid' => $wechat['appid'], 'secret' => $wechat['appsecret'], 'code' => $code, 'grant_type' => 'authorization_code');
                 try {
                     $output = Yii::app()->curl->get($wechat['oauth2']['token'], $params);
                     $value = json_decode($output, true);
                     if (Yii::app()->curl->getStatus() == 200 && isset($value['access_token'])) {
                         $userinfo = json_decode(Yii::app()->curl->get($wechat['oauth2']['userinfo'], array('access_token' => $value['access_token'], 'openid' => $value['openid'], 'lang' => 'zh_CN')), true);
                         if (Yii::app()->curl->getStatus() == 200 && isset($userinfo['unionid'])) {
                             $_identity = new UserIdentity();
                             $_identity->authWechat($userinfo, $value['openid']);
                             if ($_identity->errorCode === UserIdentity::ERROR_NONE) {
                                 $duration = 86400;
                                 Yii::app()->user->login($_identity, $duration);
                                 $this->controller->redirect(Assist::getAccessURL());
                                 // $this->controller->redirect(Yii::app()->user->getReturnUrl(Assist::getDefaultURL()));
                             } elseif ($_identity->errorCode === UserIdentity::ERROR_NO_BIND) {
                                 Yii::app()->session['wechat'] = null;
                                 $this->controller->render('index');
                             }
                         } else {
                             throw new CException(Yii::t('yii', '密鑰錯誤或丟失。'));
                         }
                     } else {
                         Yii::log(print_r($value, true), CLogger::LEVEL_ERROR, 'user.wx.auth');
                         throw new CException(Yii::t('yii', '獲取有效憑證失敗'));
                     }
                 } catch (Exception $e) {
                     $this->controller->render('index');
                     // throw new CException(Yii::t('yii', $e->getMessage() ?: '內部服務錯誤'));
                 }
             }
         } else {
             throw new CHttpException(403, Yii::t('yii', '僅支持微信訪問'));
         }
     } else {
         $this->controller->redirect(Assist::getDefaultURL());
     }
 }
開發者ID:itliuchang,項目名稱:test,代碼行數:47,代碼來源:WechatConnectCallbackAction.php


注:本文中的UserIdentity::authWechat方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。