SystemBroadcast.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\admin\controller\setting;
  12. use service\JsonService as Json;
  13. use service\FormBuilder as Form;
  14. use think\Request;
  15. use think\Url;
  16. use app\admin\controller\AuthController;
  17. use service\AlipayDisposeService;
  18. use app\admin\model\system\SystemBucket as SystemBucketModel;
  19. use app\admin\model\system\SystemBroadcast as SystemBroadcastModel;
  20. use service\SystemConfigService;
  21. /**
  22. * Class SystemBroadcast
  23. * @package app\admin\controller\setting
  24. */
  25. class SystemBroadcast extends AuthController
  26. {
  27. protected static $endpoint =[
  28. '华北2(北京)'=>'cn-beijing',
  29. '华东2(上海)'=>'cn-shanghai',
  30. '华南1(深圳)'=>'cn-shenzhen',
  31. '华北1(青岛)'=>'cn-qingdao',
  32. '亚太东南1(新加坡)'=>'ap-southeast-1',
  33. '德国'=>'eu-central-1',
  34. '亚太东北1(东京)'=>'ap-northeast-1',
  35. '印度(孟买)'=>'ap-south-1',
  36. '印度尼西亚(雅加达)'=>'ap-southeast-5',
  37. ];
  38. /**
  39. * 视频直播配置页面
  40. * @return \think\Response
  41. */
  42. public function index()
  43. {
  44. $this->assign(['list'=>SystemBroadcastModel::broadcastList()]);
  45. return $this->fetch();
  46. }
  47. /**
  48. * 添加视频直播域名
  49. */
  50. public function create()
  51. {
  52. $endpoint=self::$endpoint;
  53. $this->assign(['endpoint'=>json_encode($endpoint)]);
  54. return $this->fetch();
  55. }
  56. /**
  57. *播流域名添加推流域名
  58. */
  59. public function addStreaming($id=0)
  60. {
  61. if(!$id) return $this->failed('参数有误');
  62. $domain=SystemBroadcastModel::where('is_del',0)->where('live_domain_type','liveVideo')->where('id',$id)->find();
  63. if(!$domain) return $this->failed('域名不存在或不是播流域名');
  64. $region=$domain['region'];
  65. $list = SystemBroadcastModel::where('is_del',0)->where('region',$region)->where('live_domain_type','liveEdge')->select();
  66. if(count($list)<1) return $this->failed('请先添加推流域名');
  67. $form = Form::create(Url::build('streamingSave',['id'=>$id]), [
  68. Form::select('push_flow', '添加推流域名')->setOptions(function () use($list) {
  69. $menus=[];
  70. foreach ($list as $menu) {
  71. $menus[] = ['value' => $menu['domain_name'], 'label' => $menu['domain_name']];
  72. }
  73. return $menus;
  74. })->filterable(1),
  75. ]);
  76. $form->setMethod('post')->setTitle('播流域名配置')->setSuccessScript('parent.layer.close(parent.layer.getFrameIndex(window.name));parent.$(".J_iframe:visible")[0].contentWindow.location.reload();');
  77. $this->assign(compact('form'));
  78. return $this->fetch('public/form-builder');
  79. }
  80. /**
  81. * 保存数据
  82. */
  83. public function streamingSave($id=0)
  84. {
  85. $data = parent::postMore([
  86. ['push_flow', ''],
  87. ]);
  88. if (!$data['push_flow']) return Json::fail('请选择推流域名');
  89. if(!$id) return Json::fail('参数有误');
  90. $pushFlow=SystemBroadcastModel::where('is_del',0)->where('live_domain_type','liveEdge')->where('domain_name',$data['push_flow'])->find();
  91. if(!$pushFlow) return Json::fail('推流域名不存在或不是推流域名');
  92. $domain=SystemBroadcastModel::where('is_del',0)->where('live_domain_type','liveVideo')->where('id',$id)->find();
  93. if(!$domain) return Json::fail('域名不存在或不是播流域名');
  94. $describe=AlipayDisposeService::describeLiveStreamsNotifyUrlConfig($pushFlow['domain_name'],$pushFlow['region']);
  95. if(!$describe['LiveStreamsNotifyConfig']['NotifyUrl']){
  96. $res2=AlipayDisposeService::setLiveStreamsNotifyUrlConfigs($pushFlow['domain_name'],$pushFlow['region']);
  97. if(!$res2) return Json::fail('请设置推流域名回调地址!');
  98. }
  99. if(SystemBroadcastModel::be(['is_del'=>0,'live_domain_type'=>'liveVideo','push_domain'=>$data['push_flow']])) return Json::fail('该推流域名已绑定!');
  100. $res=AlipayDisposeService::addLiveDomainMappings($domain['domain_name'],$data['push_flow'],$domain['region']);
  101. if($res){
  102. SystemBroadcastModel::where('is_del',0)->where('live_domain_type','liveVideo')->where('id',$id)->update(['push_domain'=>$data['push_flow']]);
  103. return Json::successful('添加成功!');
  104. }else{
  105. return Json::fail('添加失败!');
  106. }
  107. }
  108. /**
  109. * 删除推流域名
  110. */
  111. public function delStreaming($id=0)
  112. {
  113. if(!$id) return Json::fail('参数有误');
  114. $domain=SystemBroadcastModel::where('is_del',0)->where('live_domain_type','liveVideo')->where('id',$id)->find();
  115. if(!$domain) return Json::fail('域名不存在或不是播流域名');
  116. $res=AlipayDisposeService::deleteLiveDomainMappings($domain['domain_name'],$domain['push_domain'],$domain['region']);
  117. if($res){
  118. SystemBroadcastModel::where('is_del',0)->where('live_domain_type','liveVideo')->where('id',$id)->update(['push_domain'=>'']);
  119. return Json::successful('删除成功!');
  120. }else{
  121. return Json::fail('删除失败!');
  122. }
  123. }
  124. /**
  125. *播流域名的录制配置
  126. */
  127. public function toConfigure($id=0)
  128. {
  129. if(!$id) return $this->failed('参数有误');
  130. $domain=SystemBroadcastModel::where('is_del',0)->where('live_domain_type','liveVideo')->where('id',$id)->find();
  131. $region='oss-'.$domain['region'].'.aliyuncs.com';
  132. $list = SystemBucketModel::where(['is_del'=>0,'is_use'=>0])->where('endpoint',$region)->select();
  133. if(count($list)<1) return $this->failed('请先添加对象存储oss');
  134. $form = Form::create(Url::build('toConfigureSave',['id'=>$id]), [
  135. Form::radio('format', '存储格式','m3u8')->options([['label' => 'm3u8', 'value' => 'm3u8'], ['label' => 'flv', 'value' => 'flv'], ['label' => 'mp4', 'value' => 'mp4']]),
  136. Form::number('duration', '录制周期(分)',30),
  137. Form::select('OssBucketId', 'OssBucket名称')->setOptions(function () use($list) {
  138. $menus=[];
  139. foreach ($list as $menu) {
  140. $menus[] = ['value' => $menu['id'], 'label' => $menu['bucket_name']];
  141. }
  142. return $menus;
  143. })->filterable(1),
  144. ]);
  145. $form->setMethod('post')->setTitle('录制配置')->setSuccessScript('parent.layer.close(parent.layer.getFrameIndex(window.name));parent.$(".J_iframe:visible")[0].contentWindow.location.reload();');
  146. $this->assign(compact('form'));
  147. return $this->fetch('public/form-builder');
  148. }
  149. /**
  150. * 保存
  151. */
  152. public function toConfigureSave($id=0)
  153. {
  154. $data = parent::postMore([
  155. ['format', 'm3u8'],
  156. ['duration', 30],
  157. ['OssBucketId', 0],
  158. ]);
  159. if (!$data['OssBucketId']) return Json::fail('请选择oss桶');
  160. if (!$data['duration']) return Json::fail('请输入录制周期(分)');
  161. if(!$id) return Json::fail('参数有误');
  162. $domain=SystemBroadcastModel::where('is_del',0)->where('live_domain_type','liveVideo')->where('id',$id)->find();
  163. if(!$domain)return Json::fail('播流域名不存在');
  164. $bucket=SystemBucketModel::where('is_del',0)->where('id',$data['OssBucketId'])->find();
  165. if(!$bucket)return Json::fail('储存空间不存在');
  166. $res=AlipayDisposeService::addLiveAppRecordConfigs($domain['domain_name'],$domain['region'],'*','*',$bucket['bucket_name'],$bucket['endpoint'],$data['format'],$data['duration']);
  167. if($res){
  168. SystemBucketModel::where('is_del',0)->where('id',$data['OssBucketId'])->update(['is_use'=>2]);
  169. SystemBroadcastModel::where('is_del',0)->where('live_domain_type','liveVideo')->where('id',$id)->update(['bucket_name'=>$bucket['bucket_name']]);
  170. return Json::successful('录制配置设置成功!');
  171. }else{
  172. return Json::fail('录制配置设置失败!');
  173. }
  174. }
  175. /**
  176. * 删除录制配置
  177. */
  178. public function delLiveAppRecordConfig($id=0)
  179. {
  180. if(!$id) return Json::fail('参数有误!');
  181. $domain=SystemBroadcastModel::where('is_del',0)->where('live_domain_type','liveVideo')->where('id',$id)->find();
  182. if(!$domain) return Json::fail('域名不存在!');
  183. $res=AlipayDisposeService::deleteLiveAppRecordConfigs($domain['domain_name'],$domain['region'],'*','*');
  184. if($res){
  185. SystemBucketModel::where('is_del',0)->where('bucket_name',$domain['bucket_name'])->update(['is_use'=>0]);
  186. SystemBroadcastModel::where('is_del',0)->where('live_domain_type','liveVideo')->where('id',$id)->update(['bucket_name'=>'']);
  187. return Json::successful('录制配置设置成功!');
  188. }else{
  189. return Json::fail('录制配置设置失败!');
  190. }
  191. }
  192. /**
  193. * 保存视频直播域名
  194. */
  195. public function save()
  196. {
  197. $data = parent::postMore([
  198. ['domain_name', ''],
  199. ['region', ''],
  200. ['live_domain_type', 'liveVideo'],
  201. ['scope', 'domestic'],
  202. ]);
  203. if (!$data['domain_name']) return Json::fail('请输入直播域名');
  204. if(mb_substr($data['domain_name'],0,1,'utf-8')=='*') return Json::fail('暂不支持添加泛域名');
  205. if(SystemBroadcastModel::be(['domain_name'=>$data['domain_name'],'is_del'=>0])) return Json::fail('域名已存在!');
  206. $res=AlipayDisposeService::addLiveDomain($data);
  207. if($res){
  208. $res1=AlipayDisposeService::describeLiveDomainDetails($data['domain_name'],$data['region']);
  209. $res2=true;
  210. if($data['live_domain_type']=='liveEdge'){
  211. $res2=AlipayDisposeService::setLiveStreamsNotifyUrlConfigs($data['domain_name'],$data['region']);
  212. }
  213. $auth_key=SystemBroadcastModel::auth_key($data['domain_name'],$data['region']);
  214. $res3=true;
  215. if($data['live_domain_type']=='liveVideo'){
  216. $res3=AlipayDisposeService::batchSetLiveDomainConfigs($data['domain_name'],$data['region']);
  217. }
  218. if($res1 && $res2 && $res3 && $auth_key && SystemBroadcastModel::addBroadcast($data,$res1,$auth_key)){
  219. return Json::successful('添加直播域名成功!');
  220. }else{
  221. return Json::fail('添加到数据库失败或添加直播域名失败!');
  222. }
  223. }else{
  224. return Json::fail('添加直播域名失败!');
  225. }
  226. }
  227. /**
  228. * 删除推流回调地址
  229. */
  230. public function delNotifUrl($id=0)
  231. {
  232. if(!$id) return Json::fail('参数有误!');
  233. $broadcast=SystemBroadcastModel::where('id',$id)->where('is_del',0)->find();
  234. if (!$broadcast) return Json::fail('直播域名不存在!');
  235. $res=AlipayDisposeService::deleteLiveStreamsNotifyUrlConfigs($broadcast['domain_name'],$broadcast['region']);
  236. if($res){
  237. return Json::successful('删除推流回调地址成功!');
  238. }else{
  239. return Json::fail('删除推流回调地址失败!');
  240. }
  241. }
  242. /**
  243. * 删除视频直播域名
  244. */
  245. public function delete($id=0)
  246. {
  247. if (!$id) return Json::fail('参数错误');
  248. $broadcast=SystemBroadcastModel::where('id',$id)->where('is_del',0)->find();
  249. if(!$broadcast) return Json::fail('删除直播域名不存在');
  250. $res=AlipayDisposeService::deleteLiveDomains($broadcast['domain_name'],$broadcast['region']);
  251. if($res){
  252. SystemBroadcastModel::where('id',$id)->update(['is_del'=>1]);
  253. return Json::successful('删除视频直播域名成功');
  254. }else{
  255. return Json::fail('删除视频直播域名失败');
  256. }
  257. }
  258. /**停用域名
  259. * @param int $id
  260. */
  261. public function offlines($id=0)
  262. {
  263. if (!$id) return Json::fail('参数错误');
  264. $broadcast=SystemBroadcastModel::where('id',$id)->where('is_del',0)->find();
  265. if(!$broadcast) return Json::fail('停用域名不存在');
  266. $res=AlipayDisposeService::stopLiveDomains($broadcast['domain_name'],$broadcast['region']);
  267. if($res){
  268. SystemBroadcastModel::where('id',$id)->update(['domain_status'=>'offline']);
  269. return Json::successful('停用域名成功');
  270. }else{
  271. return Json::fail('停用域名失败');
  272. }
  273. }
  274. /**启用域名
  275. * @param int $id
  276. */
  277. public function onlines($id=0)
  278. {
  279. if (!$id) return Json::fail('参数错误');
  280. $broadcast=SystemBroadcastModel::where('id',$id)->where('is_del',0)->find();
  281. if(!$broadcast) return Json::fail('启用不存在');
  282. $res=AlipayDisposeService::startLiveDomains($broadcast['domain_name'],$broadcast['region']);
  283. if($res){
  284. SystemBroadcastModel::where('id',$id)->update(['domain_status'=>'online']);
  285. return Json::successful('启用域名成功');
  286. }else{
  287. return Json::fail('启用域名失败');
  288. }
  289. }
  290. /**使用直播域名
  291. * @param int $id
  292. */
  293. public function userLiveUse($id=0)
  294. {
  295. if (!$id) return Json::fail('参数错误');
  296. $broadcast=SystemBroadcastModel::where('id',$id)->where('live_domain_type','liveVideo')->where('domain_status','online')->where('is_del',0)->find();
  297. if(!$broadcast) return Json::fail('域名不存在或不是播流域名');
  298. if(!$broadcast['push_domain']) return Json::fail('请绑定推流域名');
  299. $push_domain=SystemBroadcastModel::where('domain_name',$broadcast['push_domain'])->where('live_domain_type','liveEdge')->where('domain_status','online')->where('is_del',0)->find();
  300. if(!$push_domain) return Json::fail('绑定的推流域名不存在');
  301. $bucket=SystemBucketModel::where('is_del',0)->where('bucket_name',$broadcast['bucket_name'])->find();
  302. if(!$bucket) return Json::fail('储存oss桶不存在');
  303. $res=SystemConfigService::setOneValue('aliyun_live_rtmpLink',$broadcast['push_domain']);
  304. $res1=SystemConfigService::setOneValue('aliyun_live_playLike',$broadcast['domain_name']);
  305. $res2=SystemConfigService::setOneValue('aliyun_live_push_key',$push_domain['auth_key1']);
  306. $res3=SystemConfigService::setOneValue('aliyun_live_play_key',$broadcast['auth_key1']);
  307. $res4=SystemConfigService::setOneValue('aliyun_live_appName','zsffLive');
  308. $res5=SystemConfigService::setOneValue('aliyun_live_oss_bucket',$broadcast['bucket_name']);
  309. $res6=SystemConfigService::setOneValue('aliyun_live_end_point',$bucket['endpoint']);
  310. if($res && $res1 && $res2 && $res3 && $res4 && $res5 && $res6){
  311. SystemBucketModel::where('is_use',2)->where('is_del',0)->update(['is_use'=>0]);
  312. SystemBucketModel::where('id',$id)->where('is_del',0)->update(['is_use'=>2]);
  313. SystemBroadcastModel::where('id',$id)->where('live_domain_type','liveVideo')->where('domain_status','online')->where('is_del',0)->update(['is_use'=>1]);
  314. SystemBroadcastModel::where('domain_name',$broadcast['push_domain'])->where('live_domain_type','liveEdge')->where('domain_status','online')->where('is_del',0)->update(['is_use'=>1]);
  315. return Json::successful('设置直播域名成功');
  316. }else{
  317. return Json::fail('设置直播域名失败');
  318. }
  319. }
  320. }