"application/json" ]; /** * AccessToken前缀 * @var string */ protected string $prefix = "dy_[appid]_access_token"; /** * 设置配置信息 * @param array $config * @return $this */ public function config(array $config = []): static { $this->config = $config; return $this; } /** * 释放授权 * @return $this */ public function token(): static { $this->header = [ "access-token" => $this->getAccessToken() ]; return $this; } public function getAccessToken() { try { $accessToken = Cache::get($this->getPrefix()); if (!empty($accessToken)) return $accessToken; $result = (new Token)->config($this->config)->getAccessToken(); if (empty($result)) return "获取Token失败"; Cache::set($this->getPrefix(),$result['access_token'],$result['expires_in']); return $result['access_token']; } catch (\Throwable $throwable) { return ""; } } /** * @return string */ protected function getPrefix(): string { return str_replace("[appid]",$this->config['appid'],$this->prefix); } }