瀏覽代碼

增加视频ID获取视频地址

xielin 5 年之前
父節點
當前提交
23555ce995

+ 5 - 26
app/Http/Controllers/V1/AliYunVodController.php

@@ -10,6 +10,7 @@ namespace App\Http\Controllers\V1;
 
 
 use App\Repositories\FeedRepositories;
+use App\Service\AliYunVodService;
 use Illuminate\Http\Request;
 use Illuminate\Support\Facades\Log;
 use Illuminate\Support\Facades\Validator;
@@ -20,9 +21,10 @@ use ShaoZeMing\Aliyun\Core\Profile\DefaultProfile;
 
 class AliYunVodController extends Controller
 {
-    public function __construct(FeedRepositories $feedRepositories)
+    public function __construct(FeedRepositories $feedRepositories,AliYunVodService $aliYunVodService)
     {
         $this->feedRepositories = $feedRepositories;
+        $this->aliYunVodService = $aliYunVodService;
     }
 
     /**
@@ -66,30 +68,7 @@ class AliYunVodController extends Controller
             return jsonError($validator->errors()->first());
         }
         $videoId = $request['video_id'];
-        try {
-            $client = $this->initVodClient(config('aliyunvod.vod.AccessKeyID'), config('aliyunvod.vod.AccessKeySecret'));
-            $playInfo = $this->getPlayInfo($client, $videoId);
-            return $this->jsonSuccess($playInfo);
-        } catch (\Exception $e) {
-            Log::debug('获取视频地址失败:' . $e->getMessage());
-            return $this->jsonError('获取视频地址失败');
-        }
-    }
-
-    private function initVodClient($accessKeyId, $accessKeySecret)
-    {
-        $regionId = 'cn-shanghai';  // 点播服务接入区域
-        $profile = DefaultProfile::getProfile($regionId, $accessKeyId, $accessKeySecret);
-        return new DefaultAcsClient($profile);
-    }
-
-    private function getPlayInfo($client, $videoId)
-    {
-        $request = new GetPlayInfoRequest();
-        $request->setVideoId($videoId);
-        $request->setAcceptFormat('JSON');
-        $playConfig = ['PlayDomain' => 'http://oss.caihongxingqiu.net/'];
-        $request->setPlayConfig(json_encode($playConfig));
-        return $client->getAcsResponse($request);
+        $url = $this->aliYunVodService->getPlayUrlByVideoId($videoId);
+        return $this->jsonSuccess(['url'=>$url]);
     }
 }

+ 16 - 1
app/Repositories/PostRepositories.php

@@ -19,6 +19,7 @@ use App\Models\PostImgs;
 use App\Models\PostLike;
 use App\Models\PostShare;
 use App\Models\Topic;
+use App\Service\AliYunVodService;
 use App\Service\DetectionService;
 use App\Traits\PostTrait;
 use App\Traits\UserTrait;
@@ -41,6 +42,7 @@ class PostRepositories
                                 PostCollect $postCollect,
                                 PostShare $postShare,
                                 DetectionService $detectionService,
+                                AliYunVodService $aliYunVodService,
                                 Topic $topic)
     {
         $this->post = $post;
@@ -51,6 +53,7 @@ class PostRepositories
         $this->postShare = $postShare;
         $this->detectionService = $detectionService;
         $this->topic = $topic;
+        $this->aliYunVodService = $aliYunVodService;
     }
 
     /**
@@ -104,6 +107,18 @@ class PostRepositories
         if ($detectionImageResult['code']<0) {
             return jsonError('图片违规,请修正哦');
         }
+        $videoUrl = "";
+        if(isset($request['video']) && $request['video']){
+            for($i=0;$i<3;$i++){
+                $videoUrl = $this->aliYunVodService->getPlayUrlByVideoId($request['video']);
+                if($videoUrl){
+                    break;
+                }
+            }
+        }
+        if(empty($videoUrl)){
+            return jsonError('发布失败,请重试');
+        }
 
         $data = [
             'uid' => $userInfo['uid'],
@@ -112,7 +127,7 @@ class PostRepositories
             'avatar' => $userInfo['avatar']??'',
             'type' => $request['type'],
             'img' => $request['img'],
-            'video' => isset($request['video'])? $request['video'] : '',
+            'video' => $videoUrl,
             'topic_ids' => implode(',', $topicIds),
             'title' => isset($request['title'])? $request['title'] : '',
             'content' => $request['content'],

+ 44 - 0
app/Service/AliYunVodService.php

@@ -0,0 +1,44 @@
+<?php
+namespace App\Service;
+
+
+use Illuminate\Support\Facades\Log;
+use ShaoZeMing\AliVod\SDK\GetPlayInfoRequest;
+use ShaoZeMing\Aliyun\Core\DefaultAcsClient;
+use ShaoZeMing\Aliyun\Core\Profile\DefaultProfile;
+
+class AliYunVodService
+{
+    public function getPlayUrlByVideoId($videoId)
+    {
+        try {
+            $client = $this->initVodClient(config('aliyunvod.vod.AccessKeyID'), config('aliyunvod.vod.AccessKeySecret'));
+            $playInfo = $this->getPlayInfo($client, $videoId);
+            if($playInfo && is_object($playInfo)){
+                return $playInfo->PlayInfoList->PlayInfo[0]->PlayURL;
+            }else{
+                return '';
+            }
+        } catch (\Exception $e) {
+            Log::debug('获取视频地址失败:' . $e->getMessage());
+            return '';
+        }
+    }
+
+    private function initVodClient($accessKeyId, $accessKeySecret)
+    {
+        $regionId = 'cn-shanghai';  // 点播服务接入区域
+        $profile = DefaultProfile::getProfile($regionId, $accessKeyId, $accessKeySecret);
+        return new DefaultAcsClient($profile);
+    }
+
+    private function getPlayInfo($client, $videoId)
+    {
+        $request = new GetPlayInfoRequest();
+        $request->setVideoId($videoId);
+        $request->setAcceptFormat('JSON');
+        $playConfig = ['PlayDomain' => 'http://oss.caihongxingqiu.net/'];
+        $request->setPlayConfig(json_encode($playConfig));
+        return $client->getAcsResponse($request);
+    }
+}