Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat 公众号/小程序 新增稳定版接口调用凭据(StableAccessToken) #2711

Merged
merged 2 commits into from
Jun 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions docs/src/6.x/mini-app/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@
'token' => 'your-token', // Token
'aes_key' => '', // EncodingAESKey,兼容与安全模式下请一定要填写!!!

/**
* 是否使用 Stable Access Token
* 默认 false
* https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/mp-access-token/getStableAccessToken.html
* true 使用 false 不使用
*/
'use_stable_access_token' => false,

/**
* 接口请求相关配置,超时时间等,具体可用参数请参考:
* https://github.com/symfony/symfony/blob/5.3/src/Symfony/Contracts/HttpClient/HttpClientInterface.php
Expand Down
8 changes: 8 additions & 0 deletions docs/src/6.x/official-account/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@
'token' => 'your-token', // Token
'aes_key' => '', // EncodingAESKey,兼容与安全模式下请一定要填写!!!

/**
* 是否使用 Stable Access Token
* 默认 false
* https://developers.weixin.qq.com/doc/offiaccount/Basic_Information/getStableAccessToken.html
* true 使用 false 不使用
*/
'use_stable_access_token' => false,

/**
* OAuth 配置
*
Expand Down
32 changes: 31 additions & 1 deletion src/OfficialAccount/AccessToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public function __construct(
protected ?string $key = null,
?CacheInterface $cache = null,
?HttpClientInterface $httpClient = null,
protected ?bool $stable = false
) {
$this->httpClient = $httpClient ?? HttpClient::create(['base_uri' => 'https://api.weixin.qq.com/']);
$this->cache = $cache ?? new Psr16Cache(new FilesystemAdapter(namespace: 'easywechat', defaultLifetime: 1500));
Expand Down Expand Up @@ -99,6 +100,35 @@ public function toQuery(): array
* @throws ServerExceptionInterface
*/
public function refresh(): string
{
return $this->stable ? $this->getStableAccessToken() : $this->getAccessToken();
}

public function getStableAccessToken(bool $force_refresh = false): string
{
$response = $this->httpClient->request(
'POST',
'https://api.weixin.qq.com/cgi-bin/stable_token',
[
'json' => [
'grant_type' => 'client_credential',
'appid' => $this->appId,
'secret' => $this->secret,
'force_refresh' => $force_refresh
],
]
)->toArray(false);

if (empty($response['access_token'])) {
throw new HttpException('Failed to get stable access_token: ' . json_encode($response, JSON_UNESCAPED_UNICODE));
}

$this->cache->set($this->getKey(), $response['access_token'], intval($response['expires_in']));

return $response['access_token'];
}

public function getAccessToken(): string
{
$response = $this->httpClient->request(
'GET',
Expand All @@ -113,7 +143,7 @@ public function refresh(): string
)->toArray(false);

if (empty($response['access_token'])) {
throw new HttpException('Failed to get access_token: '.json_encode($response, JSON_UNESCAPED_UNICODE));
throw new HttpException('Failed to get access_token: ' . json_encode($response, JSON_UNESCAPED_UNICODE));
}

$this->cache->set($this->getKey(), $response['access_token'], intval($response['expires_in']));
Expand Down
4 changes: 3 additions & 1 deletion src/OfficialAccount/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class Application implements ApplicationInterface

protected AccessTokenInterface|RefreshableAccessTokenInterface|null $accessToken = null;

protected ?JsApiTicket $ticket = null;
protected ?JsApiTicketInterface $ticket = null;

protected ?\Closure $oauthFactory = null;

Expand Down Expand Up @@ -138,6 +138,7 @@ public function getAccessToken(): AccessTokenInterface|RefreshableAccessTokenInt
secret: $this->getAccount()->getSecret(),
cache: $this->getCache(),
httpClient: $this->getHttpClient(),
stable: $this->config->get('use_stable_access_token', false),
);
}

Expand Down Expand Up @@ -193,6 +194,7 @@ public function getTicket(): JsApiTicketInterface
secret: $this->getAccount()->getSecret(),
cache: $this->getCache(),
httpClient: $this->getClient(),
stable: $this->config->get('use_stable_access_token', false),
);
}

Expand Down