/** * 获取授权公众号的令牌 * * @return string */ public function getToken() { $cacheKey = sprintf($this->cacheKey, $this->authorizer_appid); $this->token = Cache::get($cacheKey, function () use($cacheKey) { $params = array('component_appid' => Config::get('wechat.componentAppId'), 'authorizer_appid' => $this->authorizer_appid, 'authorizer_refresh_token' => $this->authorizer_refresh_token); $http = new ComponentHttp(new ComponentAccessToken()); $response = $http->jsonPost(self::API_AUTHORIZER_TOKEN, $params); // 设置token $token = $response['authorizer_access_token']; // 把token缓存起来 $expiresAt = Carbon::now()->addSeconds($response['expires_in']); Cache::put($cacheKey, $token, $expiresAt); return $token; }); return $this->token; }
/** * 获取Token * * @return string */ public function getToken() { $this->token = Cache::get($this->cacheKey); // 从缓存中获取不到token或设置了强制刷新 if (!$this->token || $this->force) { $params = array('component_appid' => Config::get('wechat.componentAppId'), 'component_appsecret' => Config::get('wechat.componentAppSecret'), 'component_verify_ticket' => ComponentVerifyTicket::getTicket()); $http = new ComponentHttp(); $response = $http->jsonPost(self::API_COMPONENT_TOKEN, $params); // 设置token $this->token = $response['component_access_token']; // 把token缓存起来 $expiresAt = Carbon::now()->addSeconds($response['expires_in']); Cache::put($this->cacheKey, $this->token, $expiresAt); } return $this->token; }
/** * 设置授权方的选项信息 * * @param $authorizer_appid * @param $option_name * @param $option_value * @return mixed */ public function setAuthorizerOption($authorizer_appid, $option_name, $option_value) { $params = array('component_appid' => $this->appid, 'authorizer_appid' => $authorizer_appid, 'option_name' => $option_name, 'option_value' => $option_value); return $this->http->jsonPost(self::API_SET_AUTHORIZER_OPTION, $params); }