MemberLive.php 22 KB

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