"application/json" ]; /** * AccessToken前缀 * @var string */ protected string $tokenPrefix = "dy_[appid]_access_token"; public function token(): static { $this->header = [ "access-token" => $this->getAccessToken() ]; return $this; } /** * 设置配置信息 * @param array $config * @return $this */ public function config(array $config = []): static { $this->config = $config; return $this; } public function getAccessToken() { // $resp = Http::asJson()->get("https://miniapi.jsshuita.com.cn/test/token")->array(); // return $resp['data']['token']; $accessToken = Cache::get($this->getTokenPrefix()); if (!empty($accessToken)) return $accessToken; $result = (new AccessToken)->config($this->config)->getAccessTokenLine(); if (empty($result)) return "获取Token失败\n"; Cache::set($this->getTokenPrefix(),$result['access_token'],$result['expires_in']); return $result['access_token']; } /** * 注册当前请求接口 * @param string $url 接口地址 * @return string */ protected function registerApi(string &$url): string { if (empty($this->access_token)) $this->access_token = $this->getAccessToken(); return $url = str_replace('ACCESS_TOKEN', urlencode($this->access_token), $url); } /** * @return string */ protected function getTokenPrefix(): string { return str_replace("[appid]",$this->config['appid'],$this->tokenPrefix); } }