Beispiel #1
0
 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;
 }
Beispiel #2
0
 /**
  * 授权回调
  * @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('很抱歉,微信授权失败!');
     }
 }