|
@@ -0,0 +1,64 @@
|
|
|
+<?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;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ $accessTokenInfo = AccessTokenService::getAccessToken($miniprogram->appid, $miniprogram->appsecret);
|
|
|
+ } catch (\Exception $exception) {
|
|
|
+ myLog('wechat-miniprogram')->info('刷新小程序accessToken失败', [
|
|
|
+ 'appid' => $miniprogram->appid,
|
|
|
+ 'exceptionMessage' => $exception->getMessage(),
|
|
|
+ 'exceptionCode' => $exception->getCode()
|
|
|
+ ]);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ Redis::setex(AccessTokenService::getAccessTokenRedisKey($miniprogram->appid),
|
|
|
+ $accessTokenInfo['expires_in'], $accessTokenInfo['access_token']);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|