/** * 获取触发微信请求的微信用户信息 * @return Fans */ public function getFans() { if ($this->_fans === false) { $this->_fans = Fans::findByOpenId($this->message['FromUserName']); } return $this->_fans; }
/** * 获取粉丝数据(当前公众号唯一) * @param bool $oauth 是否通过微信服务器Oauth API跳转获取 通过API获取的用户信息会存入session中 * @return mixed */ public function getFans($oauth = true) { if ($this->_fans === null) { $wechat = $this->getWechat(); $sessionKey = self::SESSION_MOBILE_FANS_PREFIX . '_' . $wechat->id; $openId = Yii::$app->session->get($sessionKey); if (!$openId && $oauth) { // API获取用户 $data = $wechat->getSdk()->getAuthorizeUserInfo('fansInfo'); if (isset($data['openid'])) { $openId = $data['openid']; } } if (!$openId || ($fans = Fans::findByOpenId($openId)) === null) { return false; } $this->setFans($fans); } return $this->_fans; }