MemberLive.php 19 KB

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