MemberLive.php 18 KB

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