wesmiler 2 năm trước cách đây
mục cha
commit
bece078d28

+ 17 - 2
app/Http/Controllers/Api/v1/LiveController.php

@@ -2,6 +2,7 @@
 
 namespace App\Http\Controllers\Api\v1;
 use App\Http\Controllers\Api\webApp;
+use App\Services\LiveService;
 
 /**
  * 在线直播
@@ -10,8 +11,22 @@ use App\Http\Controllers\Api\webApp;
 class LiveController extends webApp
 {
 
-    public function videoList()
+    /**
+     * 获取流地址
+     * @return array
+     */
+    public function getUrl()
     {
-        return message('获取结果');
+        $urls = LiveService::make()->getLiveUrl($this->userId);
+        return message(1010, true, $urls);
+    }
+
+    /**
+     * 创建直播间(开启直播)
+     * @return array
+     */
+    public function create()
+    {
+        return message(1002, true, []);
     }
 }

+ 2 - 1
app/Services/Api/MemberService.php

@@ -355,6 +355,7 @@ class MemberService extends BaseService
         $ipData = ToolService::make()->getIpAddress($ip,'');
         $province = isset($ipData['regionName'])? $ipData['regionName'] : '';
         $city = isset($ipData['city'])? $ipData['city'] : '';
+        $emailName = explode('@', $email)[0];
 
         // 钱包
         $data = [
@@ -363,7 +364,7 @@ class MemberService extends BaseService
             'points' => $points,
             'openid' => '',
             'avatar' => '',
-            'nickname' => $nickname ? $nickname : '用户'.mb_substr($email,3,'utf-8'),
+            'nickname' => $nickname ? $nickname : '用户'.rand(100,999).$emailName,
             'username' => $email,
             'password' => get_password('xl123456'),
             'code' => strtoupper(get_random_code(9, 'X', "{$id}")),

+ 38 - 97
app/Services/LiveService.php

@@ -28,9 +28,6 @@ class LiveService extends BaseService
 {
     // 静态对象
     protected static $instance = null;
-    private $errors = [
-        'isv.BUSINESS_LIMIT_CONTROL'=> '验证码获取频繁,请5分钟后重试~',
-    ];
 
     /**
      * 静态入口
@@ -45,109 +42,53 @@ class LiveService extends BaseService
     }
 
     /**
-     * @param $accessKeyId
-     * @param $accessKeySecret
-     * @return mixed
+     * 获取直播推流/拉流地址
+     * @param $streamName
+     * @param string $appName
+     * @param string $playType
+     * @param int $expireTime
+     * @return array
      */
-    public function createClient($accessKeyId, $accessKeySecret, $endpoint){
-        $config = new Config([
-            // 必填,您的 AccessKey ID
-            "accessKeyId" => $accessKeyId,
-            // 必填,您的 AccessKey Secret
-            "accessKeySecret" => $accessKeySecret
-        ]);
-        // 访问的域名
-        $config->endpoint = $endpoint;
-        return new Dysmsapi($config);
-    }
-
-    /**
-     * 发送短信验证码
-     * @param $mobile
-     * @param string $type
-     * @return bool
-     */
-    public function send($mobile, $type='login')
+    public function getLiveUrl($streamName, $appName='xlapp', $playType='rtmp',$expireTime=1440)
     {
-        $cacheKey = "caches:sms:{$mobile}:{$type}";
-        if(RedisService::get($cacheKey.'_lock')){
-            $this->error = '2011';
-            return false;
-        }
+        $playUrls = [];
+        //未开启鉴权Key的情况下
+        $pushDomain = ConfigService::make()->getConfigByCode('live_push_url');
+        $playDomain = ConfigService::make()->getConfigByCode('live_play_url');
+        $accessKey = ConfigService::make()->getConfigByCode('live_url_access_key');
+        if($accessKey==''){
+            $pushUrl = 'rtmp://'.$pushDomain.'/'.$appName.'/'.$streamName;
+            $playUrl = 'rtmp://'.$pushDomain.'/'.$appName.'/'.$streamName;
 
-        $config = ConfigService::make()->getConfigOptionByGroup(2);
-        $accessKey = isset($config['ali_sms_access_key'])? trim($config['ali_sms_access_key']) : '';
-        $accessSecret = isset($config['ali_sms_access_secret'])? trim($config['ali_sms_access_secret']) : '';
-        $smsTemplateCode = isset($config['ali_sms_template_code'])? trim($config['ali_sms_template_code']) : '';
-        $smsSignName = isset($config['ali_sms_sign_name'])? trim($config['ali_sms_sign_name']) : '';
-        $endpoint = isset($config['sms_endpoint'])? trim($config['sms_endpoint']) : '';
-        if(empty($accessKey) || empty($accessSecret) || empty($smsTemplateCode) || empty($smsSignName) || empty($endpoint)){
-            $this->error = 2019;
-            return false;
-        }
+            $playUrls['rtmp'] = 'rtmp://'.$playDomain.'/'.$appName.'/'.$streamName;
+            $playUrls['flv'] = 'http://'.$playDomain.'/'.$appName.'/'.$streamName.'.flv';
+            $playUrls['hls'] = 'http://'.$playDomain.'/'.$appName.'/'.$streamName.'.m3u8';
+        }else{
+            $timeStamp = time() + $expireTime * 60;
+            $sstring = '/'.$appName.'/'.$streamName.'-'.$timeStamp.'-0-0-'.$accessKey;
+            $md5hash = md5($sstring);
+            $pushUrl = 'rtmp://'.$pushDomain.'/'.$appName.'/'.$streamName.'?auth_key='.$timeStamp.'-0-0-'.$md5hash;
 
-        // 发送逻辑
-        $code = rand(1000,9999);
-        $client = $this->createClient($accessKey, $accessSecret, $endpoint);
-        $request = new SendSmsRequest();
-        $request->phoneNumbers = $mobile;
-        $request->signName = $smsSignName;
-        $request->templateCode = $smsTemplateCode;
-        $request->templateParam = json_encode(['code'=> $code], 256);
-        $runtime = new RuntimeOptions();
-        $runtime->maxIdleConns = 5;
-        $runtime->connectTimeout = 10000;
-        $runtime->readTimeout = 10000;
-        try {
-            // 复制代码运行请自行打印 API 的返回值
-            $response = $client->sendSms($request, $runtime);
-            $resultCode = $response->body->code;
-            if($resultCode == 'OK'){
-                $this->error = 2020;
-                RedisService::set($cacheKey,['code'=> $code,'mobile'=>$mobile,'bizId'=>$response->body->bizId,'date'=> date('Y-m-d H:i:s')], 600);
-                return true;
-            }else{
-                $error = isset($this->errors[$response->body->code])? $this->errors[$response->body->code] : '';
-                $this->error = $error? $error : '获取失败';
-                RedisService::set($cacheKey.'_fail', ['mobile'=> $mobile,'config'=>$config,'response'=>$response->body,'error'=>$this->error], 6 * 3600);
-                return false;
-            }
-        } catch (TeaUnableRetryError  $e){
-            $date = date('Y-m-d H:i:s');
-            logger()->error("【{$date} SMS短信验证码】发送失败:".$e->getMessage());
-            RedisService::set($cacheKey.'_error', ['mobile'=> $mobile,'config'=>$config,'error'=>$e->getMessage()], 6 * 3600);
-        }
+            $timeStamp = time() + $expireTime;
 
-        $this->error = 2021;
-        return false;
-    }
+            $rtmpSstring = '/'.$appName.'/'.$streamName.'-'.$timeStamp.'-0-0-'.$accessKey;
+            $rtmpMd5hash = md5($rtmpSstring);
+            $playUrls['rtmp'] = 'rtmp://'.$playDomain.'/'.$appName.'/'.$streamName.'?auth_key='.$timeStamp.'-0-0-'.$rtmpMd5hash;
 
-    /**
-     * 短信验证码验证
-     * @param string $mobile 手机号
-     * @param string $code 当前验证码
-     * @param string $type 验证码场景类型,login-登录,reg-注册
-     * @return bool
-     */
-    public function check($mobile, $code, $type='login')
-    {
-        if($code == '1100'){
-            return true;
-        }
+            $flvSstring = '/'.$appName.'/'.$streamName.'.flv-'.$timeStamp.'-0-0-'.$accessKey;
+            $flvMd5hash = md5($flvSstring);
+            $playUrls['flv'] = 'http://'.$playDomain.'/'.$appName.'/'.$streamName.'.flv?auth_key='.$timeStamp.'-0-0-'.$flvMd5hash;
 
-        $cacheKey = "caches:sms:{$mobile}:{$type}";
-        $data = RedisService::get($cacheKey);
-        $smsCode = isset($data['code'])? $data['code'] : '';
-        if(empty($data) || empty($smsCode)){
-            $this->error = '2012';
-            return false;
+            $hlsSstring = '/'.$appName.'/'.$streamName.'.m3u8-'.$timeStamp.'-0-0-'.$accessKey;
+            $hlsMd5hash = md5($hlsSstring);
+            $playUrls['hls'] = 'http://'.$playDomain.'/'.$appName.'/'.$streamName.'.m3u8?auth_key='.$timeStamp.'-0-0-'.$hlsMd5hash;
         }
 
-        if($smsCode != $code){
-            $this->error = '2013';
-            return false;
-        }
 
-        return true;
+        return [
+            'push_url'=> $pushUrl,
+            'play_url'=> isset($playUrls[$playType])? $playUrls[$playType] : '',
+            'play_urls'=> $playUrls,
+        ];
     }
 }

+ 4 - 0
routes/api.php

@@ -46,6 +46,10 @@ Route::prefix('v1')->group(function(){
     Route::post('/index/notice', [\App\Http\Controllers\Api\v1\IndexController::class, 'noticeCount']);
     Route::post('/index/video', [\App\Http\Controllers\Api\v1\IndexController::class, 'videoList']);
 
+    // 直播
+    Route::get('/live/url', [\App\Http\Controllers\Api\v1\LiveController::class, 'getUrl']);
+    Route::post('/live/create', [\App\Http\Controllers\Api\v1\LiveController::class, 'create']);
+
     // 会员板块
     Route::post('/user/info', [\App\Http\Controllers\Api\v1\MemberController::class, 'getInfo']);
     Route::post('/user/homeInfo', [\App\Http\Controllers\Api\v1\MemberController::class, 'homeInfo']);