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