|
@@ -6,13 +6,14 @@ namespace SixShop\Wechat\Cron;
|
|
|
use app\api\controller\Wechat;
|
|
use app\api\controller\Wechat;
|
|
|
use EasyWeChat\Kernel\Contracts\AccessToken as AccessTokenInterface;
|
|
use EasyWeChat\Kernel\Contracts\AccessToken as AccessTokenInterface;
|
|
|
use SixShop\Core\Attribute\Cron;
|
|
use SixShop\Core\Attribute\Cron;
|
|
|
|
|
+use SixShop\Core\Job\BaseJob;
|
|
|
use SixShop\System\Job\ClosureJob;
|
|
use SixShop\System\Job\ClosureJob;
|
|
|
use SixShop\Wechat\Service\MiniApp;
|
|
use SixShop\Wechat\Service\MiniApp;
|
|
|
use think\facade\Cache;
|
|
use think\facade\Cache;
|
|
|
use think\facade\Log;
|
|
use think\facade\Log;
|
|
|
use function Symfony\Component\String\s;
|
|
use function Symfony\Component\String\s;
|
|
|
|
|
|
|
|
-class AccessTokenCron
|
|
|
|
|
|
|
+class AccessTokenCron extends BaseJob
|
|
|
{
|
|
{
|
|
|
/**
|
|
/**
|
|
|
* 提前 60 秒执行
|
|
* 提前 60 秒执行
|
|
@@ -22,6 +23,7 @@ class AccessTokenCron
|
|
|
private AccessTokenInterface $accessToken;
|
|
private AccessTokenInterface $accessToken;
|
|
|
|
|
|
|
|
private string $cacheKey;
|
|
private string $cacheKey;
|
|
|
|
|
+
|
|
|
public function __construct(private MiniApp $miniApp)
|
|
public function __construct(private MiniApp $miniApp)
|
|
|
{
|
|
{
|
|
|
$this->accessToken = $this->miniApp->getAccessToken();
|
|
$this->accessToken = $this->miniApp->getAccessToken();
|
|
@@ -30,23 +32,23 @@ class AccessTokenCron
|
|
|
|
|
|
|
|
#[Cron('* * */2 * * *', 'wechat.refreshMiniAppAccessToken')]
|
|
#[Cron('* * */2 * * *', 'wechat.refreshMiniAppAccessToken')]
|
|
|
public function refreshMiniAppAccessToken(): void
|
|
public function refreshMiniAppAccessToken(): void
|
|
|
|
|
+ {
|
|
|
|
|
+ $expireIn = $this->execute();
|
|
|
|
|
+ self::dispatch()->delay($expireIn - self::LEAD_TIME);
|
|
|
|
|
+ Log::debug('定时刷新小程序 AccessToken 任务已启动,计划执行时间:{time}', [
|
|
|
|
|
+ 'time' => date('Y-m-d H:i:s', time() + $expireIn - self::LEAD_TIME)
|
|
|
|
|
+ ]);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public function execute(): int
|
|
|
{
|
|
{
|
|
|
$oldToken = $this->accessToken->getToken();
|
|
$oldToken = $this->accessToken->getToken();
|
|
|
$newToken = $this->accessToken->refresh();
|
|
$newToken = $this->accessToken->refresh();
|
|
|
$expireIn = Cache::ttl($this->cacheKey);
|
|
$expireIn = Cache::ttl($this->cacheKey);
|
|
|
- Log::debug('定时刷新小程序 AccessToken 成功,有效期:{expireIn} 秒, 结果:{result},计划执行时间:{time}', [
|
|
|
|
|
- 'expireIn' => $expireIn,
|
|
|
|
|
|
|
+ Log::debug('刷新小程序 AccessToken 成功,过期时间:{time} 秒, 结果:{result}', [
|
|
|
|
|
+ 'time' => date('Y-m-d H:i:s', time() + $expireIn),
|
|
|
'result' => $oldToken === $newToken ? '未更新' : '已更新',
|
|
'result' => $oldToken === $newToken ? '未更新' : '已更新',
|
|
|
- 'time' => date('Y-m-d H:i:s', time() + $expireIn - self::LEAD_TIME )
|
|
|
|
|
]);
|
|
]);
|
|
|
- ClosureJob::dispatch(function () {
|
|
|
|
|
- $accessToken = app(MiniApp::class)->getAccessToken();
|
|
|
|
|
- $oldToken = $accessToken->getToken();
|
|
|
|
|
- $newToken = $accessToken->refresh();
|
|
|
|
|
- Log::debug('计划刷新小程序 AccessToken 成功,结果:{result}', [
|
|
|
|
|
- 'expireIn' => $expireIn,
|
|
|
|
|
- 'result' => $oldToken === $newToken ? '未更新' : '已更新',
|
|
|
|
|
- ]);
|
|
|
|
|
- })->delay($expireIn - self::LEAD_TIME);
|
|
|
|
|
|
|
+ return $expireIn;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|