protected function messageEvent($postObj) { $event = $postObj->Event; switch ($event) { case 'subscribe': $user = User::model()->findByAttributes(array('udid' => $postObj->FromUserName)); if (!$user) { $user = new User(); $user->udid = $postObj->FromUserName; $user->create_at = $this->getTime(); } else { $user->status = 1; $user->times++; } $user->save(); WX::updateInfo($postObj->FromUserName); $this->returnText('love,love', $postObj); break; case 'unsubscribe': $user = User::model()->findByAttributes(array('udid' => $postObj->FromUserName)); if ($user) { $user->status = 0; $user->update_at = $this->getTime(); $user->save(); } $this->returnText('dont,love,love', $postObj); break; case 'LOCATION': $user = User::model()->findByAttributes(array('udid' => $postObj->FromUserName)); if ($user) { $user->x = floatval($postObj->Latitude); $user->y = floatval($postObj->Longitude); $user->locate = floatval($postObj->Precision); $user->update_at = $this->getTime(); $user->save(); } $this->returnText('get x,y', $postObj); break; case 'CLICK': $keyvalue = $postObj->EventKey; if ($keyvalue == 'TEM_COMMUNITY') { //创建临时的网点 CommunityController::createOrUpdate($postObj->FromUserName); } $this->returnText('创建临时网点成功', $postObj); break; } }
public static function getUserWebOpenid($code) { $appid = APP_ID; $appsecret = APP_SECRET; $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={$appid}&secret={$appsecret}&code={$code}&grant_type=authorization_code"; Yii::trace($url, 'web accesstoken from code'); $response = Yii::app()->curl->get($url); $response = json_decode($response, true); if (isset($response['errcode'])) { Yii::trace(CVarDumper::dumpAsString($response), 'Error Code from Web'); } else { Yii::trace(CVarDumper::dumpAsString($response), 'get user web openid'); $openid = $response['openid']; $user = User::model()->findByAttributes(array('udid' => $openid)); $newuser = false; if (!$user) { $user = new User(); $user->udid = $openid; $user->create_at = WX::getTime(); //如果是新用户,那么取下用户基本信息吧 $newuser = true; } $user->accesstoken = $response['access_token']; $user->refresh_token = $response['refresh_token']; $user->expires_in = time() + $response['expires_in'] - 200; $user->token_refreshed = intval(0); if (!$user->save()) { Yii::log(CVarDumper::dumpAsString($user->errors), 'error', 'User Web AccessToken Save Error'); } if ($newuser) { WX::updateInfo($openid); } return $openid; } }