MemberLive.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. <?php
  2. namespace app\home\controller;
  3. use think\facade\View;
  4. use think\facade\Lang;
  5. use think\facade\Db;
  6. use GatewayClient\Gateway;
  7. use TencentCloud\Common\Credential;
  8. use TencentCloud\Common\Profile\ClientProfile;
  9. use TencentCloud\Common\Profile\HttpProfile;
  10. use TencentCloud\Common\Exception\TencentCloudSDKException;
  11. use TencentCloud\Live\V20180801\LiveClient;
  12. use TencentCloud\Live\V20180801\Models\DescribeLiveStreamStateRequest;
  13. use \Firebase\JWT\JWT;
  14. use AlibabaCloud\Client\AlibabaCloud;
  15. /**
  16. * ============================================================================
  17. *
  18. * ============================================================================
  19. *
  20. * ----------------------------------------------------------------------------
  21. *
  22. * ============================================================================
  23. * 控制器
  24. */
  25. class MemberLive extends BaseMember
  26. {
  27. public function initialize()
  28. {
  29. parent::initialize(); // TODO: Change the autogenerated stub
  30. Lang::load(base_path() . 'home/lang/' . config('lang.default_lang') . '/live.lang.php');
  31. }
  32. /**
  33. * 直播间
  34. *
  35. * @param
  36. * @return
  37. */
  38. public function index()
  39. {
  40. $live_apply_id = input('param.live_apply_id');
  41. if (!$live_apply_id) {
  42. $this->error(lang('param_error'));
  43. }
  44. //获取最近未结束的直播
  45. $live_apply_model = model('live_apply');
  46. $condition = array();
  47. $condition[] = array('live_apply_state', '=', 1);
  48. $condition[] = array('live_apply_id', '=', $live_apply_id);
  49. $live_apply = $live_apply_model->getLiveApplyInfo($condition);
  50. if (!$live_apply) {
  51. $this->error(lang('live_not_exit'));
  52. }
  53. if ($live_apply['live_apply_play_time'] > TIMESTAMP) {
  54. $this->error(lang('live_not_begin'));
  55. }
  56. $extral_info = array();
  57. $live_apply['live_apply_cover_image_url'] = ds_get_pic(ATTACH_COMMON, config('ds_config.default_goods_image'));
  58. if ($live_apply['live_apply_cover_video']) {
  59. $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']);
  60. } elseif ($live_apply['live_apply_cover_image']) {
  61. $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']);
  62. }
  63. switch ($live_apply['live_apply_user_type']) {
  64. case 2:
  65. $store_model = model('store');
  66. $store_info = $store_model->getOneStore(array(array('store_id', '=', $live_apply['live_apply_user_id'])), 'store_id,store_collect,store_name,store_avatar,area_info');
  67. if (!$store_info) {
  68. $this->error(lang('ds_store_is_not_exist'));
  69. }
  70. $live_apply['live_apply_user_avatar'] = get_store_logo($store_info['store_avatar']);
  71. $live_apply['live_apply_user_name'] = $store_info['store_name'];
  72. $live_apply['live_apply_fans'] = $store_info['store_collect'];
  73. $live_apply['area_info'] = $store_info['area_info'];
  74. $favorites_model = model('favorites');
  75. $c = (int) $favorites_model->getStoreFavoritesCountByStoreId($store_info['store_id'], $this->member_info['member_id']);
  76. $live_apply['is_favorate'] = $c > 0;
  77. break;
  78. }
  79. $online_info = false;
  80. if (config('ds_config.instant_message_register_url')) {
  81. // 设置GatewayWorker服务的Register服务ip和端口,请根据实际情况改成实际值(ip不能是0.0.0.0)
  82. try {
  83. Gateway::$registerAddress = config('ds_config.instant_message_register_url');
  84. $online_info = array(
  85. 'online_count' => Gateway::getClientIdCountByGroup('live_apply_' . $live_apply_id),
  86. 'online_list' => Gateway::getClientSessionsByGroup('live_apply_' . $live_apply_id),
  87. );
  88. } catch (\Exception $e) {
  89. $msg = $e->getMessage();
  90. }
  91. }
  92. $goods_commonid = Db::name('live_apply_goods')->where('live_apply_id', $live_apply_id)->column('goods_commonid');
  93. $goods_model = model('goods');
  94. if (!empty($goods_commonid)) {
  95. $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');
  96. foreach ($goods_list as $k => $v) {
  97. if (!$v['goods_storage']) {
  98. $goods_info = $goods_model->getGoodsStorageByCommonId($v['goods_commonid']);
  99. if ($goods_info) {
  100. $goods_list[$k]['goods_id'] = $goods_info['goods_id'];
  101. }
  102. }
  103. $goods_list[$k]['goods_image'] = goods_cthumb($v['goods_image']);
  104. }
  105. $live_apply['goods_list'] = $goods_list;
  106. }
  107. //通过链接获取腾讯fileId
  108. $live_apply['file_id'] = '';
  109. $live_apply['video_type'] = '';
  110. if ($live_apply['live_apply_video']) {
  111. $preg_str = '/\/\S{8}vodcq' . config('ds_config.vod_tencent_appid') . '\/\S{8}(\d{19})\//';
  112. if (preg_match($preg_str, $live_apply['live_apply_video'], $matches)) {
  113. if (config('ds_config.vod_tencent_appid') && config('ds_config.vod_tencent_play_key')) {
  114. $appId = config('ds_config.vod_tencent_appid'); // 用户 appid
  115. $fileId = $matches[1]; // 目标 FileId
  116. $currentTime = TIMESTAMP;
  117. $psignExpire = $currentTime + 3600; // 可任意设置过期时间,示例1h
  118. $urlTimeExpire = dechex($psignExpire); // 可任意设置过期时间,16进制字符串形式,示例1h
  119. $key = config('ds_config.vod_tencent_play_key');
  120. $payload = array(
  121. "appId" => $appId,
  122. "fileId" => $fileId,
  123. "currentTimeStamp" => $currentTime,
  124. "expireTimeStamp" => $psignExpire,
  125. "urlAccessInfo" => array(
  126. "t" => $urlTimeExpire
  127. )
  128. );
  129. $jwt = JWT::encode($payload, $key, 'HS256');
  130. $live_apply['video_type'] = 'tencent';
  131. $live_apply['file_id'] = $fileId;
  132. $live_apply['psign'] = $jwt;
  133. }
  134. } else if (preg_match('/aliyuncs/', $live_apply['live_apply_video'], $matches)) {
  135. $live_apply['video_type'] = 'aliyun';
  136. }
  137. }
  138. View::assign('online_info', $online_info);
  139. View::assign('member_avatar', get_member_avatar_for_id($this->member_info['member_id']));
  140. View::assign('member_points', $this->member_info['member_points']);
  141. $active = false;
  142. if ($live_apply['live_apply_end_time'] > TIMESTAMP) {
  143. //判断当前流状态
  144. if (config('ds_config.video_type') == 'aliyun') {
  145. $live_apply['video_type'] = 'aliyun';
  146. if (!config('ds_config.aliyun_live_push_domain')) {
  147. $this->error(lang('aliyun_live_push_domain_empty'));
  148. }
  149. if (!config('ds_config.aliyun_live_push_key')) {
  150. $this->error(lang('aliyun_live_push_key_empty'));
  151. }
  152. if (!config('ds_config.aliyun_live_play_domain')) {
  153. $this->error(lang('aliyun_live_play_domain_empty'));
  154. }
  155. if (!config('ds_config.aliyun_live_play_key')) {
  156. $this->error(lang('aliyun_live_play_key_empty'));
  157. }
  158. $regionId = 'cn-shanghai';
  159. AlibabaCloud::accessKeyClient(config('ds_config.aliyun_access_key_id'), config('ds_config.aliyun_access_key_secret'))
  160. ->regionId($regionId)
  161. ->asDefaultClient();
  162. try {
  163. $result = AlibabaCloud::rpc()
  164. ->product('live')
  165. // ->scheme('https') // https | http
  166. ->version('2016-11-01')
  167. ->action('DescribeLiveStreamsOnlineList')
  168. ->method('POST')
  169. ->host('live.aliyuncs.com')
  170. ->options([
  171. 'query' => [
  172. 'RegionId' => $regionId,
  173. 'DomainName' => config('ds_config.aliyun_live_push_domain'),
  174. 'AppName' => "live",
  175. 'StreamName' => 'live_apply_' . $live_apply['live_apply_id'],
  176. 'PageSize' => "1",
  177. 'PageNum' => "1",
  178. 'QueryType' => "strict",
  179. ],
  180. ])
  181. ->request();
  182. if ($result->TotalNum) {
  183. $active = true;
  184. }
  185. } catch (\Exception $e) {
  186. $this->error($e->getMessage());
  187. }
  188. } else {
  189. $live_apply['video_type'] = 'tencent';
  190. if (!config('ds_config.live_push_domain')) {
  191. $this->error(lang('live_push_domain_empty'));
  192. }
  193. if (!config('ds_config.live_push_key')) {
  194. $this->error(lang('live_push_key_empty'));
  195. }
  196. if (!config('ds_config.live_play_domain')) {
  197. $this->error(lang('live_play_domain_empty'));
  198. }
  199. try {
  200. $cred = new Credential(config('ds_config.vod_tencent_secret_id'), config('ds_config.vod_tencent_secret_key'));
  201. $httpProfile = new HttpProfile();
  202. $httpProfile->setEndpoint("live.tencentcloudapi.com");
  203. $clientProfile = new ClientProfile();
  204. $clientProfile->setHttpProfile($httpProfile);
  205. $client = new LiveClient($cred, "", $clientProfile);
  206. $req = new DescribeLiveStreamStateRequest();
  207. $params = '{"AppName":"live","DomainName":"' . config('ds_config.live_push_domain') . '","StreamName":"' . 'live_apply_' . $live_apply['live_apply_id'] . '"}';
  208. $req->fromJsonString($params);
  209. $resp = $client->DescribeLiveStreamState($req);
  210. } catch (TencentCloudSDKException $e) {
  211. $this->error($e->getMessage());
  212. }
  213. if ($resp->StreamState == 'active') {
  214. $active = true;
  215. }
  216. }
  217. //生成推流url
  218. $live_apply['live_apply_push_url'] = $live_apply_model->getPushUrl('live_apply_' . $live_apply['live_apply_id'], $live_apply['live_apply_end_time']);
  219. //生成拉流url
  220. $live_apply['live_apply_play_url'] = $live_apply_model->getPlayUrl('live_apply_' . $live_apply['live_apply_id'], $live_apply['live_apply_end_time']);
  221. }
  222. View::assign('live_apply_info', array_merge($live_apply, $extral_info, array('instant_message_url' => config('ds_config.instant_message_gateway_url'))));
  223. View::assign('active', $active);
  224. return View::fetch($this->template_dir . 'index');
  225. }
  226. function join_live()
  227. {
  228. session('name');
  229. $live_apply_id = input('param.live_apply_id');
  230. $client_id = input('param.client_id');
  231. if (!config('ds_config.instant_message_register_url')) {
  232. ds_json_encode(10001, lang('instant_message_register_url_empty'));
  233. }
  234. $live_apply_model = model('live_apply');
  235. $condition = array();
  236. $condition[] = array('live_apply_state', '=', 1);
  237. $condition[] = array('live_apply_end_time', '>', TIMESTAMP);
  238. $condition[] = array('live_apply_id', '=', $live_apply_id);
  239. $live_apply = $live_apply_model->getLiveApplyInfo($condition);
  240. if (empty($live_apply)) {
  241. ds_json_encode(10001, lang('live_not_exit'));
  242. }
  243. // 设置GatewayWorker服务的Register服务ip和端口,请根据实际情况改成实际值(ip不能是0.0.0.0)
  244. try {
  245. Gateway::$registerAddress = config('ds_config.instant_message_register_url');
  246. // client_id与uid绑定
  247. Gateway::bindUid($client_id, '0:' . $this->member_info['member_id']);
  248. $online_item = array(
  249. 'instant_message_from_avatar' => get_member_avatar_for_id($this->member_info['member_id']),
  250. 'instant_message_from_id' => $this->member_info['member_id'],
  251. 'instant_message_from_type' => 0,
  252. 'instant_message_from_name' => $this->member_info['member_name']
  253. );
  254. Gateway::setSession($client_id, $online_item);
  255. // 加入某个群组(可调用多次加入多个群组)
  256. Gateway::joinGroup($client_id, 'live_apply_' . $live_apply_id);
  257. //更新在线人数
  258. Gateway::sendToGroup('live_apply_' . $live_apply_id, json_encode(array(
  259. 'type' => 'join',
  260. 'online_count' => Gateway::getClientIdCountByGroup('live_apply_' . $live_apply_id),
  261. 'online_list' => Gateway::getClientSessionsByGroup('live_apply_' . $live_apply_id)
  262. )));
  263. } catch (\Exception $e) {
  264. ds_json_encode(10001, $e->getMessage());
  265. }
  266. Db::name('live_apply')->where('live_apply_id', $live_apply_id)->inc('live_apply_view_count')->update();
  267. ds_json_encode(10000, '');
  268. }
  269. function add_like()
  270. {
  271. $live_apply_id = input('param.live_apply_id');
  272. $live_apply_model = model('live_apply');
  273. $condition = array();
  274. $condition[] = array('live_apply_state', '=', 1);
  275. $condition[] = array('live_apply_end_time', '>', TIMESTAMP);
  276. $condition[] = array('live_apply_id', '=', $live_apply_id);
  277. $live_apply = $live_apply_model->getLiveApplyInfo($condition);
  278. if (empty($live_apply)) {
  279. ds_json_encode(10001, lang('live_not_exit'));
  280. }
  281. Db::name('live_apply')->where('live_apply_id', $live_apply_id)->inc('live_apply_like_count')->update();
  282. ds_json_encode(10000, '');
  283. }
  284. function add_gift()
  285. {
  286. $live_apply_id = input('param.live_apply_id');
  287. $num = intval(input('param.num'));
  288. if (!config('ds_config.instant_message_register_url')) {
  289. ds_json_encode(10001, lang('instant_message_register_url_empty'));
  290. }
  291. if ($num < 1) {
  292. ds_json_encode(10001, lang('num_error'));
  293. }
  294. $live_apply_model = model('live_apply');
  295. $condition = array();
  296. $condition[] = array('live_apply_state', '=', 1);
  297. $condition[] = array('live_apply_end_time', '>', TIMESTAMP);
  298. $condition[] = array('live_apply_id', '=', $live_apply_id);
  299. $live_apply = $live_apply_model->getLiveApplyInfo($condition);
  300. if (empty($live_apply)) {
  301. ds_json_encode(10001, lang('live_not_exit'));
  302. }
  303. if ($this->member_info['member_points'] < 100) {
  304. ds_json_encode(10001, lang('points_not_enough'));
  305. }
  306. $points_model = model('points');
  307. Db::startTrans();
  308. try {
  309. switch ($live_apply['live_apply_user_type']) {
  310. case 2:
  311. $store_model = model('store');
  312. $store_info = $store_model->getOneStore(array(array('store_id', '=', $live_apply['live_apply_user_id'])), 'member_id,member_name');
  313. if (!$store_info) {
  314. throw new \think\Exception(lang('ds_store_is_not_exist'), 10006);
  315. }
  316. $points_model->savePointslog('gift', array(
  317. '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 * $num
  318. ));
  319. $points_model->savePointslog('gift', array(
  320. '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 * $num
  321. ));
  322. break;
  323. }
  324. } catch (\Exception $e) {
  325. Db::rollback();
  326. ds_json_encode(10001, $e->getMessage());
  327. }
  328. Db::commit();
  329. // 设置GatewayWorker服务的Register服务ip和端口,请根据实际情况改成实际值(ip不能是0.0.0.0)
  330. try {
  331. Gateway::$registerAddress = config('ds_config.instant_message_register_url');
  332. Gateway::sendToGroup('live_apply_' . $live_apply_id, json_encode(array(
  333. 'type' => 'gift',
  334. 'gift_num' => $num,
  335. 'member' => array(
  336. 'member_id' => $this->member_info['member_id'],
  337. 'member_name' => $this->member_info['member_name'],
  338. 'member_avatar' => get_member_avatar_for_id($this->member_info['member_id'])
  339. ),
  340. 'online_count' => Gateway::getClientIdCountByGroup('live_apply_' . $live_apply_id),
  341. 'online_list' => Gateway::getClientSessionsByGroup('live_apply_' . $live_apply_id)
  342. )));
  343. } catch (\Exception $e) {
  344. ds_json_encode(10001, $e->getMessage());
  345. }
  346. ds_json_encode(10000, '', array('member_points' => $this->member_info['member_points'] - 100 * $num));
  347. }
  348. }