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 新增刷新ticket #2733

Merged
merged 1 commit into from
Aug 21, 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 src/Kernel/Contracts/RefreshableJsApiTicket.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace EasyWeChat\Kernel\Contracts;

interface RefreshableJsApiTicket extends JsApiTicket
{
public function refreshTicket(): string;
}
5 changes: 3 additions & 2 deletions src/OfficialAccount/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use function call_user_func;
use EasyWeChat\Kernel\Contracts\AccessToken as AccessTokenInterface;
use EasyWeChat\Kernel\Contracts\JsApiTicket as JsApiTicketInterface;
use EasyWeChat\Kernel\Contracts\RefreshableJsApiTicket as RefreshableJsApiTicketInterface;
use EasyWeChat\Kernel\Contracts\RefreshableAccessToken as RefreshableAccessTokenInterface;
use EasyWeChat\Kernel\Contracts\Server as ServerInterface;
use EasyWeChat\Kernel\Encryptor;
Expand Down Expand Up @@ -186,7 +187,7 @@ public function getOAuth(): SocialiteProviderInterface
return $provider;
}

public function getTicket(): JsApiTicketInterface
public function getTicket(): JsApiTicketInterface|RefreshableJsApiTicketInterface
{
if (! $this->ticket) {
$this->ticket = new JsApiTicket(
Expand All @@ -201,7 +202,7 @@ public function getTicket(): JsApiTicketInterface
return $this->ticket;
}

public function setTicket(JsApiTicketInterface $ticket): static
public function setTicket(JsApiTicketInterface|RefreshableJsApiTicketInterface $ticket): static
{
$this->ticket = $ticket;

Expand Down
11 changes: 8 additions & 3 deletions src/OfficialAccount/JsApiTicket.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

namespace EasyWeChat\OfficialAccount;

use EasyWeChat\Kernel\Contracts\JsApiTicket as JsApiTicketInterface;
use EasyWeChat\Kernel\Contracts\RefreshableJsApiTicket as RefreshableJsApiTicketInterface;
use EasyWeChat\Kernel\Exceptions\HttpException;
use JetBrains\PhpStorm\ArrayShape;
use function sprintf;

class JsApiTicket extends AccessToken implements JsApiTicketInterface
class JsApiTicket extends AccessToken implements RefreshableJsApiTicketInterface
{
/**
* @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface
Expand All @@ -29,14 +29,19 @@ public function getTicket(): string
return $ticket;
}

return $this->refreshTicket();
}

public function refreshTicket(): string
{
$response = $this->httpClient->request('GET', '/cgi-bin/ticket/getticket', ['query' => ['type' => 'jsapi']])
->toArray(false);

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

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

return $response['ticket'];
}
Expand Down
Loading