当前位置: 首页>>代码示例>>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;未经允许,请勿转载。