liuzejian 1 год назад
Родитель
Сommit
8639739b7f

+ 4 - 3
modules/Common/Errors/Errors.php

@@ -30,8 +30,9 @@ class Errors
     public const  TIXIAN_ONLY_ONCE_EVERY_DAY = [500204, '每天只能提现一次'];
     public const  OPERATION_FIRST_PAGE_LIST_NOT_EXISTS = [500301, '首页列表配置项不存在'];
     public const  VIDEO_SERIES_NOT_EXISTS = [500302, '剧集不存在'];
-    public const REQUEST_HTTP_STATUS_ERROR = [500401, '请求上游接口返回http状态码有误'];
-    public const REQUEST_CODE_STATUS_ERROR = [500402, '请求上游接口返回code状态码有误'];
-    public const SYNC_WECHAT_NOT_OK = [500302, '剧集没有成功同步到微信'];
+    public const  REQUEST_HTTP_STATUS_ERROR = [500401, '请求上游接口返回http状态码有误'];
+    public const  REQUEST_CODE_STATUS_ERROR = [500402, '请求上游接口返回code状态码有误'];
+    public const  SYNC_WECHAT_NOT_OK = [500303, '剧集没有成功同步到微信'];
     public const  MINIPROGRAM_OWNER_ACCOUNT_ROLE_ERROR= [5000501, '所分配的账号不是投放公司账号'];
+    public const  GET_WECHAT_MEDIA_LINK_ERROR = [500304, '获取短剧播放链接失败'];
 }

+ 61 - 0
modules/Common/Support/Http/HttpRequest.php

