SystemBroadcast.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 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, 'endpoint' => $region])->select();
  133. $form = Form::create(Url::build('toConfigureSave', ['id' => $id]), [
  134. Form::radio('format', '存储格式', 'm3u8')->options([['label' => 'm3u8', 'value' => 'm3u8'], ['label' => 'flv', 'value' => 'flv'], ['label' => 'mp4', 'value' => 'mp4']]),
  135. Form::number('duration', '录制周期(分)', 30),
  136. Form::select('OssBucketId', 'OssBucket名称')->setOptions(function () use ($list) {
  137. $menus = [];
  138. foreach ($list as $menu) {
  139. $menus[] = ['value' => $menu['id'], 'label' => $menu['bucket_name']];
  140. }
  141. return $menus;
  142. })->filterable(1)
  143. ]);
  144. $form->setMethod('post')->setTitle('录制配置')->setSuccessScript('parent.layer.close(parent.layer.getFrameIndex(window.name));parent.$(".J_iframe:visible")[0].contentWindow.location.reload();');
  145. $this->assign(compact('form'));
  146. return $this->fetch('public/form-builder');
  147. }
  148. /**
  149. * 保存
  150. */
  151. public function toConfigureSave($id = 0)
  152. {
  153. $data = parent::postMore([
  154. ['format', 'm3u8'],
  155. ['duration', 30],
  156. ['OssBucketId', 0],
  157. ]);
  158. if (!$data['OssBucketId']) return Json::fail('请选择oss桶');
  159. if (!$data['duration']) return Json::fail('请输入录制周期(分)');
  160. if (!$id) return Json::fail('参数有误');
  161. $OssBucketId = $data['OssBucketId'];
  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. $dat['is_use'] = 1;
  169. SystemBucketModel::edit($dat, $OssBucketId, 'id');
  170. SystemBroadcastModel::where('is_del', 0)->where('live_domain_type', 'liveVideo')->where('id', $id)->update(['bucket_name' => $bucket['bucket_name']]);
  171. return Json::successful('录制配置设置成功!');
  172. } else {
  173. return Json::fail('录制配置设置失败!');
  174. }
  175. }
  176. /**
  177. * 删除录制配置
  178. */
  179. public function delLiveAppRecordConfig($id = 0)
  180. {
  181. if (!$id) return Json::fail('参数有误!');
  182. $domain = SystemBroadcastModel::where('is_del', 0)->where('live_domain_type', 'liveVideo')->where('id', $id)->find();
  183. if (!$domain) return Json::fail('域名不存在!');
  184. $res = AlipayDisposeService::deleteLiveAppRecordConfigs($domain['domain_name'], $domain['region'], '*', '*');
  185. if ($res) {
  186. SystemBucketModel::where('is_del', 0)->where('bucket_name', $domain['bucket_name'])->update(['is_use' => 0]);
  187. SystemBroadcastModel::where('is_del', 0)->where('live_domain_type', 'liveVideo')->where('id', $id)->update(['bucket_name' => '']);
  188. return Json::successful('录制配置设置成功!');
  189. } else {
  190. return Json::fail('录制配置设置失败!');
  191. }
  192. }
  193. /**
  194. * 保存视频直播域名
  195. */
  196. public function save()
  197. {
  198. $data = parent::postMore([
  199. ['domain_name', ''],
  200. ['region', ''],
  201. ['live_domain_type', 'liveVideo'],
  202. ['scope', 'domestic'],
  203. ]);
  204. if (!$data['domain_name']) return Json::fail('请输入直播域名');
  205. if (mb_substr($data['domain_name'], 0, 1, 'utf-8') == '*') return Json::fail('暂不支持添加泛域名');
  206. if (SystemBroadcastModel::be(['domain_name' => $data['domain_name'], 'is_del' => 0])) return Json::fail('域名已存在!');
  207. SystemBroadcastModel::beginTrans();
  208. $res = AlipayDisposeService::addLiveDomain($data);
  209. if ($res) {
  210. $res1 = AlipayDisposeService::describeLiveDomainDetails($data['domain_name'], $data['region']);
  211. $res2 = true;
  212. if ($data['live_domain_type'] == 'liveEdge') {
  213. $res2 = AlipayDisposeService::setLiveStreamsNotifyUrlConfigs($data['domain_name'], $data['region']);
  214. }
  215. $auth_key = SystemBroadcastModel::auth_key($data['domain_name'], $data['region']);
  216. $res3 = true;
  217. if ($data['live_domain_type'] == 'liveVideo') {
  218. $res3 = AlipayDisposeService::batchSetLiveDomainConfigs($data['domain_name'], $data['region']);
  219. }
  220. if ($res1 && $res2 && $res3 && $auth_key && SystemBroadcastModel::addBroadcast($data, $res1, $auth_key)) {
  221. SystemBroadcastModel::commitTrans();
  222. return Json::successful('添加直播域名成功!');
  223. } else {
  224. SystemBroadcastModel::rollbackTrans();
  225. return Json::fail('添加到数据库失败/请确认域名是否和视频直播在同一个阿里云账户下!');
  226. }
  227. } else {
  228. SystemBroadcastModel::rollbackTrans();
  229. return Json::fail('添加直播域名失败!');
  230. }
  231. }
  232. /**
  233. * 删除推流回调地址
  234. */
  235. public function delNotifUrl($id = 0)
  236. {
  237. if (!$id) return Json::fail('参数有误!');
  238. $broadcast = SystemBroadcastModel::where('id', $id)->where('is_del', 0)->find();
  239. if (!$broadcast) return Json::fail('直播域名不存在!');
  240. $res = AlipayDisposeService::deleteLiveStreamsNotifyUrlConfigs($broadcast['domain_name'], $broadcast['region']);
  241. if ($res) {
  242. return Json::successful('删除推流回调地址成功!');
  243. } else {
  244. return Json::fail('删除推流回调地址失败!');
  245. }
  246. }
  247. /**
  248. * 删除视频直播域名
  249. */
  250. public function delete($id = 0)
  251. {
  252. if (!$id) return Json::fail('参数错误');
  253. $broadcast = SystemBroadcastModel::where('id', $id)->where('is_del', 0)->find();
  254. if (!$broadcast) return Json::fail('删除直播域名不存在');
  255. $res = AlipayDisposeService::deleteLiveDomains($broadcast['domain_name'], $broadcast['region']);
  256. if ($res) {
  257. SystemBroadcastModel::where('id', $id)->update(['is_del' => 1]);
  258. return Json::successful('删除视频直播域名成功');
  259. } else {
  260. return Json::fail('删除视频直播域名失败');
  261. }
  262. }
  263. /**停用域名
  264. * @param int $id
  265. */
  266. public function offlines($id = 0)
  267. {
  268. if (!$id) return Json::fail('参数错误');
  269. $broadcast = SystemBroadcastModel::where('id', $id)->where('is_del', 0)->find();
  270. if (!$broadcast) return Json::fail('停用域名不存在');
  271. $res = AlipayDisposeService::stopLiveDomains($broadcast['domain_name'], $broadcast['region']);
  272. if ($res) {
  273. SystemBroadcastModel::where('id', $id)->update(['domain_status' => 'offline']);
  274. return Json::successful('停用域名成功');
  275. } else {
  276. return Json::fail('停用域名失败');
  277. }
  278. }
  279. /**启用域名
  280. * @param int $id
  281. */
  282. public function onlines($id = 0)
  283. {
  284. if (!$id) return Json::fail('参数错误');
  285. $broadcast = SystemBroadcastModel::where('id', $id)->where('is_del', 0)->find();
  286. if (!$broadcast) return Json::fail('启用不存在');
  287. $res = AlipayDisposeService::startLiveDomains($broadcast['domain_name'], $broadcast['region']);
  288. if ($res) {
  289. SystemBroadcastModel::where('id', $id)->update(['domain_status' => 'online']);
  290. return Json::successful('启用域名成功');
  291. } else {
  292. return Json::fail('启用域名失败');
  293. }
  294. }
  295. /**使用直播域名
  296. * @param int $id
  297. */
  298. public function userLiveUse($id = 0)
  299. {
  300. if (!$id) return Json::fail('参数错误');
  301. $broadcast = SystemBroadcastModel::where('id', $id)->where('live_domain_type', 'liveVideo')->where('domain_status', 'online')->where('is_del', 0)->find();
  302. if (!$broadcast) return Json::fail('域名不存在或不是播流域名');
  303. if (!$broadcast['push_domain']) return Json::fail('请绑定推流域名');
  304. $push_domain = SystemBroadcastModel::where('domain_name', $broadcast['push_domain'])->where('live_domain_type', 'liveEdge')->where('domain_status', 'online')->where('is_del', 0)->find();
  305. if (!$push_domain) return Json::fail('绑定的推流域名不存在');
  306. $bucket = SystemBucketModel::where('is_del', 0)->where('bucket_name', $broadcast['bucket_name'])->find();
  307. if (!$bucket) return Json::fail('储存oss桶不存在');
  308. $res = SystemConfigService::setOneValue('aliyun_live_rtmpLink', $broadcast['push_domain']);
  309. $res1 = SystemConfigService::setOneValue('aliyun_live_playLike', $broadcast['domain_name']);
  310. $res2 = SystemConfigService::setOneValue('aliyun_live_push_key', $push_domain['auth_key1']);
  311. $res3 = SystemConfigService::setOneValue('aliyun_live_play_key', $broadcast['auth_key1']);
  312. $res4 = SystemConfigService::setOneValue('aliyun_live_appName', 'zsffLive');
  313. $res5 = SystemConfigService::setOneValue('aliyun_live_oss_bucket', $broadcast['bucket_name']);
  314. $res6 = SystemConfigService::setOneValue('aliyun_live_end_point', $bucket['endpoint']);
  315. if ($res && $res1 && $res2 && $res3 && $res4 && $res5 && $res6) {
  316. SystemBucketModel::where('is_use', 2)->where('is_del', 0)->update(['is_use' => 0]);
  317. SystemBucketModel::where('id', $id)->where('is_del', 0)->update(['is_use' => 2]);
  318. SystemBroadcastModel::where('id', $id)->where('live_domain_type', 'liveVideo')->where('domain_status', 'online')->where('is_del', 0)->update(['is_use' => 1]);
  319. SystemBroadcastModel::where('domain_name', $broadcast['push_domain'])->where('live_domain_type', 'liveEdge')->where('domain_status', 'online')->where('is_del', 0)->update(['is_use' => 1]);
  320. return Json::successful('设置直播域名成功');
  321. } else {
  322. return Json::fail('设置直播域名失败');
  323. }
  324. }
  325. }