|
|
@@ -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,
|
|
|
+ ];
|
|
|
}
|
|
|
}
|