diff --git a/src/MiniApp/AccessToken.php b/src/MiniApp/AccessToken.php index cf30dff4e..ff44d1c33 100644 --- a/src/MiniApp/AccessToken.php +++ b/src/MiniApp/AccessToken.php @@ -6,5 +6,5 @@ class AccessToken extends \EasyWeChat\OfficialAccount\AccessToken { - // + const CACHE_KEY_PREFIX = 'mini_app'; } diff --git a/src/OfficialAccount/AccessToken.php b/src/OfficialAccount/AccessToken.php index 84b45669b..da32d4214 100644 --- a/src/OfficialAccount/AccessToken.php +++ b/src/OfficialAccount/AccessToken.php @@ -30,6 +30,8 @@ class AccessToken implements RefreshableAccessTokenInterface protected CacheInterface $cache; + const CACHE_KEY_PREFIX = 'official_account'; + public function __construct( protected string $appId, protected string $secret, @@ -44,7 +46,7 @@ public function __construct( public function getKey(): string { - return $this->key ?? $this->key = sprintf('official_account.access_token.%s.%s', $this->appId, $this->secret); + return $this->key ?? $this->key = sprintf('%s.access_token.%s.%s', static::CACHE_KEY_PREFIX, $this->appId, $this->secret); } public function setKey(string $key): static @@ -92,19 +94,28 @@ public function toQuery(): array } /** - * @throws RedirectionExceptionInterface - * @throws DecodingExceptionInterface - * @throws InvalidArgumentException - * @throws ClientExceptionInterface - * @throws HttpException - * @throws TransportExceptionInterface - * @throws ServerExceptionInterface + * @throws \EasyWeChat\Kernel\Exceptions\HttpException + * @throws \Psr\SimpleCache\InvalidArgumentException + * @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface + * @throws \Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface + * @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface + * @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface + * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface */ public function refresh(): string { return $this->stable ? $this->getStableAccessToken() : $this->getAccessToken(); } + /** + * @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface + * @throws \Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface + * @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface + * @throws \Psr\SimpleCache\InvalidArgumentException + * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface + * @throws \EasyWeChat\Kernel\Exceptions\HttpException + * @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface + */ public function getStableAccessToken(bool $force_refresh = false): string { $response = $this->httpClient->request( @@ -129,6 +140,15 @@ public function getStableAccessToken(bool $force_refresh = false): string return $response['access_token']; } + /** + * @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface + * @throws \Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface + * @throws \Psr\SimpleCache\InvalidArgumentException + * @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface + * @throws \EasyWeChat\Kernel\Exceptions\HttpException + * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface + * @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface + */ public function getAccessToken(): string { $response = $this->httpClient->request( diff --git a/tests/MiniApp/AccessTokenTest.php b/tests/MiniApp/AccessTokenTest.php new file mode 100644 index 000000000..7a232eb1d --- /dev/null +++ b/tests/MiniApp/AccessTokenTest.php @@ -0,0 +1,16 @@ +assertStringStartsWith('mini_app.access_token', $accessToken->getKey()); + } +}