MemberLive.php 21 KB

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