Skip to content

Commit

Permalink
fix: 小程序 access_token 缓存 key
Browse files Browse the repository at this point in the history
  • Loading branch information
overtrue committed Aug 30, 2023
1 parent 30ee1d9 commit 5169192
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/MiniApp/AccessToken.php
Expand Up @@ -6,5 +6,5 @@

class AccessToken extends \EasyWeChat\OfficialAccount\AccessToken
{
//
const CACHE_KEY_PREFIX = 'mini_app';
}
36 changes: 28 additions & 8 deletions src/OfficialAccount/AccessToken.php
Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand Down
16 changes: 16 additions & 0 deletions tests/MiniApp/AccessTokenTest.php
@@ -0,0 +1,16 @@
<?php

namespace EasyWeChat\Tests\MiniApp;

use EasyWeChat\MiniApp\AccessToken;
use EasyWeChat\Tests\TestCase;

class AccessTokenTest extends TestCase
{
public function test_it_will_use_mini_app_cache_prefix()
{
$accessToken = new AccessToken('mock-app-id', 'mock-secret');

$this->assertStringStartsWith('mini_app.access_token', $accessToken->getKey());
}
}

1 comment on commit 5169192

@vercel
Copy link

@vercel vercel bot commented on 5169192 Aug 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

easywechat – ./

easywechat-overtrue.vercel.app
easywechat.vercel.app
easywechat-git-6x-overtrue.vercel.app

Please sign in to comment.