代码拉取完成,页面将自动刷新
<?php
namespace ESign\Core;
use ESign\Exceptions\HttpException;
use Doctrine\Common\Cache\Cache;
use Doctrine\Common\Cache\FilesystemCache;
class AccessToken
{
protected $appId;
protected $secret;
protected $cache;
protected $cacheKey;
protected $http;
protected $tokenJsonKey = 'token';
protected $prefix = 'esign.common.access_token.';
const API_TOKEN_GET = '/v1/oauth2/access_token';
public function __construct($appId, $secret, Cache $cache = null)
{
$this->appId = $appId;
$this->secret = $secret;
$this->cache = $cache;
}
/**
* @param bool $forceRefresh
* @return bool|mixed
* @throws HttpException
*/
public function getToken($forceRefresh = false)
{
$cacheKey = $this->getCacheKey();
$cached = $this->getCache()->fetch($cacheKey);
// var_dump($cached);
// $cached = null;
// var_dump($cached);
if ($forceRefresh || empty($cached)) {
$token = $this->getTokenFromServer();
$this->getCache()->save($cacheKey, $token['data'][$this->tokenJsonKey], 60 * 100);
return $token['data'][$this->tokenJsonKey];
}
return $cached;
}
/**
* @return mixed
* @throws HttpException
*/
public function getTokenFromServer()
{
$params = [
'appId' => $this->appId,
'secret' => $this->secret,
'grantType' => 'client_credentials',
];
$http = $this->getHttp();
$token = $http->parseJSON($http->get(self::API_TOKEN_GET, $params));
if (empty($token['data'][$this->tokenJsonKey])) {
throw new HttpException('Request AccessToken fail. response: ' . json_encode($token, JSON_UNESCAPED_UNICODE));
}
return $token;
}
public function getAppId()
{
return $this->appId;
}
public function getSecret()
{
return $this->secret;
}
protected function getCache()
{
return $this->cache ?: $this->cache = new FilesystemCache(sys_get_temp_dir());
}
public function getHttp()
{
return $this->http ?: $this->http = new Http();
}
public function setHttp($http)
{
$this->http = $http;
return $this;
}
public function setPrefix($prefix)
{
$this->prefix = $prefix;
return $this;
}
public function setCacheKey($cacheKey)
{
$this->cacheKey = $cacheKey;
return $this;
}
protected function getCacheKey()
{
if (is_null($this->cacheKey)) {
return $this->prefix . $this->appId;
}
return $this->cacheKey;
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。