MemberLive.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. <?php
  2. namespace app\api\controller;
  3. use think\facade\Db;
  4. use think\facade\Lang;
  5. use GatewayClient\Gateway;
  6. use TencentCloud\Common\Credential;
  7. use TencentCloud\Common\Profile\ClientProfile;
  8. use TencentCloud\Common\Profile\HttpProfile;
  9. use TencentCloud\Common\Exception\TencentCloudSDKException;
  10. use TencentCloud\Live\V20180801\LiveClient;
  11. use TencentCloud\Live\V20180801\Models\DescribeLiveStreamStateRequest;
  12. use AlibabaCloud\Client\AlibabaCloud;
  13. /**
  14. * DSKMS多用户商城
  15. *
  16. * ----------------------------------------------------------------------------
  17. *
  18. * 虚拟订单控制器
  19. */
  20. class MemberLive extends MobileMember
  21. {
  22. public function initialize()
  23. {
  24. parent::initialize(); // TODO: Change the autogenerated stub
  25. Lang::load(base_path() . 'home/lang/' . config('lang.default_lang') . '/live.lang.php');
  26. }
  27. /**
  28. * @api {POST} api/MemberLive/get_live_info 消息列表
  29. * @apiVersion 1.0.0
  30. * @apiGroup MemberLive
  31. *
  32. * @apiHeader {String} X-DS-KEY 用户授权token
  33. *
  34. * @apiParam {Int} live_apply_id 直播id
  35. *
  36. * @apiSuccess {String} code 返回码,10000为成功
  37. * @apiSuccess {String} message 返回消息
  38. * @apiSuccess {Object} result 返回数据
  39. * @apiSuccess {Object} result.live_apply_info 直播信息
  40. * @apiSuccess {String} result.live_apply_info.live_apply_cover_image_url 直播图片封面地址
  41. * @apiSuccess {String} result.live_apply_info.live_apply_cover_video_url 直播视频封面地址
  42. * @apiSuccess {String} result.live_apply_info.live_apply_push_url 直播推流地址
  43. * @apiSuccess {String} result.live_apply_info.live_apply_play_url 直播拉流地址
  44. * @apiSuccess {String} result.live_apply_info.live_apply_user_avatar 主播头像
  45. * @apiSuccess {String} result.live_apply_info.live_apply_user_name 主播用户名
  46. * @apiSuccess {Int} result.live_apply_info.live_apply_fans 主播关注数
  47. * @apiSuccess {Int} result.live_apply_info.is_favorate 是否已关注
  48. * @apiSuccess {String} result.live_apply_info.instant_message_url 聊天websocket地址
  49. * @apiSuccess {Object} result.online_info 在线信息
  50. * @apiSuccess {Int} result.online_info.online_count 在线人数
  51. * @apiSuccess {String} result.online_info.online_list 在线列表
  52. * @apiSuccess {String} result.online_info.online_list.instant_message_from_avatar 在线用户头像
  53. * @apiSuccess {Int} result.online_info.online_list.instant_message_from_id 在线用户id
  54. * @apiSuccess {Int} result.online_info.online_list.instant_message_from_type 在线用户类型
  55. * @apiSuccess {String} result.online_info.online_list.instant_message_from_name 在线用户名称
  56. */
  57. public function get_live_info()
  58. {
  59. $live_apply_id = input('param.live_apply_id');
  60. if (!$live_apply_id) {
  61. ds_json_encode(10001, lang('param_error'));
  62. }
  63. //获取最近未结束的直播
  64. $live_apply_model = model('live_apply');
  65. $condition = array();
  66. $condition[] = array('live_apply_state', '=', 1);
  67. $condition[] = array('live_apply_end_time', '>', TIMESTAMP);
  68. $condition[] = array('live_apply_id', '=', $live_apply_id);
  69. $live_apply = $live_apply_model->getLiveApplyInfo($condition);
  70. if (!$live_apply) {
  71. ds_json_encode(10001, lang('live_not_exit'));
  72. }
  73. $goods_commonid = Db::name('live_apply_goods')->where('live_apply_id', $live_apply_id)->column('goods_commonid');
  74. $goods_model = model('goods');
  75. if (!empty($goods_commonid)) {
  76. $goods_list = $goods_model->getGoodsUnionList(array(array('goodscommon.goods_commonid', 'in', $goods_commonid)), 'goods_id,goods_storage,goodscommon.goods_commonid,goodscommon.goods_price,goodscommon.goods_name,goodscommon.goods_image', '', 'goodscommon.goods_commonid');
  77. foreach ($goods_list as $k => $v) {
  78. if (!$v['goods_storage']) {
  79. $goods_info = $goods_model->getGoodsStorageByCommonId($v['goods_commonid']);
  80. if ($goods_info) {
  81. $goods_list[$k]['goods_id'] = $goods_info['goods_id'];
  82. }
  83. }
  84. $goods_list[$k]['goods_image'] = goods_cthumb($v['goods_image']);
  85. }
  86. $live_apply['goods_list'] = $goods_list;
  87. }
  88. //判断当前流状态
  89. if (config('ds_config.video_type') == 'aliyun') {
  90. if (!config('ds_config.aliyun_live_push_domain')) {
  91. ds_json_encode(10001, lang('aliyun_live_push_domain_empty'), array('live_apply_info' => $live_apply));
  92. }
  93. if (!config('ds_config.aliyun_live_push_key')) {
  94. ds_json_encode(10001, lang('aliyun_live_push_key_empty'), array('live_apply_info' => $live_apply));
  95. }
  96. if (!config('ds_config.aliyun_live_play_domain')) {
  97. ds_json_encode(10001, lang('aliyun_live_play_domain_empty'), array('live_apply_info' => $live_apply));
  98. }
  99. if (!config('ds_config.aliyun_live_play_key')) {
  100. ds_json_encode(10001, lang('aliyun_live_play_key_empty'), array('live_apply_info' => $live_apply));
  101. }
  102. $regionId = 'cn-shanghai';
  103. AlibabaCloud::accessKeyClient(config('ds_config.aliyun_access_key_id'), config('ds_config.aliyun_access_key_secret'))
  104. ->regionId($regionId)
  105. ->asDefaultClient();
  106. try {
  107. $result = AlibabaCloud::rpc()
  108. ->product('live')
  109. // ->scheme('https') // https | http
  110. ->version('2016-11-01')
  111. ->action('DescribeLiveStreamsOnlineList')
  112. ->method('POST')
  113. ->host('live.aliyuncs.com')
  114. ->options([
  115. 'query' => [
  116. 'RegionId' => $regionId,
  117. 'DomainName' => config('ds_config.aliyun_live_push_domain'),
  118. 'AppName' => "live",
  119. 'StreamName' => 'live_apply_' . $live_apply['live_apply_id'],
  120. 'PageSize' => "1",
  121. 'PageNum' => "1",
  122. 'QueryType' => "strict",
  123. ],
  124. ])
  125. ->request();
  126. if (!$result->TotalNum) {
  127. ds_json_encode(10001, '主播不在线:' . ($live_apply['live_apply_push_message'] ? $live_apply['live_apply_push_message'] : ''), array('live_apply_info' => $live_apply));
  128. }
  129. } catch (\Exception $e) {
  130. ds_json_encode(10001, $e->getMessage(), array('live_apply_info' => $live_apply));
  131. }
  132. } else {
  133. if (!config('ds_config.live_push_domain')) {
  134. ds_json_encode(10001, lang('live_push_domain_empty'), array('live_apply_info' => $live_apply));
  135. }
  136. if (!config('ds_config.live_push_key')) {
  137. ds_json_encode(10001, lang('live_push_key_empty'), array('live_apply_info' => $live_apply));
  138. }
  139. if (!config('ds_config.live_play_domain')) {
  140. ds_json_encode(10001, lang('live_play_domain_empty'), array('live_apply_info' => $live_apply));
  141. }
  142. try {
  143. $cred = new Credential(config('ds_config.vod_tencent_secret_id'), config('ds_config.vod_tencent_secret_key'));
  144. $httpProfile = new HttpProfile();
  145. $httpProfile->setEndpoint("live.tencentcloudapi.com");
  146. $clientProfile = new ClientProfile();
  147. $clientProfile->setHttpProfile($httpProfile);
  148. $client = new LiveClient($cred, "", $clientProfile);
  149. $req = new DescribeLiveStreamStateRequest();
  150. $params = '{"AppName":"live","DomainName":"' . config('ds_config.live_push_domain') . '","StreamName":"' . 'live_apply_' . $live_apply['live_apply_id'] . '"}';
  151. $req->fromJsonString($params);
  152. $resp = $client->DescribeLiveStreamState($req);
  153. } catch (TencentCloudSDKException $e) {
  154. ds_json_encode(10001, $e->getMessage(), array('live_apply_info' => $live_apply));
  155. }
  156. if ($resp->StreamState != 'active') {
  157. ds_json_encode(10001, lang('live_not_online') . ($live_apply['live_apply_push_message'] ? $live_apply['live_apply_push_message'] : ''), array('live_apply_info' => $live_apply));
  158. }
  159. }
  160. //生成推流url
  161. $live_apply['live_apply_push_url'] = model('live_apply')->getPushUrl('live_apply_' . $live_apply['live_apply_id'], $live_apply['live_apply_end_time']);
  162. //生成拉流url
  163. $live_apply['live_apply_play_url'] = model('live_apply')->getPlayUrl('live_apply_' . $live_apply['live_apply_id'], $live_apply['live_apply_end_time']);
  164. $live_apply['live_apply_play_rtmp_url'] = model('live_apply')->getPlayUrl('live_apply_' . $live_apply['live_apply_id'], $live_apply['live_apply_end_time'], true);
  165. if ($live_apply['live_apply_play_time'] > TIMESTAMP) {
  166. ds_json_encode(10001, lang('live_not_begin'), array('live_apply_info' => $live_apply));
  167. }
  168. $extral_info = array();
  169. $live_apply['live_apply_cover_image_url'] = ds_get_pic(ATTACH_COMMON, config('ds_config.default_goods_image'));
  170. if ($live_apply['live_apply_cover_video']) {
  171. $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']);
  172. } elseif ($live_apply['live_apply_cover_image']) {
  173. $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']);
  174. }
  175. switch ($live_apply['live_apply_user_type']) {
  176. case 2:
  177. $store_model = model('store');
  178. $store_info = $store_model->getOneStore(array(array('store_id', '=', $live_apply['live_apply_user_id'])), 'store_id,store_collect,store_name,store_avatar');
  179. if (!$store_info) {
  180. ds_json_encode(10001, lang('ds_store_is_not_exist'), array('live_apply_info' => $live_apply));
  181. }
  182. $live_apply['live_apply_user_avatar'] = get_store_logo($store_info['store_avatar']);
  183. $live_apply['live_apply_user_name'] = $store_info['store_name'];
  184. $live_apply['live_apply_fans'] = $store_info['store_collect'];
  185. $favorites_model = model('favorites');
  186. $c = (int) $favorites_model->getStoreFavoritesCountByStoreId($store_info['store_id'], $this->member_info['member_id']);
  187. $live_apply['is_favorate'] = $c > 0;
  188. break;
  189. }
  190. $online_info = false;
  191. if (config('ds_config.instant_message_register_url')) {
  192. // 设置GatewayWorker服务的Register服务ip和端口,请根据实际情况改成实际值(ip不能是0.0.0.0)
  193. try {
  194. Gateway::$registerAddress = config('ds_config.instant_message_register_url');
  195. $online_info = array(
  196. 'online_count' => Gateway::getClientIdCountByGroup('live_apply_' . $live_apply_id),
  197. 'online_list' => Gateway::getClientSessionsByGroup('live_apply_' . $live_apply_id),
  198. );
  199. } catch (\Exception $e) {
  200. $msg = $e->getMessage();
  201. }
  202. }
  203. 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));
  204. }
  205. /**
  206. * @api {POST} api/MemberLive/get_live_info 消息列表
  207. * @apiVersion 1.0.0
  208. * @apiGroup MemberLive
  209. *
  210. * @apiHeader {String} X-DS-KEY 用户授权token
  211. *
  212. * @apiParam {Int} live_apply_id 直播id
  213. * @apiParam {Int} client_id 客户端id
  214. *
  215. * @apiSuccess {String} code 返回码,10000为成功
  216. * @apiSuccess {String} message 返回消息
  217. * @apiSuccess {Object} result 返回数据
  218. */
  219. function join_live()
  220. {
  221. session('name');
  222. $live_apply_id = input('param.live_apply_id');
  223. $client_id = input('param.client_id');
  224. if (!config('ds_config.instant_message_register_url')) {
  225. ds_json_encode(10001, lang('instant_message_register_url_empty'));
  226. }
  227. $live_apply_model = model('live_apply');
  228. $condition = array();
  229. $condition[] = array('live_apply_state', '=', 1);
  230. $condition[] = array('live_apply_end_time', '>', TIMESTAMP);
  231. $condition[] = array('live_apply_id', '=', $live_apply_id);
  232. $live_apply = $live_apply_model->getLiveApplyInfo($condition);
  233. if (empty($live_apply)) {
  234. ds_json_encode(10001, lang('live_not_exit'));
  235. }
  236. // 设置GatewayWorker服务的Register服务ip和端口,请根据实际情况改成实际值(ip不能是0.0.0.0)
  237. try {
  238. Gateway::$registerAddress = config('ds_config.instant_message_register_url');
  239. // client_id与uid绑定
  240. Gateway::bindUid($client_id, '0:' . $this->member_info['member_id']);
  241. $online_item = array(
  242. 'instant_message_from_avatar' => get_member_avatar_for_id($this->member_info['member_id']),
  243. 'instant_message_from_id' => $this->member_info['member_id'],
  244. 'instant_message_from_type' => 0,
  245. 'instant_message_from_name' => $this->member_info['member_name']
  246. );
  247. Gateway::setSession($client_id, $online_item);
  248. // 加入某个群组(可调用多次加入多个群组)
  249. Gateway::joinGroup($client_id, 'live_apply_' . $live_apply_id);
  250. //更新在线人数
  251. Gateway::sendToGroup('live_apply_' . $live_apply_id, json_encode(array(
  252. 'type' => 'join',
  253. 'online_count' => Gateway::getClientIdCountByGroup('live_apply_' . $live_apply_id),
  254. 'online_list' => Gateway::getClientSessionsByGroup('live_apply_' . $live_apply_id)
  255. )));
  256. } catch (\Exception $e) {
  257. ds_json_encode(10001, $e->getMessage());
  258. }
  259. Db::name('live_apply')->where('live_apply_id', $live_apply_id)->inc('live_apply_view_count')->update();
  260. ds_json_encode(10000, '');
  261. }
  262. /**
  263. * @api {POST} api/MemberLive/get_live_info 消息列表
  264. * @apiVersion 1.0.0
  265. * @apiGroup MemberLive
  266. *
  267. * @apiHeader {String} X-DS-KEY 用户授权token
  268. *
  269. * @apiParam {Int} live_apply_id 直播id
  270. *
  271. * @apiSuccess {String} code 返回码,10000为成功
  272. * @apiSuccess {String} message 返回消息
  273. * @apiSuccess {Object} result 返回数据
  274. */
  275. function add_like()
  276. {
  277. $live_apply_id = input('param.live_apply_id');
  278. $live_apply_model = model('live_apply');
  279. $condition = array();
  280. $condition[] = array('live_apply_state', '=', 1);
  281. $condition[] = array('live_apply_end_time', '>', TIMESTAMP);
  282. $condition[] = array('live_apply_id', '=', $live_apply_id);
  283. $live_apply = $live_apply_model->getLiveApplyInfo($condition);
  284. if (empty($live_apply)) {
  285. ds_json_encode(10001, lang('live_not_exit'));
  286. }
  287. Db::name('live_apply')->where('live_apply_id', $live_apply_id)->inc('live_apply_like_count')->update();
  288. ds_json_encode(10000, '');
  289. }
  290. /**
  291. * @api {POST} api/MemberLive/get_live_info 消息列表
  292. * @apiVersion 1.0.0
  293. * @apiGroup MemberLive
  294. *
  295. * @apiHeader {String} X-DS-KEY 用户授权token
  296. *
  297. * @apiParam {Int} live_apply_id 直播id
  298. *
  299. * @apiSuccess {String} code 返回码,10000为成功
  300. * @apiSuccess {String} message 返回消息
  301. * @apiSuccess {Object} result 返回数据
  302. */
  303. function add_gift()
  304. {
  305. $live_apply_id = input('param.live_apply_id');
  306. if (!config('ds_config.instant_message_register_url')) {
  307. ds_json_encode(10001, lang('instant_message_register_url_empty'));
  308. }
  309. $live_apply_model = model('live_apply');
  310. $condition = array();
  311. $condition[] = array('live_apply_state', '=', 1);
  312. $condition[] = array('live_apply_end_time', '>', TIMESTAMP);
  313. $condition[] = array('live_apply_id', '=', $live_apply_id);
  314. $live_apply = $live_apply_model->getLiveApplyInfo($condition);
  315. if (empty($live_apply)) {
  316. ds_json_encode(10001, lang('live_not_exit'));
  317. }
  318. if ($this->member_info['member_points'] < 100) {
  319. ds_json_encode(10001, lang('points_not_enough'));
  320. }
  321. $points_model = model('points');
  322. Db::startTrans();
  323. try {
  324. switch ($live_apply['live_apply_user_type']) {
  325. case 2:
  326. $store_model = model('store');
  327. $store_info = $store_model->getOneStore(array(array('store_id', '=', $live_apply['live_apply_user_id'])), 'member_id,member_name');
  328. if (!$store_info) {
  329. throw new \think\Exception(lang('ds_store_is_not_exist'), 10006);
  330. }
  331. $points_model->savePointslog('gift', array(
  332. 'pl_memberid' => $store_info['member_id'], 'pl_membername' => $store_info['member_name'], 'pl_desc' => $live_apply['live_apply_name'] . lang('live_present_rocket'), 'pl_points' => 100
  333. ));
  334. $points_model->savePointslog('gift', array(
  335. 'pl_memberid' => $this->member_info['member_id'], 'pl_membername' => $this->member_info['member_name'], 'pl_desc' => $live_apply['live_apply_name'] . lang('live_present_rocket'), 'pl_points' => -100
  336. ));
  337. break;
  338. }
  339. } catch (\Exception $e) {
  340. Db::rollback();
  341. ds_json_encode(10001, $e->getMessage());
  342. }
  343. Db::commit();
  344. // 设置GatewayWorker服务的Register服务ip和端口,请根据实际情况改成实际值(ip不能是0.0.0.0)
  345. try {
  346. Gateway::$registerAddress = config('ds_config.instant_message_register_url');
  347. Gateway::sendToGroup('live_apply_' . $live_apply_id, json_encode(array(
  348. 'type' => 'gift',
  349. 'gift_num' => 1,
  350. 'member' => array(
  351. 'member_id' => $this->member_info['member_id'],
  352. 'member_name' => $this->member_info['member_name'],
  353. 'member_avatar' => get_member_avatar_for_id($this->member_info['member_id'])
  354. ),
  355. 'online_count' => Gateway::getClientIdCountByGroup('live_apply_' . $live_apply_id),
  356. 'online_list' => Gateway::getClientSessionsByGroup('live_apply_' . $live_apply_id)
  357. )));
  358. } catch (\Exception $e) {
  359. ds_json_encode(10001, $e->getMessage());
  360. }
  361. ds_json_encode(10000, '');
  362. }
  363. /**
  364. * @api {POST} api/MemberLive/get_live_info 消息列表
  365. * @apiVersion 1.0.0
  366. * @apiGroup MemberLive
  367. *
  368. * @apiHeader {String} X-DS-KEY 用户授权token
  369. *
  370. * @apiParam {Int} live_apply_id 直播id
  371. * @apiParam {Int} client_id 客户端id
  372. *
  373. * @apiSuccess {String} code 返回码,10000为成功
  374. * @apiSuccess {String} message 返回消息
  375. * @apiSuccess {Object} result 返回数据
  376. */
  377. function leave_live()
  378. {
  379. $live_apply_id = input('param.live_apply_id');
  380. $client_id = input('param.client_id');
  381. if (!config('ds_config.instant_message_register_url')) {
  382. ds_json_encode(10001, lang('instant_message_register_url_empty'));
  383. }
  384. try {
  385. // 设置GatewayWorker服务的Register服务ip和端口,请根据实际情况改成实际值(ip不能是0.0.0.0)
  386. Gateway::$registerAddress = config('ds_config.instant_message_register_url');
  387. // client_id与uid绑定
  388. Gateway::unbindUid($client_id, '0:' . $this->member_info['member_id']);
  389. // 加入某个群组(可调用多次加入多个群组)
  390. Gateway::leaveGroup($client_id, 'live_apply_' . $live_apply_id);
  391. //更新在线人数
  392. Gateway::sendToGroup('live_apply_' . $live_apply_id, json_encode(array(
  393. 'type' => 'leave',
  394. 'online_count' => Gateway::getClientIdCountByGroup('live_apply_' . $live_apply_id),
  395. 'online_list' => Gateway::getClientSessionsByGroup('live_apply_' . $live_apply_id),
  396. )));
  397. } catch (\Exception $e) {
  398. ds_json_encode(10001, $e->getMessage());
  399. }
  400. ds_json_encode(10000, '');
  401. }
  402. }