| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- <?php
- namespace app\extra\dyLife;
- use support\think\Cache;
- class BasicLife
- {
- /**
- * 网关
- * @var string
- */
- protected string $gateway = "https://open.douyin.com/";
- /**
- * 配置信息
- * @var array
- */
- protected array $config = [];
- /**
- * 默认header
- * @var string[]
- */
- protected array $header = [
- "content-type" => "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);
- }
- }
|