accountDao = $accountDao; $this->openService = $openService; } /** * @return void * @throws Exceptions\HttpException * @throws Exceptions\InvalidArgumentException * @throws Exceptions\InvalidConfigException * @throws Exceptions\RuntimeException * @throws \GuzzleHttp\Exception\GuzzleException * @throws \Psr\SimpleCache\InvalidArgumentException */ public function handle() { // 获取所有小程序 $apps = $this->accountDao->getMiniApps(); if (empty($apps)) { return; } foreach ($apps as $app) { // 数据库记录的token到期时间 $tokenExpiresAt = getProp($app, 'access_token_expires_at'); // token到期前10分钟开始更新 if (strtotime($tokenExpiresAt) - time() > 600) { continue; } // 实例化并获取token $tokenData = $this->openService->getInstance([ 'app_id' => getProp($app, 'app_id'), 'secret' => getProp($app, 'app_secret'), 'sandbox' => (bool)getProp($app, 'is_sandbox'), ])->getAccessToken(true); if ($tokenData) { // 更新数据库 $seconds = (int)getProp($tokenData, 'expires_in'); $this->accountDao->updateMiniApp(['id' => getProp($app, 'id')], [ 'access_token' => getProp($tokenData, 'access_token'), 'access_token_expires_at' => date('Y-m-d H:i:s', strtotime("+ $seconds second")), ]); } } } }