SellerLiveApply.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. <?php
  2. /**
  3. * 机构直播管理
  4. */
  5. namespace app\api\controller;
  6. use think\facade\Lang;
  7. use think\facade\Db;
  8. use GatewayClient\Gateway;
  9. use TencentCloud\Common\Credential;
  10. use TencentCloud\Common\Profile\ClientProfile;
  11. use TencentCloud\Common\Profile\HttpProfile;
  12. use TencentCloud\Common\Exception\TencentCloudSDKException;
  13. use TencentCloud\Live\V20180801\LiveClient;
  14. use TencentCloud\Live\V20180801\Models\DescribeLiveStreamStateRequest;
  15. use AlibabaCloud\Client\AlibabaCloud;
  16. /**
  17. * DSKMS多用户商城
  18. *
  19. * ----------------------------------------------------------------------------
  20. *
  21. * 控制器
  22. */
  23. class SellerLiveApply extends MobileSeller
  24. {
  25. public function initialize()
  26. {
  27. parent::initialize();
  28. Lang::load(base_path() . 'home/lang/' . config('lang.default_lang') . '/live.lang.php');
  29. }
  30. /**
  31. * 显示机构所有直播列表
  32. */
  33. public function get_live_apply_list()
  34. {
  35. $live_apply_model = model('live_apply');
  36. $condition[] = array('live_apply_type', 'in', [LIVE_APPLY_TYPE_GOODS]);
  37. $condition[] = array('live_apply_user_type', '=', 2);
  38. $condition[] = array('live_apply_user_id', '=', $this->store_info['store_id']);
  39. $live_apply_list = $live_apply_model->getLiveApplyList($condition, '*', 10);
  40. $goods_model = model('goods');
  41. foreach ($live_apply_list as $key => $val) {
  42. $live_apply_list[$key]['minipro_code'] = '';
  43. if ($val['live_apply_state'] == 1 && file_exists(BASE_UPLOAD_PATH . DIRECTORY_SEPARATOR . ATTACH_LIVE_APPLY . DIRECTORY_SEPARATOR . $val['live_apply_id'] . '.png')) {
  44. $live_apply_list[$key]['minipro_code'] = UPLOAD_SITE_URL . '/' . ATTACH_LIVE_APPLY . '/' . $val['live_apply_id'] . '.png';
  45. }
  46. $goods_commonid = Db::name('live_apply_goods')->where('live_apply_id', $val['live_apply_id'])->column('goods_commonid');
  47. if (!empty($goods_commonid)) {
  48. $goods_list = $goods_model->getGoodsCommonOnlineList(array(array('goods_commonid', 'in', $goods_commonid)), 'goods_name,goods_image', 10);
  49. foreach ($goods_list as $k => $v) {
  50. $goods_list[$k]['goods_image'] = goods_cthumb($v['goods_image']);
  51. }
  52. $live_apply_list[$key]['goods_list'] = $goods_list;
  53. }
  54. }
  55. $result = array_merge(array('live_apply_list' => $live_apply_list), mobile_page($live_apply_model->page_info));
  56. ds_json_encode(10000, '', $result);
  57. }
  58. /**
  59. * 删除直播
  60. */
  61. public function del_live_apply()
  62. {
  63. $live_apply_id = intval(input('param.live_apply_id'));
  64. if ($live_apply_id <= 0) {
  65. ds_json_encode(10001, lang('param_error'));
  66. }
  67. $live_apply_model = model('live_apply');
  68. $condition[] = array('live_apply_type', '=', LIVE_APPLY_TYPE_GOODS);
  69. $condition[] = array('live_apply_user_type', '=', 2);
  70. $condition[] = array('live_apply_user_id', '=', $this->store_info['store_id']);
  71. $condition[] = array('live_apply_id', '=', $live_apply_id);
  72. $live_apply = $live_apply_model->getLiveApplyInfo($condition);
  73. if (empty($live_apply)) {
  74. ds_json_encode(10001, lang('param_error'));
  75. }
  76. $live_apply_model->delLiveApply($condition);
  77. ds_json_encode(10000, '');
  78. }
  79. public function get_live_apply_info()
  80. {
  81. $msg = '';
  82. $live_apply_id = intval(input('param.live_apply_id'));
  83. if ($live_apply_id <= 0) {
  84. ds_json_encode(10001, lang('param_error'));
  85. }
  86. $live_apply_model = model('live_apply');
  87. $condition = array();
  88. $condition[] = array('live_apply_user_type', '=', 2);
  89. $condition[] = array('live_apply_user_id', '=', $this->store_info['store_id']);
  90. $condition[] = array('live_apply_id', '=', $live_apply_id);
  91. $live_apply = $live_apply_model->getLiveApplyInfo($condition);
  92. if (empty($live_apply)) {
  93. ds_json_encode(10001, lang('live_not_exit'));
  94. }
  95. //判断当前流状态
  96. // require_once root_path() . 'vendor/tencentcloud/tencentcloud-sdk-php/TCloudAutoLoader.php';
  97. if (config('ds_config.video_type') == 'aliyun') {
  98. if (!config('ds_config.aliyun_live_push_domain')) {
  99. ds_json_encode(10001, lang('aliyun_live_push_domain_empty'));
  100. }
  101. if (!config('ds_config.aliyun_live_push_key')) {
  102. ds_json_encode(10001, lang('aliyun_live_push_key_empty'));
  103. }
  104. if (!config('ds_config.aliyun_live_play_domain')) {
  105. ds_json_encode(10001, lang('aliyun_live_play_domain_empty'));
  106. }
  107. if (!config('ds_config.aliyun_live_play_key')) {
  108. ds_json_encode(10001, lang('aliyun_live_play_key_empty'));
  109. }
  110. $regionId = 'cn-shanghai';
  111. AlibabaCloud::accessKeyClient(config('ds_config.aliyun_access_key_id'), config('ds_config.aliyun_access_key_secret'))
  112. ->regionId($regionId)
  113. ->asDefaultClient();
  114. try {
  115. $result = AlibabaCloud::rpc()
  116. ->product('live')
  117. // ->scheme('https') // https | http
  118. ->version('2016-11-01')
  119. ->action('DescribeLiveStreamsOnlineList')
  120. ->method('POST')
  121. ->host('live.aliyuncs.com')
  122. ->options([
  123. 'query' => [
  124. 'RegionId' => $regionId,
  125. 'DomainName' => config('ds_config.aliyun_live_push_domain'),
  126. 'AppName' => "live",
  127. 'StreamName' => 'live_apply_' . $live_apply['live_apply_id'],
  128. 'PageSize' => "1",
  129. 'PageNum' => "1",
  130. 'QueryType' => "strict",
  131. ],
  132. ])
  133. ->request();
  134. if ($result->TotalNum) {
  135. ds_json_encode(10001, '已有另外客户端占用了此直播,请通知管理员解除占用');
  136. }
  137. } catch (\Exception $e) {
  138. ds_json_encode(10001, $e->getMessage());
  139. }
  140. } else {
  141. if (!config('ds_config.live_push_domain')) {
  142. ds_json_encode(10001, lang('live_push_domain_empty'));
  143. }
  144. if (!config('ds_config.live_push_key')) {
  145. ds_json_encode(10001, lang('live_push_key_empty'));
  146. }
  147. if (!config('ds_config.live_play_domain')) {
  148. ds_json_encode(10001, lang('live_play_domain_empty'));
  149. }
  150. try {
  151. $cred = new Credential(config('ds_config.vod_tencent_secret_id'), config('ds_config.vod_tencent_secret_key'));
  152. $httpProfile = new HttpProfile();
  153. $httpProfile->setEndpoint("live.tencentcloudapi.com");
  154. $clientProfile = new ClientProfile();
  155. $clientProfile->setHttpProfile($httpProfile);
  156. $client = new LiveClient($cred, "", $clientProfile);
  157. $req = new DescribeLiveStreamStateRequest();
  158. $params = '{"AppName":"live","DomainName":"' . config('ds_config.live_push_domain') . '","StreamName":"' . 'live_apply_' . $live_apply['live_apply_id'] . '"}';
  159. $req->fromJsonString($params);
  160. $resp = $client->DescribeLiveStreamState($req);
  161. } catch (TencentCloudSDKException $e) {
  162. ds_json_encode(10001, $e->getMessage());
  163. }
  164. if ($resp->StreamState == 'active') {
  165. ds_json_encode(10001, lang('live_occupy'));
  166. }
  167. }
  168. //生成推流url
  169. $live_apply['live_apply_push_url'] = model('live_apply')->getPushUrl('live_apply_' . $live_apply['live_apply_id'], $live_apply['live_apply_end_time']);
  170. //生成拉流url
  171. $live_apply['live_apply_play_url'] = model('live_apply')->getPlayUrl('live_apply_' . $live_apply['live_apply_id'], $live_apply['live_apply_end_time']);
  172. $extral_info = array('live_apply_image' => '');
  173. $live_apply['live_apply_cover_image_url'] = ds_get_pic(ATTACH_COMMON, config('ds_config.default_goods_image'));
  174. if ($live_apply['live_apply_cover_video']) {
  175. $live_apply['live_apply_cover_video_url'] = ds_get_pic(ATTACH_LIVE_APPLY . '/' . $live_apply['live_apply_user_id'], $live_apply['live_apply_cover_video']);
  176. } elseif ($live_apply['live_apply_cover_image']) {
  177. $live_apply['live_apply_cover_image_url'] = ds_get_pic(ATTACH_LIVE_APPLY . '/' . $live_apply['live_apply_user_id'], $live_apply['live_apply_cover_image']);
  178. }
  179. $extral_info['live_apply_image'] = $live_apply['live_apply_cover_image_url'];
  180. $online_info = false;
  181. if (config('ds_config.instant_message_register_url')) {
  182. try {
  183. // 设置GatewayWorker服务的Register服务ip和端口,请根据实际情况改成实际值(ip不能是0.0.0.0)
  184. Gateway::$registerAddress = config('ds_config.instant_message_register_url');
  185. $online_info = array(
  186. 'online_count' => Gateway::getClientIdCountByGroup('live_apply_' . $live_apply_id),
  187. 'online_list' => Gateway::getClientSessionsByGroup('live_apply_' . $live_apply_id),
  188. );
  189. } catch (\Exception $e) {
  190. $msg = $e->getMessage();
  191. }
  192. }
  193. ds_json_encode(10000, '', array('live_apply_info' => array_merge($live_apply, $extral_info, array('instant_message_url' => config('ds_config.instant_message_gateway_url'))), 'online_info' => $online_info));
  194. }
  195. public function save_live_apply()
  196. {
  197. $live_apply_model = model('live_apply');
  198. $data = array(
  199. 'live_apply_type' => LIVE_APPLY_TYPE_GOODS,
  200. 'live_apply_remark' => input('param.live_apply_remark'),
  201. 'live_apply_play_time' => input('param.live_apply_play_time'),
  202. 'live_apply_name' => input('param.live_apply_name'),
  203. 'live_apply_cover_image' => input('param.live_apply_cover_image'),
  204. 'live_apply_cover_video' => input('param.live_apply_cover_video'),
  205. 'live_apply_add_time' => TIMESTAMP,
  206. 'live_apply_user_type' => 2,
  207. 'live_apply_user_id' => $this->store_info['store_id'],
  208. );
  209. $live_apply_validate = ds_validate('live_apply');
  210. if (!$live_apply_validate->scene('live_apply_save')->check($data)) {
  211. ds_json_encode(10001, $live_apply_validate->getError());
  212. }
  213. $live_apply_id = $live_apply_model->addLiveApply($data);
  214. if ($live_apply_id) {
  215. $goods_ids = ds_delete_param(implode(',', input('param.goods_ids/a')));
  216. if (!empty($goods_ids)) {
  217. $goods_model = model('goods');
  218. $goods_list = $goods_model->getGoodsCommonOnlineList(array(array('goods_commonid', 'in', $goods_ids)), 'goods_commonid,store_id,store_name,goods_name,gc_id,gc_id_1,gc_id_2,gc_id_3,gc_name', 10);
  219. if (!empty($goods_list)) {
  220. $goods_data = array();
  221. foreach ($goods_list as $goods) {
  222. $goods_data[] = array(
  223. 'live_apply_id' => $live_apply_id,
  224. 'goods_commonid' => $goods['goods_commonid'],
  225. 'goods_name' => $goods['goods_name'],
  226. 'gc_id' => $goods['gc_id'],
  227. 'gc_id_1' => $goods['gc_id_1'],
  228. 'gc_id_2' => $goods['gc_id_2'],
  229. 'gc_id_3' => $goods['gc_id_3'],
  230. 'gc_name' => $goods['gc_name'],
  231. 'store_id' => $goods['store_id'],
  232. 'store_name' => $goods['store_name'],
  233. );
  234. }
  235. $live_apply_model->addLiveApplyGoodsAll($goods_data);
  236. }
  237. }
  238. ds_json_encode(10000);
  239. } else {
  240. ds_json_encode(10001, lang('ds_common_op_fail'));
  241. }
  242. }
  243. function join_live()
  244. {
  245. session('name');
  246. $live_apply_id = input('param.live_apply_id');
  247. $client_id = input('param.client_id');
  248. if (!config('ds_config.instant_message_register_url')) {
  249. ds_json_encode(10001, lang('instant_message_register_url_empty'));
  250. }
  251. $live_apply_model = model('live_apply');
  252. $condition[] = array('live_apply_user_type', '=', 2);
  253. $condition[] = array('live_apply_state', '=', 1);
  254. $condition[] = array('live_apply_user_id', '=', $this->store_info['store_id']);
  255. $condition[] = array('live_apply_id', '=', $live_apply_id);
  256. $live_apply = $live_apply_model->getLiveApplyInfo($condition);
  257. if (empty($live_apply)) {
  258. ds_json_encode(10001, lang('live_not_exit'));
  259. }
  260. // 设置GatewayWorker服务的Register服务ip和端口,请根据实际情况改成实际值(ip不能是0.0.0.0)
  261. try {
  262. Gateway::$registerAddress = config('ds_config.instant_message_register_url');
  263. // client_id与uid绑定
  264. Gateway::bindUid($client_id, '1:' . $this->store_info['store_id']);
  265. $online_item = array(
  266. 'instant_message_from_avatar' => get_store_logo($this->store_info['store_avatar'], 'store_avatar'),
  267. 'instant_message_from_id' => $this->store_info['store_id'],
  268. 'instant_message_from_type' => 1,
  269. 'instant_message_from_name' => $this->store_info['store_name']
  270. );
  271. Gateway::setSession($client_id, $online_item);
  272. // 加入某个群组(可调用多次加入多个群组)
  273. Gateway::joinGroup($client_id, 'live_apply_' . $live_apply_id);
  274. //更新在线人数
  275. Gateway::sendToGroup('live_apply_' . $live_apply_id, json_encode(array(
  276. 'type' => 'join',
  277. 'online_count' => Gateway::getClientIdCountByGroup('live_apply_' . $live_apply_id),
  278. 'online_list' => Gateway::getClientSessionsByGroup('live_apply_' . $live_apply_id)
  279. )));
  280. } catch (\Exception $e) {
  281. ds_json_encode(10001, $e->getMessage());
  282. }
  283. ds_json_encode(10000, '');
  284. }
  285. function leave_live()
  286. {
  287. $live_apply_id = input('param.live_apply_id');
  288. $client_id = input('param.client_id');
  289. $live_apply_push_message = input('param.live_apply_push_message', '');
  290. // if (!$live_apply_push_message) {
  291. // ds_json_encode(10001, '请输入关闭直播理由');
  292. // }
  293. $this->change_live($live_apply_id, 2, $live_apply_push_message);
  294. if ($client_id) {
  295. if (!config('ds_config.instant_message_register_url')) {
  296. ds_json_encode(10001, lang('instant_message_register_url_empty'));
  297. }
  298. // 设置GatewayWorker服务的Register服务ip和端口,请根据实际情况改成实际值(ip不能是0.0.0.0)
  299. try {
  300. Gateway::$registerAddress = config('ds_config.instant_message_register_url');
  301. // client_id与uid绑定
  302. Gateway::unbindUid($client_id, '1:' . $this->store_info['store_id']);
  303. // 加入某个群组(可调用多次加入多个群组)
  304. Gateway::leaveGroup($client_id, 'live_apply_' . $live_apply_id);
  305. //更新在线人数
  306. Gateway::sendToGroup('live_apply_' . $live_apply_id, json_encode(array(
  307. 'type' => 'leave',
  308. 'online_count' => Gateway::getClientIdCountByGroup('live_apply_' . $live_apply_id),
  309. 'online_list' => Gateway::getClientSessionsByGroup('live_apply_' . $live_apply_id),
  310. )));
  311. } catch (\Exception $e) {
  312. ds_json_encode(10001, $e->getMessage());
  313. }
  314. }
  315. ds_json_encode(10000, '');
  316. }
  317. function change_live($live_apply_id = 0, $live_apply_push_state = 0, $live_apply_push_message = '')
  318. {
  319. $if_fun = false;
  320. if (input('param.live_apply_push_state')) {
  321. $live_apply_id = input('param.live_apply_id');
  322. $live_apply_push_state = input('param.live_apply_push_state');
  323. $live_apply_push_message = input('param.live_apply_push_message', '');
  324. } else {
  325. $if_fun = true;
  326. }
  327. if ($live_apply_id <= 0) {
  328. ds_json_encode(10001, lang('param_error'));
  329. }
  330. $live_apply_model = model('live_apply');
  331. $condition = array();
  332. $condition[] = array('live_apply_user_type', '=', 2);
  333. $condition[] = array('live_apply_user_id', '=', $this->store_info['store_id']);
  334. $condition[] = array('live_apply_id', '=', $live_apply_id);
  335. $live_apply = $live_apply_model->getLiveApplyInfo($condition);
  336. if (empty($live_apply)) {
  337. ds_json_encode(10001, lang('live_not_exit'));
  338. }
  339. $data = array(
  340. 'live_apply_push_state' => $live_apply_push_state,
  341. 'live_apply_push_message' => $live_apply_push_message,
  342. );
  343. $live_apply_validate = ds_validate('live_apply');
  344. if (!$live_apply_validate->scene('live_apply_change')->check($data)) {
  345. ds_json_encode(10001, $live_apply_validate->getError());
  346. }
  347. $live_apply_model->editLiveApply($data, $condition);
  348. if (!$if_fun) {
  349. ds_json_encode(10000);
  350. }
  351. }
  352. /**
  353. * @api {POST} api/SellerLiveApply/image_upload 上传封面
  354. * @apiVersion 1.0.0
  355. * @apiGroup SellerLiveApply
  356. *
  357. * @apiHeader {String} X-DS-KEY 卖家授权token
  358. *
  359. * @apiParam {File} file 文件
  360. *
  361. * @apiSuccess {String} code 返回码,10000为成功
  362. * @apiSuccess {String} message 返回消息
  363. * @apiSuccess {String} result 店铺图片
  364. */
  365. public function image_upload()
  366. {
  367. $store_id = $this->store_info['store_id'];
  368. $store_image_name = 'file';
  369. $file_type = 'image';
  370. if (!empty($_FILES[$store_image_name]['name'])) {
  371. if ($_FILES[$store_image_name]['type'] == 'video/mp4') {
  372. $file_ext = 'mp4';
  373. } else {
  374. $file_ext = ALLOW_IMG_EXT;
  375. }
  376. $res = ds_upload_pic(ATTACH_LIVE_APPLY . DIRECTORY_SEPARATOR . $store_id, $store_image_name, '', $file_ext);
  377. if ($res['code']) {
  378. $file_name = $res['data']['file_name'];
  379. } else {
  380. ds_json_encode(10001, $res['msg']);
  381. }
  382. $data = array();
  383. $data['file_type'] = $file_type;
  384. $data['file_name'] = $file_name;
  385. $data['file_path'] = ds_get_pic(ATTACH_LIVE_APPLY . '/' . $store_id, $file_name);
  386. ds_json_encode(10000, '', $data);
  387. } else {
  388. ds_json_encode(10001, lang('param_error'));
  389. }
  390. }
  391. }