@@ -0,0 +1,61 @@
+<?php
+namespace Modules\Common\Support\Http;
+
+use GuzzleHttp\Client;
+
+class HttpRequest
+{
+
+    /**
+     * 发送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('HttpRequest')->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('HttpRequest')->error('请求失败:', [
+                'url' => $url,
+                'query' => $query,
+                'exceptionMessage' => $exception->getMessage(),
+            ]);
+        }
+        return false;
+    }
+}

+ 13 - 0
modules/Common/Support/Http/WechatURL.php

@@ -0,0 +1,13 @@
+<?php
+
+namespace Modules\Common\Support\Http;
+
+class WechatURL
+{
+    // 短剧拉取上传
+    public const vod_pullupload = 'https://api.weixin.qq.com/wxa/sec/vod/pullupload?access_token=';
+    // 短剧拉取任务查询
+    public const vod_gettask = 'https://api.weixin.qq.com/wxa/sec/vod/gettask?access_token=';
+    // 获取短剧播放链接
+    public const vod_getmedialink = 'https://api.weixin.qq.com/wxa/sec/vod/getmedialink?access_token=';
+}

+ 8 - 1
modules/Video/Http/Controllers/VideoSeriesWechatCheckController.php

@@ -39,7 +39,11 @@ class VideoSeriesWechatCheckController extends CatchController
         foreach ($series as $item) {
             $item->video_url = config('common.qiniu.publicVideoLinkDomain') . DIRECTORY_SEPARATOR . $item->video_key;
             $item->media_name = sprintf('%s - 第%s集', $item->name, $item->series_sequence);
-            $taskId = WechatCheckSyncService::pullupload($item, $accessToken);
+            $parsedContent = WechatCheckSyncService::pullupload($item, $accessToken);
+            if(false === $parsedContent) {
+                CommonBusinessException::throwError(Errors::SYNC_WECHAT_NOT_OK);
+            }
+            $taskId = $parsedContent['task_id'];
             DB::table('video_series_wechat_check')
                 ->where(['series_id' => $item->id, 'is_enabled' => 1])
                 ->update(['is_enabled' => 0, 'updated_at' => $now]);
@@ -73,6 +77,9 @@ class VideoSeriesWechatCheckController extends CatchController
         }
 
         $medialinkInfo = WechatCheckSyncService::getMedialinkInfo($seriesId);
+        if(false === $medialinkInfo) {
+            CommonBusinessException::throwError(Errors::GET_WECHAT_MEDIA_LINK_ERROR);
+        }
         $link = $medialinkInfo['mp4_url'];
         Redis::setex($redisKey, 6000, $link);
         return $link;

+ 10 - 41
modules/Video/Services/WechatCheckSyncService.php

@@ -6,6 +6,8 @@ use GuzzleHttp\Client;
 use Illuminate\Support\Facades\DB;
 use Modules\Common\Errors\Errors;
 use Modules\Common\Exceptions\CommonBusinessException;
+use Modules\Common\Support\Http\HttpRequest;
+use Modules\Common\Support\Http\WechatURL;
 use Modules\Manage\Services\WechatMiniprogramService;
 
 
@@ -19,12 +21,10 @@ class WechatCheckSyncService
      * @throws \GuzzleHttp\Exception\GuzzleException
      */
     public static function pullupload($item, $accessToken) {
-        $url = 'https://api.weixin.qq.com/wxa/sec/vod/pullupload?access_token='.$accessToken;
-        $parsedContent = self::postWechat($url, [
+        return HttpRequest::simplePost(WechatURL::vod_pullupload. $accessToken, [
             'media_name' => $item->media_name,
             'media_url' => $item->video_url
         ]);
-        return $parsedContent['task_id'];
     }
 
     /**
@@ -46,13 +46,11 @@ class WechatCheckSyncService
         }
         $mediaId = $syncInfo->media_id;
         $accessToken = WechatMiniprogramService::getDuanjuCheckAccessToken($syncInfo->appid);
-        $url = 'https://api.weixin.qq.com/wxa/sec/vod/getmedialink?access_token='.$accessToken;
 
-        $parsedContent = self::postWechat($url, [
+        return HttpRequest::simplePost(WechatURL::vod_getmedialink. $accessToken, [
             'media_id' => $mediaId,
             't' => time() + 7200,
         ]);
-        return $parsedContent['media_info'];
     }
 
     public static function getWechatMediaLinkRedisKey($seriesId) {
@@ -60,44 +58,15 @@ class WechatCheckSyncService
     }
 
     public static function getTask($syncInfo) {
-        try {
-            $accessToken = WechatMiniprogramService::getDuanjuCheckAccessToken($syncInfo->appid);
-            $url = 'https://api.weixin.qq.com/wxa/sec/vod/gettask?access_token='.$accessToken;
-            $parsedContent = self::postWechat($url, [
-                'task_id' => $syncInfo->sync_task_id
-            ]);
+        $accessToken = WechatMiniprogramService::getDuanjuCheckAccessToken($syncInfo->appid);
+        $parsedContent = HttpRequest::simplePost(WechatURL::vod_gettask . $accessToken, [
+            'task_id' => $syncInfo->sync_task_id
+        ]);
+        if(false === $parsedContent || (0 != $parsedContent['errcode'] ?? 0)) {
             return $parsedContent['task_info'];
-        } catch (\Exception $exception) {
+        } else {
             return [];
         }
 
     }
-
-    /**
-     *  post 请求微信上游
-     * @param $url
-     * @param $data
-     * @throws \GuzzleHttp\Exception\GuzzleException
-     */
-    public static function postWechat($url, $data) {
-        $client = new Client(['timeout' => 3]);
-        $httpResult = $client->post($url, $data);
-
-        $httpStatus = $httpResult->getStatusCode();
-        if(200 != $httpStatus) {
-            CommonBusinessException::throwError(Errors::REQUEST_HTTP_STATUS_ERROR);
-        }
-        $httpContent = $httpResult->getBody()->getContents();
-        $parsedContent = \json_decode($httpContent, true);
-        if(0 != ($parsedContent['errcode'] ?? 0)) {
-            myLog('WechatCheckSync')->error('请求微信异常', [
-                'url' => $url,
-                'data' => $data,
-                'errMsg' => $httpContent
-            ]);
-            CommonBusinessException::throwError(Errors::REQUEST_CODE_STATUS_ERROR);
-        }
-
-        return $parsedContent;
-    }
 }