Ver Fonte

回传队列配置

liuzejian há 1 ano atrás
pai
commit
6911f58e75

+ 64 - 0
app/Console/Commands/Miniprogram/WechatAccessToken.php

@@ -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']);
+        }
+    }
+}

+ 4 - 1
app/Console/Kernel.php

@@ -12,7 +12,10 @@ class Kernel extends ConsoleKernel
      */
     protected function schedule(Schedule $schedule): void
     {
-        // $schedule->command('inspire')->hourly();
+        /**
+         * 刷新微信小程序的accessToken
+         */
+        $schedule->command('Miniprogram:WechatAccessToken')->everyMinute();
     }
 
     /**

+ 28 - 0
app/Service/Miniprogram/Wechat/AccessTokenService.php

@@ -0,0 +1,28 @@
+<?php
+
+namespace App\Service\Miniprogram\Wechat;
+
+use GuzzleHttp\Client;
+
+class AccessTokenService
+{
+    public static function getAccessToken($appid, $secret) {
+        $client = new Client(['timeout' => 3]);
+        $httpResult = $client->get('https://api.weixin.qq.com/cgi-bin/token', [
+            'query' => ['grant_type' => 'client_credential', 'appid' => $appid, 'secret' => $secret]
+        ]);
+
+        if(200 != $httpResult->getStatusCode()) {
+            throw new \RuntimeException('请求微信上游失败', '500001');
+        }
+        $parsedContent = \json_decode($httpResult->getBody()->getContents(), true);
+        if(0 != ($parsedContent['errcode'] ?? 0)) {
+            throw new \RuntimeException('请求微信上游失败:'. ($parsedContent['errmsg'] ?? ''), '500002');
+        }
+        return $parsedContent;
+    }
+
+    public static function getAccessTokenRedisKey($appid) {
+        return 'dj.miniWechat.at.' . $appid;
+    }
+}

+ 1 - 1
config/database.php

@@ -148,7 +148,7 @@ return [
             'prefix' => env('REDIS_PREFIX', Str::slug(env('APP_NAME', 'laravel'), '_').'_database_'),
         ],
 
-        'default1' => [
+        'default' => [
             'url' => env('REDIS_URL'),
             'host' => env('REDIS_HOST', '127.0.0.1'),
             'username' => env('REDIS_USERNAME'),

+ 1 - 1
config/queue.php

@@ -64,7 +64,7 @@ return [
 
         'redis' => [
             'driver' => 'redis',
-            'connection' => 'default1',
+            'connection' => 'default',
             'queue' => env('REDIS_QUEUE', 'default'),
             'retry_after' => 90,
             'block_for' => null,