xielin 5 years ago
parent
commit
5fe493f604

+ 81 - 0
app/Http/Controllers/AliYunVodController.php

@@ -0,0 +1,81 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: Administrator
+ * Date: 2019-06-15
+ * Time: 10:53
+ */
+
+namespace App\Http\Controllers;
+
+
+use App\Service\AliYunVodService;
+use function GuzzleHttp\Psr7\str;
+use Illuminate\Http\Request;
+use Illuminate\Http\Response;
+use Illuminate\Support\Facades\Log;
+use Illuminate\Support\Facades\Validator;
+use ShaoZeMing\AliVod\SDK\GetPlayInfoRequest;
+use ShaoZeMing\AliVod\Services\UploadService;
+use ShaoZeMing\Aliyun\Core\DefaultAcsClient;
+use ShaoZeMing\Aliyun\Core\Profile\DefaultProfile;
+
+class AliYunVodController extends Controller
+{
+    public function __construct(AliYunVodService $aliYunVodService)
+    {
+        $this->aliYunVodService = $aliYunVodService;
+    }
+
+    /**
+     * 获取视频上传凭证信息
+     * @param Request $request
+     * @return array
+     * @throws \ShaoZeMing\Aliyun\Core\Exception\ClientException
+     * @throws \ShaoZeMing\Aliyun\Core\Exception\ServerException
+     */
+    public function getVodUploadAuth(Request $request)
+    {
+        $request = $request->all();
+        $validator = Validator::make($request, [
+            'title' => 'required',
+            'filename' => 'required'
+        ]);
+        if ($validator->fails()) {
+            return $this->response->error($validator->errors()->first(),500);
+        }
+        $instance = new UploadService(config('aliyunvod'));
+        $title = $request['title'];
+        $filename = $request['filename'];
+        $coverUrl = isset($request['cover_url']) ?$request['cover_url']:'';
+        try {
+            $result = $instance->createUploadVideo($title, $filename, '', $coverUrl);  //获取视频上传地址和凭证
+            $authInfo['UploadAddress'] = $result->UploadAddress;
+            $authInfo['VideoId'] = $result->VideoId;
+            $authInfo['RequestId'] = $result->RequestId;
+            $authInfo['UploadAuth'] = $result->UploadAuth;
+            return $authInfo;
+        } catch (\Exception $e) {
+            return $this->response->error('获取上传凭证失败' . $e->getMessage(),500);
+        }
+    }
+
+    /**
+     * //根据videoId获取播放地址
+     * @param Request $request
+     * @return array|void
+     */
+    public function getPlayUrlByVideoId(Request $request)
+    {
+        $request = $request->all();
+        $validator = Validator::make($request, [
+            'video_id' => 'required'
+        ]);
+        if ($validator->fails()) {
+            return $this->response->error($validator->errors()->first(),500);
+        }
+        $videoId = $request['video_id'];
+        $url = $this->aliYunVodService->getPlayUrlByVideoId($videoId);
+        return ['url'=>$url];
+    }
+}

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

+ 1 - 0
bootstrap/app.php

@@ -31,6 +31,7 @@ $app->configure('jwt');
 $app->configure('customer');
 $app->configure('database');
 $app->configure('constants');
+$app->configure('aliyunvod');
 
 /*
 |--------------------------------------------------------------------------

+ 1 - 0
composer.json

@@ -18,6 +18,7 @@
         "php-amqplib/php-amqplib": "^2.9",
         "predis/predis": "^1.1",
         "tymon/jwt-auth": "1.0.0-rc.4.1",
+        "shaozeming/aliyun-vod": "^2.0",
         "vlucas/phpdotenv": "^3.3"
     },
     "require-dev": {

+ 20 - 0
config/aliyunvod.php

@@ -0,0 +1,20 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: shaozeming
+ * Date: 2019/3/9
+ * Time: 11:57 AM
+ */
+
+return [
+
+    /*点播配置*/
+    'vod' => [
+        'AccessKeyID' => 'LTAINdryz1AB0KpO',
+        'AccessKeySecret' => 'xjKm6yWIewMBQdJYC3H8CnuIOvBv8l',
+        'regionId' => 'cn-shanghai',   // 点播服务接入区域
+        'timeout' => '3600',  // 如果是获取签名有效时间为多少
+        'type' => 'access',  //类型:access|sts 如果是sts 实例化对象时候需要传入$securityToken
+        'acceptFormat' => 'JSON',  //返回数据格式
+    ],
+];

+ 6 - 0
routes/api.php

@@ -17,6 +17,7 @@ $api->version('v1', [
     'namespace' => 'App\Http\Controllers',
 ], function ($api) {
 
+
     $api->group(['middleware' => 'jwt.chxq_auth'], function ($api) {
         //配置
         $api->get('config', 'ConfigController@index');
@@ -26,6 +27,11 @@ $api->version('v1', [
         //新增下载
         $api->post('download', 'DownloadController@create');
 
+        //获取上传凭证和地址
+        $api->get('getVodUploadAuth', 'AliYunVodController@getVodUploadAuth');
+        //根据videoId获取播放地址
+        $api->get('getPlayUrlByVideoId', 'AliYunVodController@getPlayUrlByVideoId');
+
         $api->group(['namespace' => 'Post'], function ($api) {
             $api->get('statistics', 'PostController@statistics');
             //发布内容