| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- <?php
- namespace app\extra\douyin;
- use support\think\Cache;
- use yzh52521\EasyHttp\Http;
- class Base
- {
- /**
- * API地址
- * @var string
- */
- protected string $gateway = "https://open.douyin.com/";
- /**
- * 配置信息
- * @var array
- */
- protected array $config = [];
- /**
- * @var string
- */
- private string $access_token;
- /**
- * 默认header
- * @var string[]
- */
- protected array $header = [
- "content-type" => "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);
- }
- }
|