GetAccessTokenCallback = $options['GetAccessTokenCallback']; } $this->config = new DataArray($options); } public function getAccessToken() { if (!empty($this->access_token)) { return $this->access_token; } $cache = $this->config->get('appid') . '_access_token'; $this->access_token = Tools::getCache($cache); if (!empty($this->access_token)) { return $this->access_token; } // 处理开放平台授权公众号获取AccessToken if (!empty($this->GetAccessTokenCallback) && is_callable($this->GetAccessTokenCallback)) { $this->access_token = call_user_func_array($this->GetAccessTokenCallback, [$this->config->get('appid'), $this]); if (!empty($this->access_token)) { Tools::setCache($cache, $this->access_token, 7000); } return $this->access_token; } list($appid, $secret) = [$this->config->get('appid'), $this->config->get('appsecret')]; $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$secret}"; $result = Http::get($url)->array(); if (!empty($result['access_token'])) { Tools::setCache($cache, $result['access_token'], 7000); } return $this->access_token = $result['access_token']; } public function setAccessToken($accessToken) { if (!is_string($accessToken)) { throw new InvalidArgumentException("Invalid AccessToken type, need string."); } $cache = $this->config->get('appid') . '_access_token'; Tools::setCache($cache, $this->access_token = $accessToken); } /** * 清理删除 AccessToken * @return bool */ public function delAccessToken() { $this->access_token = ''; return Tools::delCache($this->config->get('appid') . '_access_token'); } protected function registerApi(&$url, $method, $arguments = []) { $this->currentMethod = ['method' => $method, 'arguments' => $arguments]; if (empty($this->access_token)) $this->access_token = $this->getAccessToken(); return $url = str_replace('ACCESS_TOKEN', urlencode($this->access_token), $url); } }