Parcourir la source

剧目提审,素材上传

liuzejian il y a 1 an
Parent
commit
af84278145

+ 1 - 1
app/Console/Commands/Video/WechatCheck/SyncDramaInfo.php

@@ -4,7 +4,7 @@ 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 App\Service\Util\Support\Http\HttpRequestService;
 use Illuminate\Console\Command;
 use Predis\Command\Traits\DB;
 

+ 4 - 0
app/Console/Kernel.php

@@ -19,6 +19,10 @@ class Kernel extends ConsoleKernel
         // $schedule->command('inspire')->hourly();
         //每月24号执行创建表
         $schedule->command('create_track_table')->monthlyOn(24);
+        /**
+         * 同步短剧剧目信息
+         */
+        $schedule->command('WechatCheck:SyncDramaInfo')->daily();
     }
 
     /**

+ 62 - 0
app/Jobs/Video/WechatCheck.php

@@ -2,13 +2,20 @@
 
 namespace App\Jobs\Video;
 
+use App\Service\Miniprogram\Wechat\AccessTokenService;
+use App\Service\Util\Support\Http\WechatURL;
 use App\Service\Util\Support\Trace\TraceContext;
+use App\Service\Util\Support\Http\HttpRequestService;
+use GuzzleHttp\Psr7\Utils;
 use Illuminate\Bus\Queueable;
 use Illuminate\Contracts\Queue\ShouldBeUnique;
 use Illuminate\Contracts\Queue\ShouldQueue;
 use Illuminate\Foundation\Bus\Dispatchable;
 use Illuminate\Queue\InteractsWithQueue;
 use Illuminate\Queue\SerializesModels;
+use Illuminate\Support\Arr;
+use Illuminate\Support\Facades\Redis;
+use Predis\Command\Traits\DB;
 
 class WechatCheck implements ShouldQueue
 {
@@ -33,5 +40,60 @@ class WechatCheck implements ShouldQueue
         ]);
         $traceContext = TraceContext::newFromParent($this->info['traceInfo']);
 
+        $id = $this->info['id'];
+        $record = DB::table('video_wechat_check')
+            ->join('videos', 'videos.id', 'video_wechat_check.video_id')
+            ->where(['video_wechat_check.id' => $id,
+                'video_wechat_check.is_enabled' => 1, 'video_wechat_check.status' => 0])
+            ->select('video_wechat_check.*', 'videos.name as video_name', 'videos.cover_image as video_cover_image')
+            ->first();
+        if(!$record) {
+            myLog('WechatCheck')->info('当前状态不支持提审', [
+                'traceInfo' => $traceContext->getTraceInfo()
+            ]);
+            return;
+        }
+        $accessToken = Redis::get(AccessTokenService::getAccessTokenRedisKey($record->appid ?:
+        config('wechat.duanju.masterAppid')));
+        $medias = DB::table('video_series_wechat_check')
+            ->where('video_id', $record->video_id)
+            ->where(['sync_status' => 4, 'is_enabled' => 1])
+            ->where('media_id', '<>', 0)
+            ->get();
+        $cover_material_id = $this->getMaterialId($record->video_cover_image, $accessToken);
+        $authorized_material_id = $this->getMaterialId($record->authorized_img, $accessToken);
+        $publish_license_material_id = $this->getMaterialId($record->production_license_img, $accessToken);
+        $postData = [
+            'name' => $record->video_name,
+            'media_count' => $medias->count(),
+            'media_id_list' => $medias->pluck('media_id')->toArray(),
+            'producer' => $record->producer,
+            'cover_material_id' =>$cover_material_id,
+            'authorized_material_id' => $authorized_material_id,
+            'publish_license' => $record->publish_license,
+            ''
+        ];
+        if($record->drama_id) {
+            $postData['drama_id'] = $record->drama_id;
+        }
+        HttpRequestService::simplePost(WechatURL::vod_auditdrama . $accessToken, );
+
+    }
+
+    public function getMaterialId($url, $accessToken){
+        $result = HttpRequestService::post(WechatURL::media_upload. $accessToken, [
+            'multipart' => [
+                [
+                    'name' => 'type',
+                    'contents' => 'image',
+                ],
+                [
+                    'name' => 'media',
+                    'contents' => file_get_contents($url),
+                    'filename' => Arr::last(explode('/', $url)),
+                ]
+            ]
+        ]);
+        return $result;
     }
 }

+ 1 - 2
app/Service/Miniprogram/Wechat/AccessTokenService.php

@@ -3,8 +3,7 @@
 namespace App\Service\Miniprogram\Wechat;
 
 use App\Service\Util\Support\Http\WechatURL;
-use App\Services\Util\Support\Http\HttpRequestService;
-use GuzzleHttp\Client;
+use App\Service\Util\Support\Http\HttpRequestService;
 
 class AccessTokenService
 {

+ 24 - 9
app/Service/Util/Support/Http/HttpRequestService.php

@@ -1,9 +1,5 @@
 <?php
-
-
-namespace App\Services\Util\Support\Http;
-
-
+namespace App\Service\Util\Support\Http;
 
 use GuzzleHttp\Client;
 
@@ -31,8 +27,8 @@ class HttpRequestService
             if (200 == $httpStatusCode) {
                 return $parsedContent;
             }
-        } catch (Exception $exception) {
-            myLog('RequestWechat')->error('请求微信失败:', [
+        } catch (\Exception $exception) {
+            myLog('RequestWechat')->error('请求上游失败:', [
                 'url' => $url,
                 'json' => $postMessage,
                 'exceptionMessage' => $exception->getMessage(),
@@ -41,6 +37,25 @@ class HttpRequestService
         return false;
     }
 
+    public static function post($url, $options) {
+        $client = new Client(['timeout' => 10]);
+        try {
+            $res = $client->request('POST', $url, $options);
+            $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,
+                'exceptionMessage' => $exception->getTraceAsString(),
+            ]);
+        }
+        return false;
+    }
+
     public static function simpleGet($url, $query) {
         $client = new Client(['timeout' => 10]);
         try {
@@ -53,8 +68,8 @@ class HttpRequestService
             if (200 == $httpStatusCode)  {
                 return $parsedContent;
             }
-        } catch (Exception $exception) {
-            myLog('RequestWechat')->error('请求微信失败:', [
+        } catch (\Exception $exception) {
+            myLog('HttpRequest')->error('请求上游失败:', [
                 'url' => $url,
                 'query' => $query,
                 'exceptionMessage' => $exception->getMessage(),

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

@@ -8,4 +8,8 @@ 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';
+    // 剧目提审
+    public const vod_auditdrama = 'https://api.weixin.qq.com/wxa/sec/vod/auditdrama?access_token=';
+    // 新增临时素材
+    public const media_upload = 'https://api.weixin.qq.com/cgi-bin/media/upload?access_token=';
 }

+ 23 - 0
tests/Jobs/Video/WechatCheckTest.php

@@ -0,0 +1,23 @@
+<?php
+
+namespace Tests\Jobs\Video;
+
+use App\Jobs\Video\WechatCheck;
+use GuzzleHttp\Psr7\Utils;
+use PHPUnit\Framework\TestCase;
+
+class WechatCheckTest extends \Tests\TestCase
+{
+
+    public function testGetMaterialId()
+    {
+        $wechatCheck = new WechatCheck([]);
+        $url  = 'https://minifile-cdn.zvyhjkx.com/uploads/images/20230531/9NUcrj2Dfz1685513143.png';
+//        dump(fopen($url, 'r'));
+//        $url = '/Users/liuzejian/Pictures/images.png';
+//        dump(Utils::tryFopen($url, 'r'));
+        $accessToken = '70_8irXfqI0X-CVZh_2kd5UylnUIaAONdTv9AglcuYqNXwUdhiPylun0aNlOPTNl3nXamPo_WJEcu9XstHMSVtRZh4PQw0SAKxrKOKDL2VvxIvaCpnzSw54DWs95b8VUYcAIARQU';
+        $result = $wechatCheck->getMaterialId($url, $accessToken);
+        dump($result);
+    }
+}