本文整理匯總了PHP中UserService::instance方法的典型用法代碼示例。如果您正苦於以下問題:PHP UserService::instance方法的具體用法?PHP UserService::instance怎麽用?PHP UserService::instance使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類UserService
的用法示例。
在下文中一共展示了UserService::instance方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。
示例1: getUserInfoWithCache
public function getUserInfoWithCache($userId)
{
$data = Yii::app()->cache->get('user-' . $userId);
if (empty($data)) {
$data = UserService::instance()->getUser($userId);
Yii::app()->cache->set('user-' . $userId, $data, 3600 * 24);
}
return $data;
}
示例2: actionWechat
/**
* 授權回調
* @param string $code
* @param string $data
* @throws CException
* @throws Exception
*/
public function actionWechat($code = '', $data = '')
{
Yii::import('ext.wechat.*');
//判斷不同授權來源
//code: 正常微信授權的參數
//data: 群友代理授權json
if (!empty($code)) {
$wehcat = Yii::app()->params['wechat'];
$wechat = new Wechat($wehcat['appid'], $wehcat['appsecret']);
$accessToken = $wechat->getAccessToken($code);
if (!empty($accessToken) && !empty($accessToken['openid'])) {
$openid = $accessToken['openid'];
$wechat = new Wechat($wehcat['appid'], $wehcat['appsecret'], $accessToken['access_token']);
$data = $wechat->getUserInfo($openid);
}
} else {
if (!empty($data)) {
$data = json_decode(base64_decode($data), 1);
if (!empty($data['openid'])) {
$openid = $data['openid'];
}
}
}
//都不存在時,創建一個默認帳號
if (empty($openid)) {
$openid = 'temp-' . md5(microtime());
$data = array();
}
$user = UserService::instance()->save($openid, $data);
//登錄
$identity = new UserIdentity($user);
if ($identity->authenticate()) {
Yii::app()->user->login($identity, 24 * 3600 * 30);
$returnUrl = Yii::app()->user->returnUrl;
$this->redirect(!empty($returnUrl) ? $returnUrl : Yii::app()->homeUrl);
} else {
exit('很抱歉,微信授權失敗!');
}
}