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()); } }
public function filterWechat($filterChain) { if (Assist::isWeixin()) { // if(!Yii::app()->session['wechat']) { // if(!strpos(Yii::app()->request->getPathInfo(), 'wechatconnect')){ // $this->redirect('/wechat/wechatconnect'); // return false; // } // } $filterChain->run(); return true; } else { // echo 'ERROR'; $this->layout = '//layouts/error'; $this->render('/site/error', array('code' => 403, 'message' => yii::t('yii', 'Only login in wechat'))); } }
public function run() { if (Yii::app()->user->isGuest) { if (Assist::isWeixin()) { //login valid for ajax request $returnurl = Yii::app()->request->getParam('returnurl'); //->encodeURIComponent if (!empty($returnurl)) { Yii::app()->user->setReturnUrl($returnurl); //urldecode($returnurl) } $wechat = Yii::app()->params['partner']['wechat']; $params = array('appid' => $wechat['appid'], 'redirect_uri' => 'http://hubapp.livenaked.com' . $wechat['oauth2']['callback'], 'response_type' => 'code', 'scope' => $wechat['oauth2']['scope'][1], 'state' => 1); $url = $wechat['oauth2']['authorize'] . http_build_query($params) . '#wechat_redirect'; $this->controller->redirect($url); } else { // throw new CException(Yii::t('yii', '请在微信客户端打开链接')); throw new CHttpException(403, Yii::t('yii', '请在微信客户端打开链接')); } } else { $this->controller->redirect(Assist::getDefaultURL()); } }