MemberLive.php 20 KB

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