Example #1
0
 /**
  * 检测微信用户是否在这个广告主的账户下
  * @param $openID 用户的微信原始ID
  * @return void
  */
 static function checkUser($openID, $aUser = array())
 {
     if (empty($openID)) {
         $openID = $aUser['openid'];
         $_aUser = WeixinApi::getUserInfo($openID);
         $aUser['subscribe'] = $_aUser['subscribe'];
     } else {
         $aUser = WeixinApi::getUserInfo($openID);
     }
     //用 unionid取数据,和微信登录统一用户
     $unionID = $aUser['unionid'];
     $user = User::model()->findByAttributes(array('unionid' => $unionID));
     if ($user) {
         if ($aUser['nickname'] !== '' && $user->NickName == $user->UserName && !User::checkUserName($aUser['nickname'])) {
             $user->UserName = $aUser['nickname'];
         }
         $user->NickName = $aUser['nickname'];
         $user->Icon = $aUser['headimgurl'];
         $user->IsFollow = $aUser['subscribe'];
         //微信登录的用户,这个字段为空
         $user->Openid = $openID;
         if ($user->save()) {
             //更新redis
             CommonFunc::setRedis('user_' . $user->ID, 'userHeadUrl', $aUser['headimgurl']);
             CommonFunc::setRedis('user_' . $user->ID, 'userName', $user->UserName);
             return $user->ID;
         } else {
             Yii::log(__FILE__ . __LINE__ . 'insert fans error', 'error', 'system.api.weixin');
         }
     } else {
         $model_user = new User();
         if ($aUser['nickname'] === '') {
             $aUser['nickname'] = 'name_' . time();
         }
         $model_user->Account = $aUser['nickname'];
         $model_user->NickName = $aUser['nickname'];
         $model_user->UserName = $aUser['nickname'];
         if (User::checkUserName($aUser['nickname'])) {
             $model_user->UserName = $aUser['nickname'] . '_' . time();
         }
         $model_user->Openid = $openID;
         $model_user->Icon = $aUser['headimgurl'];
         $model_user->unionid = $aUser['unionid'];
         $model_user->CreateTime = date('Y-m-d H:i:s');
         $model_user->Status = 0;
         $model_user->LastLoginTime = date('Y-m-d H:i:s');
         $model_user->IsFollow = $aUser['subscribe'];
         if ($model_user->save()) {
             CommonFunc::setRedis('user_' . $model_user->ID, 'userHeadUrl', $aUser['headimgurl']);
             CommonFunc::setRedis('user_' . $model_user->ID, 'userName', $model_user->UserName);
             return $model_user->ID;
         } else {
             Yii::log(__FILE__ . __LINE__ . 'insert fans error', 'error', 'system.api.weixin');
         }
     }
     Yii::app()->end();
 }
Example #2
0
 static function getResonseUserInfo($code)
 {
     $url = str_replace('[CODE]', $code, WX_AT_URL_MOBILE);
     $url = str_replace('[APPID]', 'wx1ac3d9fb295a53b1', $url);
     $url = str_replace('[SECRET]', 'bc219ed3e76b9da02cf1f82f635f2704', $url);
     $request = Yii::app()->curl->run($url);
     $aResponse = WeixinApi::parseResponse($request, __FILE__, __LINE__);
     if (isset($_SESSION['from_btn_view']) && $_SESSION['from_btn_view']) {
         //不需要用户授权,直接登录
         return WeixinApi::getUserInfo($aResponse['openid']);
     }
     $url = str_replace('[TOKEN]', $aResponse['access_token'], WX_USER_INFO_MOBILE);
     $url = str_replace('[OPENID]', $aResponse['openid'], $url);
     $request = Yii::app()->curl->run($url);
     $aResponse = WeixinApi::parseResponse($request, __FILE__, __LINE__);
     $aUser = array('nickname' => $aResponse['nickname'], 'headimgurl' => $aResponse['headimgurl'], 'unionid' => $aResponse['unionid'], 'openid' => $aResponse['openid']);
     return $aUser;
 }