1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- <?php
- namespace App\Console\Commands\Miniprogram;
- use App\Service\Miniprogram\Wechat\AccessTokenService;
- use Illuminate\Console\Command;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Redis;
- class WechatAccessToken extends Command
- {
- /**
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = 'Miniprogram:WechatAccessToken {--appid= : 小程序appid} {--secret= : 小程序secret}';
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = '微信小程序维护';
- /**
- * Execute the console command.
- */
- public function handle()
- {
- $appid = $this->option('appid');
- $secret = $this->option('secret');
- if($appid && $secret) {
- $miniprograms = [(object)[
- 'appid' => $appid, 'appsecret' => $secret
- ]];
- } else {
- $miniprograms = DB::table('miniprogram')
- ->where(['type' => 1, 'status' => 1])
- ->select('appid', 'appsecret')
- ->get();
- }
- foreach ($miniprograms as $miniprogram) {
- $ttl = Redis::ttl(AccessTokenService::getAccessTokenRedisKey($miniprogram->appid));
- if($ttl > 600) {
- continue;
- }
- $accessTokenInfo = AccessTokenService::getAccessToken($miniprogram->appid, $miniprogram->appsecret);
- if(false === $accessTokenInfo) {
- myLog('wechat-miniprogram')->info('刷新小程序accessToken失败', [
- 'appid' => $miniprogram->appid,
- ]);
- continue;
- }
- Redis::setex(AccessTokenService::getAccessTokenRedisKey($miniprogram->appid),
- $accessTokenInfo['expires_in'], $accessTokenInfo['access_token']);
- }
- }
- }
|