SellerLiveApply.php 20 KB

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