فهرست منبع

获取剧目信息

liuzejian 1 سال پیش
والد
کامیت
3d56197900

+ 3 - 5
app/Console/Commands/Miniprogram/WechatAccessToken.php

@@ -46,13 +46,11 @@ class WechatAccessToken extends Command
             if($ttl > 600) {
                 continue;
             }
-            try {
-                $accessTokenInfo = AccessTokenService::getAccessToken($miniprogram->appid, $miniprogram->appsecret);
-            } catch (\Exception $exception) {
+
+            $accessTokenInfo = AccessTokenService::getAccessToken($miniprogram->appid, $miniprogram->appsecret);
+            if(false === $accessTokenInfo) {
                 myLog('wechat-miniprogram')->info('刷新小程序accessToken失败', [
                     'appid' => $miniprogram->appid,
-                    'exceptionMessage' => $exception->getMessage(),
-                    'exceptionCode' => $exception->getCode()
                 ]);
                 continue;
             }

+ 78 - 0
app/Console/Commands/Video/WechatCheck/SyncDramaInfo.php

@@ -0,0 +1,78 @@
+<?php
+
+namespace App\Console\Commands\Video\WechatCheck;
+
+use App\Service\Miniprogram\Wechat\AccessTokenService;
+use App\Service\Util\Support\Http\WechatURL;
+use App\Services\Util\Support\Http\HttpRequestService;
+use Illuminate\Console\Command;
+use Predis\Command\Traits\DB;
+
+class SyncDramaInfo extends Command
+{
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'WechatCheck:SyncDramaInfo {--drama_ids= : 剧目ids}';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = '微信剧目提审-获取剧目信息';
+
+    /**
+     * Execute the console command.
+     */
+    public function handle()
+    {
+        $drama_ids = $this->option('drama_ids');
+        if($drama_ids) {
+            $dramaIds = explode(',', $drama_ids);
+        }
+        $accessToken = $this->getAccessToken();
+        DB::table('video_wechat_check')
+            ->when($dramaIds, function ($query, $dramaIds) {
+                return $query->whereIn('drama_id', $dramaIds);
+            })
+            ->where('drama_id', '<>', 0)
+            ->where([
+                'status' => 1,
+                'is_enabled' => 1
+            ])
+            ->select('drama_id', 'id', 'status')
+            ->orderBy('id')
+            ->chunk(100, function ($items) use ($accessToken){
+                foreach ($items as $item) {
+                    $result = HttpRequestService::simplePost(WechatURL::vod_getdrama. $accessToken, [
+                        'drama_id' => $item->drama_id
+                    ]);
+                    if(false === $result || (0 != ($result['errcode'] ?? 0))) {
+                        myLog('SyncDramaInfo')->error('获取剧目信息失败', [
+                            'id' => $item->id, 'drama_id' => $item->drama_id,
+                            'errcode' => $result['errocode'] ?? '',
+                            'errmsg' => $result['errmsg'] ?? ''
+                        ]);
+                        continue;
+                    }
+                    $status = $result['drama_info']['audit_detail']['status'];
+                    if($status != $item->status)
+                    {
+                        DB::table('video_wechat_check')
+                            ->where('id', $item->id)
+                            ->update([
+                                'check_at' => date('Y-m-d H:i:s', $result['drama_info']['audit_detail']['audit_time']),
+                                'status' => $status,
+                            ]);
+                    }
+                }
+            });
+    }
+
+    private function getAccessToken() {
+        return Redis::get(AccessTokenService::getAccessTokenRedisKey(config('wechat.duanju.masterAppid')));
+    }
+}

+ 4 - 13
app/Service/Miniprogram/Wechat/AccessTokenService.php

@@ -2,24 +2,15 @@
 
 namespace App\Service\Miniprogram\Wechat;
 
+use App\Service\Util\Support\Http\WechatURL;
+use App\Services\Util\Support\Http\HttpRequestService;
 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;
+        return HttpRequestService::simpleGet(WechatURL::access_token, ['grant_type' => 'client_credential',
+            'appid' => $appid, 'secret' => $secret]);
     }
 
     public static function getAccessTokenRedisKey($appid) {

+ 65 - 0
app/Service/Util/Support/Http/HttpRequestService.php

@@ -0,0 +1,65 @@
+<?php
+
+
+namespace App\Services\Util\Support\Http;
+
+
+
+use GuzzleHttp\Client;
+
+class HttpRequestService
+{
+
+    /**
+     * 发送post请求
+     * @param $url 请求地址
+     * @param $accessToken 请求access_token
+     * @param $postMessage 请求体
+     * @return array
+     */
+    public static function simplePost($url, $postMessage) {
+        $client = new Client(['timeout' => 10]);
+        try {
+            $res = $client->post(
+                $url,
+                [
+                    'json' => $postMessage
+                ]);
+            $httpStatusCode = $res->getStatusCode();
+            $httpContent = $res->getBody()->getContents();
+            $parsedContent = json_decode($httpContent, true);
+            if (200 == $httpStatusCode) {
+                return $parsedContent;
+            }
+        } catch (Exception $exception) {
+            myLog('RequestWechat')->error('请求微信失败:', [
+                'url' => $url,
+                'json' => $postMessage,
+                'exceptionMessage' => $exception->getMessage(),
+            ]);
+        }
+        return false;
+    }
+
+    public static function simpleGet($url, $query) {
+        $client = new Client(['timeout' => 10]);
+        try {
+            $response = $client->get($url, [
+                'query' => $query
+            ]);
+            $httpStatusCode = $response->getStatusCode();
+            $httpContent = $response->getBody()->getContents();
+            $parsedContent = json_decode($httpContent, true);
+            if (200 == $httpStatusCode)  {
+                return $parsedContent;
+            }
+        } catch (Exception $exception) {
+            myLog('RequestWechat')->error('请求微信失败:', [
+                'url' => $url,
+                'query' => $query,
+                'exceptionMessage' => $exception->getMessage(),
+            ]);
+        }
+        return false;
+    }
+}

+ 11 - 0
app/Service/Util/Support/Http/WechatURL.php

@@ -0,0 +1,11 @@
+<?php
+
+namespace App\Service\Util\Support\Http;
+
+class WechatURL
+{
+    // 获取剧目信息
+    public const vod_getdrama = 'https://api.weixin.qq.com/wxa/sec/vod/getdrama?access_token=';
+    // accessToken
+    public const access_token = 'https://api.weixin.qq.com/cgi-bin/token';
+}

+ 10 - 0
app/Service/Video/WechatCheck/DramaInfoService.php

@@ -0,0 +1,10 @@
+<?php
+
+namespace App\Service\Video\WechatCheck;
+
+class DramaInfoService
+{
+    public static function syncDramaInfo($dramaId, $accessToken) {
+
+    }
+}

+ 8 - 0
config/wechat.php

@@ -0,0 +1,8 @@
+<?php
+
+return [
+    'duanju' => [
+        // 短剧主小程序
+        'masterAppid' => env('WECHAT_DUANJU_MASTER_APPID', 'wx86822355ccd03a78'),
+    ]
+];