本文整理汇总了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());
}
}