MemberLive.php 18 KB

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