Live.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\wap\controller;
  12. use Api\AliyunLive;
  13. use Api\AliyunLive as ApiAliyunLive;
  14. use app\admin\model\system\SystemGroupData;
  15. use app\wap\model\live\LiveBarrage;
  16. use app\wap\model\live\LiveGoods;
  17. use app\wap\model\live\LiveHonouredGuest;
  18. use app\wap\model\live\LiveReward;
  19. use app\wap\model\live\LiveStudio;
  20. use app\wap\model\live\LivePlayback;
  21. use app\wap\model\live\LiveUser;
  22. use app\wap\model\live\LiveGift;
  23. use app\wap\model\special\SpecialBuy;
  24. use app\wap\model\special\Special;
  25. use app\wap\model\special\SpecialTask;
  26. use app\wap\model\user\User;
  27. use app\wap\model\user\UserBill;
  28. use service\GroupDataService;
  29. use service\UtilService;
  30. use think\Config;
  31. use service\JsonService;
  32. use think\Cookie;
  33. use think\Db;
  34. use think\Session;
  35. use think\Url;
  36. use service\SystemConfigService;
  37. class Live extends AuthController
  38. {
  39. /*
  40. * 白名单
  41. * */
  42. public static function WhiteList()
  43. {
  44. return [
  45. 'get_live_record_list',
  46. ];
  47. }
  48. /**
  49. * 阿里云直播句柄
  50. * @var \Api\AliyunLive
  51. */
  52. protected $aliyunLive;
  53. protected function _initialize()
  54. {
  55. parent::_initialize();
  56. $this->aliyunLive = \Api\AliyunLive::instance([
  57. 'AccessKey' => SystemConfigService::get('accessKeyId'),
  58. 'AccessKeySecret' => SystemConfigService::get('accessKeySecret'),
  59. 'OssEndpoint' => SystemConfigService::get('aliyun_live_end_point'),
  60. 'OssBucket' => SystemConfigService::get('aliyun_live_oss_bucket'),
  61. 'appName' => SystemConfigService::get('aliyun_live_appName'),
  62. 'payKey' => SystemConfigService::get('aliyun_live_play_key'),
  63. 'key' => SystemConfigService::get('aliyun_live_push_key'),
  64. 'playLike' => SystemConfigService::get('aliyun_live_playLike'),
  65. 'rtmpLink' => SystemConfigService::get('aliyun_live_rtmpLink'),
  66. ]);
  67. }
  68. /**
  69. * 直播间主页
  70. * @param string $stream_name
  71. * @return mixed|void
  72. * @throws \think\Exception
  73. * @throws \think\db\exception\DataNotFoundException
  74. * @throws \think\db\exception\ModelNotFoundException
  75. * @throws \think\exception\DbException
  76. */
  77. public function index($stream_name = '', $special_id = 0, $live_id = 0, $record_id = 0)
  78. {
  79. if ($special_id && $special_id != 0 && $live_id && $live_id != 0) {
  80. $special_task = SpecialTask::where(['special_id' => $special_id, 'live_id' => $live_id])->value('live_id');
  81. if (!$special_task) {
  82. $this->failed('直播间不存在', Url::build('index/index'));
  83. }
  84. $stream_name = LiveStudio::where('id', $live_id)->value('stream_name');
  85. }
  86. if (!$stream_name) return $this->failed('缺少在直播间号!', Url::build('index/index'));
  87. $liveInfo = LiveStudio::where('stream_name', $stream_name)->find();
  88. if (!$liveInfo) return $this->failed('直播间不存在', Url::build('index/index'));
  89. if ($liveInfo->is_del) return $this->failed('直播间已被删除', Url::build('index/index'));
  90. $userInfo = LiveUser::setLiveUser($this->uid, $liveInfo->id);
  91. if ($userInfo === false) return $this->failed(LiveUser::getErrorInfo('用户写入不成功'), Url::build('index/index'));
  92. $specialLive =Special::where(['is_show' => 1, 'is_del' => 0, 'id' => $liveInfo->special_id])->find();
  93. if (!$specialLive) return $this->failed('专题不存在或者已被删除', Url::build('index/index'));
  94. $user_level = !$this->uid ? 0 : User::where('uid', $this->uid)->find();
  95. if ($specialLive->pay_type == 1 && !SpecialBuy::PaySpecial($specialLive->id, $this->uid)) {
  96. if($specialLive->member_pay_type == 1 || ($user_level['level'] <= 0 && $specialLive->member_pay_type == 0)){
  97. return $this->failed('您还没有支付请支付后再进行观看', Url::build('special/details', ['id' => $liveInfo->special_id]));
  98. }
  99. }
  100. if ($specialLive->pay_type == 2) {
  101. $cookie_value = Cookie::get($stream_name."studio_pwd");
  102. if (!$cookie_value || $cookie_value!=$liveInfo['studio_pwd']) {
  103. return $this->failed('您需要先获得密码后再进行观看', Url::build('special/details', ['id' => $liveInfo->special_id]));
  104. }
  105. }
  106. $AliyunLive = $this->aliyunLive;
  107. if ($liveInfo->is_play)
  108. $PullUrl = $AliyunLive->getPullSteam($liveInfo->stream_name);
  109. else {
  110. $record_id = $record_id ? $record_id : $liveInfo->playback_record_id;
  111. if ($liveInfo->is_playback && $record_id) {
  112. $livePlayBack = LivePlayback::where(['RecordId' => $record_id, 'stream_name' => $liveInfo->stream_name])->find();
  113. $PullUrl = $livePlayBack ? $livePlayBack->playback_url : false;
  114. } else
  115. $PullUrl = false;
  116. }
  117. $live_status = 0;
  118. $datatime = strtotime($liveInfo->start_play_time);
  119. $endTime = strtotime($liveInfo->stop_play_time);
  120. if ($datatime < time() && $endTime > time())
  121. $live_status = 1;//正在直播
  122. else if ($datatime < time() && $endTime < time())
  123. $live_status = 2;//直播结束
  124. else if ($datatime > time())
  125. $live_status = 0;//尚未直播
  126. $user_type = LiveHonouredGuest::where(['uid' => $this->uid, 'live_id' => $liveInfo->id])->value('type');
  127. if (is_null($user_type)) $user_type = 2;
  128. $uids = LiveHonouredGuest::where(['live_id' => $liveInfo->id])->column('uid');
  129. $liveInfo['abstract'] = $specialLive->abstract;
  130. $this->assign([
  131. 'goldInfo' => json_encode(SystemConfigService::more("gold_name,gold_rate,gold_image")),
  132. 'liveInfo' => json_encode($liveInfo),
  133. 'UserSum' => bcadd(LiveUser::where(['live_id' => $liveInfo->id, 'is_open_ben' => 0, 'is_online' => 1])->sum('visit_num'), $liveInfo->online_num, 0),
  134. 'live_title' => $liveInfo->live_title,
  135. 'PullUrl' => $PullUrl,
  136. //'requirejs' => true,
  137. 'is_ban' => $userInfo->is_ban,
  138. 'room' => $liveInfo->id,
  139. 'datatime' => $datatime,
  140. 'workerman' => json_encode(Config::get('workerman.chat', [])),
  141. 'phone_type' => UtilService::getDeviceType(),
  142. 'live_status' => $live_status,
  143. 'user_type' => $user_type,
  144. 'OpenCommentCount' => LiveBarrage::where(['live_id' => $liveInfo->id, 'is_show' => 1])->count(),
  145. 'OpenCommentTime' => LiveBarrage::where(['live_id' => $liveInfo->id, 'is_show' => 1])->order('add_time asc')->value('add_time'),
  146. 'CommentCount' => LiveBarrage::where(['live_id' => $liveInfo->id, 'is_show' => 1])->where('uid', 'in', $uids)->count(),
  147. 'CommentTime' => LiveBarrage::where(['live_id' => $liveInfo->id, 'is_show' => 1])->where('uid', 'in', $uids)->order('add_time asc')->value('add_time'),
  148. ]);
  149. return $this->fetch();
  150. }
  151. /**
  152. * 获取助教评论
  153. */
  154. public function get_comment_list()
  155. {
  156. list($page, $limit, $live_id, $add_time) = UtilService::getMore([
  157. ['page', 0],
  158. ['limit', 20],
  159. ['live_id', 0],
  160. ['add_time', 0],
  161. ], $this->request, true);
  162. if (!$live_id) return JsonService::fail('缺少参数!');
  163. $uids = LiveHonouredGuest::where(['live_id' => $live_id])->column('uid');
  164. if (!$uids) {
  165. $ystemConfig = \service\SystemConfigService::more(['site_name', 'site_logo']);
  166. $data = [
  167. 'nickname' => $ystemConfig['site_name'],
  168. 'avatar' => $ystemConfig['site_logo'],
  169. 'user_type' => 2,
  170. 'content' => LiveStudio::where('id', $live_id)->value('auto_phrase'),
  171. 'id' => 0,
  172. 'type' => 1,
  173. 'uid' => 0
  174. ];
  175. return JsonService::successful(['list' => [$data], 'page' => 0]);
  176. }
  177. return JsonService::successful(LiveBarrage::getCommentList($uids, $live_id, $page, $limit, $add_time));
  178. }
  179. /**
  180. * 获取助教,讲师,普通人的评论
  181. */
  182. public function get_open_comment_list()
  183. {
  184. list($page, $limit, $live_id, $add_time) = UtilService::getMore([
  185. ['page', 0],
  186. ['limit', 20],
  187. ['live_id', 0],
  188. ['add_time', 0],
  189. ], $this->request, true);
  190. if (!$live_id) return JsonService::fail('缺少参数!');
  191. return JsonService::successful(LiveBarrage::getCommentList(false, $live_id, $page, $limit, $add_time));
  192. }
  193. /**
  194. * 获取直播间下的录制内容
  195. * @param string $record_id
  196. * @param string $stream_name
  197. * @param string $start_time
  198. * @param string $end_time
  199. * @param int $page
  200. * @param int $limit
  201. */
  202. public function get_live_record_list($special_id = '', $start_time = '', $end_time = '', $page = 1, $limit = 10)
  203. {
  204. if (!$special_id) return JsonService::fail('参数缺失');
  205. $specialInfo = Special::get($special_id);
  206. if (!$specialInfo) return JsonService::fail('直播专题不存在');
  207. $liveStudio = LiveStudio::where(['special_id' => $specialInfo['id']])->find();
  208. if (!$liveStudio) return JsonService::fail('缺少直播间');
  209. if (!$liveStudio['stream_name']) return JsonService::fail('缺少直播间id');
  210. $data = [];
  211. $count = 0;
  212. $data = compact('data','count');
  213. if ($liveStudio['is_playback'] == 1) {
  214. $where['stream_name']=$liveStudio['stream_name'];
  215. $where['page']=$page;
  216. $where['limit']=$limit;
  217. $where['start_time']=$start_time;
  218. $where['end_time']=$end_time;
  219. $data= LivePlayback::getLivePlaybackList($where);
  220. }
  221. $data['page'] = $page++;
  222. return JsonService::successful($data);
  223. }
  224. /**
  225. * 获取直播间下的录制内容
  226. * @param string $record_id
  227. * @param string $stream_name
  228. * @param string $start_time
  229. * @param string $end_time
  230. * @param int $page
  231. * @param int $limit
  232. */
  233. public function get_live_record_list1($special_id = '', $start_time = '', $end_time = '', $page = 1, $limit = 10)
  234. {
  235. if (!$special_id) return JsonService::fail('参数缺失');
  236. $specialInfo = Special::get($special_id);
  237. if (!$specialInfo) return JsonService::fail('直播专题不存在');
  238. $liveStudio = LiveStudio::where(['special_id' => $specialInfo['id']])->find();
  239. if (!$liveStudio) return JsonService::fail('缺少直播间');
  240. if (!$liveStudio['stream_name']) return JsonService::fail('缺少直播间id');
  241. $aliyunLive = $this->aliyunLive;
  242. $beginToday = mktime(0, 0, 0, date('m'), date('d') - 3, date('Y'));
  243. if ($start_time && $end_time) {
  244. $start_time = strtotime($start_time);
  245. $end_time = strtotime($end_time);
  246. if ($start_time > $end_time) return JsonService::fail('开始时间不能大于结束时间');
  247. $time = bcsub($end_time, $start_time, 0) / 86400;
  248. if ($time > 4) return JsonService::fail('开始和结束的时间不能间隔4天');
  249. }
  250. $res = $aliyunLive->queryLiveRecordFiles(
  251. $liveStudio['stream_name'],
  252. $start_time ? ApiAliyunLive::setTimeFormat($start_time) : ApiAliyunLive::setTimeFormat($beginToday),
  253. $end_time ? ApiAliyunLive::setTimeFormat($end_time) : ApiAliyunLive::setTimeFormat(time()),
  254. $page,
  255. $limit
  256. );
  257. $data = [];
  258. $count = 0;
  259. if ($res) {
  260. if (isset($res['RecordIndexInfoList']['RecordIndexInfo'])) {
  261. foreach ($res['RecordIndexInfoList']['RecordIndexInfo'] as $item) {
  262. $data['list'][] = [
  263. 'StreamName' => $item['StreamName'],
  264. 'RecordId' => $item['RecordId'],
  265. 'playback_record_id' => $liveStudio['playback_record_id'],
  266. 'RecordUrl' => $item['RecordUrl'],
  267. 'StartTime' => $item['StartTime'],
  268. 'EndTime' => $item['EndTime'],
  269. ];
  270. }
  271. }
  272. if (isset($res['TotalNum'])) $count = $res['TotalNum'];
  273. $data['page'] = $page++;
  274. $data['count'] = $count;
  275. return JsonService::successful($data);
  276. } else {
  277. return JsonService::fail("网络错误");
  278. }
  279. }
  280. /**
  281. * 打赏礼物列表
  282. */
  283. public function live_gift_list()
  284. {
  285. $live_gift = LiveGift::liveGiftList();
  286. return JsonService::successful($live_gift);
  287. }
  288. /**打赏接口
  289. * @throws \think\Exception
  290. * @throws \think\db\exception\DataNotFoundException
  291. * @throws \think\db\exception\ModelNotFoundException
  292. * @throws \think\exception\DbException
  293. */
  294. public function live_reward()
  295. {
  296. list($uid, $live_gift_id, $live_gift_num, $stream_name, $special_id) = UtilService::postMore([
  297. ['uid', 0],
  298. ['live_gift_id', 0],
  299. ['live_gift_num', 0],
  300. ['stream_name', 0],
  301. ['special_id', 0],
  302. ], $this->request, true);
  303. if (!$uid) return JsonService::fail('用户id缺失');
  304. if (!$live_gift_id || !is_numeric($live_gift_id)) return JsonService::fail('礼物id缺失');
  305. if (!$stream_name || !is_numeric($stream_name)) return JsonService::fail('直播间号缺失');
  306. if (!$special_id || !is_numeric($special_id)) return JsonService::fail('直播专题ID缺失');
  307. $user_info = $this->userInfo;
  308. if ($uid != $user_info['uid']) return JsonService::fail('非法用户');
  309. if (!$live_gift_num || !is_numeric($live_gift_num) || $live_gift_num < 0) return JsonService::fail('请选择礼物数量');
  310. //获取礼物配置列表
  311. $live_gift = LiveGift::liveGiftOne($live_gift_id);
  312. if (!$live_gift) return JsonService::fail('礼物不存在');
  313. //查看直播间是否存在
  314. $live_studio = LiveStudio::where(['stream_name' => $stream_name])->find();
  315. if (!$live_studio) return JsonService::fail('直播间不存在');
  316. $live_studio = $live_studio->toarray();
  317. if ($live_studio['special_id'] != $special_id) return JsonService::fail('直播专题有误');
  318. //看金币是否足够
  319. $gift_price = $live_gift['live_gift_price'] * $live_gift_num;
  320. $gold_name = SystemConfigService::get('gold_name');
  321. if ($user_info['gold_num'] <= 0 || $gift_price > $user_info['gold_num']) return JsonService::fail('您当前'.$gold_name.'不够,请充值',[],406);
  322. try{
  323. User::beginTrans();
  324. //插入打赏数据
  325. $add_gift['uid'] = $uid;
  326. $add_gift['live_id'] = $live_studio['id'];
  327. $add_gift['nickname'] = $user_info['nickname'];
  328. $add_gift['gift_id'] = $live_gift_id;
  329. $add_gift['gift_name'] = $live_gift['live_gift_name'];
  330. $add_gift['gift_price'] = $live_gift['live_gift_price'];
  331. $add_gift['total_price'] = $gift_price;
  332. $add_gift['gift_num'] = $live_gift_num;
  333. $add_gift['add'] = time();
  334. $live_reward_id = LiveReward::insertLiveRewardData($add_gift);
  335. //插入聊天记录
  336. $add_barrage['uid'] = $uid;
  337. $add_barrage['to_uid'] = 0;
  338. $add_barrage['type'] = 4;//礼物
  339. $add_barrage['barrage'] = $live_gift_id;//礼物ID
  340. $add_barrage['live_id'] = $live_reward_id;
  341. $add_barrage['is_show'] = 1;
  342. LiveBarrage::set($add_barrage);
  343. //插入虚拟货币支出记录(资金监管记录表)
  344. $gold_name = SystemConfigService::get("gold_name");
  345. $mark = '用户赠送'.$stream_name."号直播间".$live_gift_num.'个'.$live_gift["live_gift_name"].',扣除'.$gold_name.$gift_price.'金币';
  346. UserBill::expend('用户打赏扣除金币',$uid,'gold_num','live_reward',$gift_price,0,$this->userInfo['gold_num'],$mark);
  347. User::bcDec($uid,'gold_num',$gift_price,'uid');
  348. User::commitTrans();
  349. return JsonService::successful($mark);
  350. }catch (\Exception $e){
  351. User::rollbackTrans();
  352. return JsonService::fail('网路异常,打赏失败');
  353. }
  354. }
  355. /**
  356. * 带货商品列表
  357. */
  358. public function live_goods_list()
  359. {
  360. list($live_id) = UtilService::getMore([
  361. ['live_id', 0],
  362. ], $this->request, true);
  363. return JsonService::successful(LiveGoods::getLiveGoodsList(['is_show'=>1, 'live_id' => $live_id], 0, 0));
  364. }
  365. /**直播礼物排行榜
  366. * @throws \think\db\exception\DataNotFoundException
  367. * @throws \think\db\exception\ModelNotFoundException
  368. * @throws \think\exception\DbException
  369. */
  370. public function get_live_reward()
  371. {
  372. list($uid, $live_id, $page, $limit) = UtilService::postMore([
  373. ['uid', 0],
  374. ['live_id', 0],
  375. ['page', 1],
  376. ['limit', 20],
  377. ], $this->request, true);
  378. if (!$uid) return JsonService::fail('用户参数缺失');
  379. $user_info = $this->userInfo;
  380. if ($uid != $user_info['uid']) return JsonService::fail('非法用户');
  381. if (!$live_id) return JsonService::fail('直播间参数缺失');
  382. $live_info = LiveStudio::where('id',$live_id)->find();
  383. if (!$live_info) return JsonService::fail('直播间不存在');
  384. $data = LiveReward::getLiveRewardList(['live_id'=>$live_id], $page, $limit);
  385. $now_user_reward = LiveReward::getLiveRewardOne(['live_id' => $live_id, 'uid' => $uid]);
  386. $data['now_user_reward'] = $now_user_reward;
  387. return JsonService::successful($data);
  388. }
  389. }