LiveingController.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. /**
  3. * 直播列表
  4. */
  5. namespace app\admin\controller;
  6. use cmf\controller\AdminBaseController;
  7. use think\facade\Db;
  8. use think\db\Query;
  9. class LiveingController extends AdminbaseController {
  10. protected function getLiveClass(){
  11. $liveclass=Db::name("live_class")->order('list_order asc, id desc')->column('id,name');
  12. return $liveclass;
  13. }
  14. protected function getTypes($k=''){
  15. $type=[
  16. '0'=>'普通房间',
  17. '1'=>'密码房间',
  18. '2'=>'门票房间',
  19. '3'=>'计时房间',
  20. ];
  21. if($k===''){
  22. return $type;
  23. }
  24. return $type[$k];
  25. }
  26. function index(){
  27. $data = $this->request->param();
  28. $map=[];
  29. $map[]=['islive','=',1];
  30. $start_time= $data['start_time'] ?? '';
  31. $end_time= $data['end_time'] ?? '';
  32. if($start_time!=""){
  33. $map[]=['starttime','>=',strtotime($start_time)];
  34. }
  35. if($end_time!=""){
  36. $map[]=['starttime','<=',strtotime($end_time) + 60*60*24];
  37. }
  38. $uid=$data['uid'] ?? '';
  39. if($uid!=''){
  40. $map[]=['uid','=',$uid];
  41. }
  42. $configpri=getConfigPri();
  43. $lists = Db::name("user_live")
  44. ->where($map)
  45. ->order("starttime DESC")
  46. ->paginate(20);
  47. $liveclass=$this->getLiveClass();
  48. array_push($liveclass,['id'=>0,'name'=>'默认分类']);
  49. $lists->each(function($v,$k) use ($configpri,$liveclass){
  50. $v['userinfo']=getUserInfo($v['uid']);
  51. $where=[];
  52. $where['action']='sendgift';
  53. $where['touid']=$v['uid'];
  54. $where['showid']=$v['showid'];
  55. /* 本场总收益 */
  56. $totalcoin=Db::name("user_coinrecord")->where($where)->sum('totalcoin');
  57. if(!$totalcoin){
  58. $totalcoin=0;
  59. }
  60. /* 送礼物总人数 */
  61. $total_nums=Db::name("user_coinrecord")->where($where)->group("uid")->count();
  62. if(!$total_nums){
  63. $total_nums=0;
  64. }
  65. /* 人均 */
  66. $total_average=0;
  67. if($totalcoin && $total_nums){
  68. $total_average=round($totalcoin/$total_nums,2);
  69. }
  70. /* 人数 */
  71. $nums=zSize('user_'.$v['stream']);
  72. $v['totalcoin']=$totalcoin;
  73. $v['total_nums']=$total_nums;
  74. $v['total_average']=$total_average;
  75. $v['nums']=$nums;
  76. if($v['isvideo']==0 && $configpri['cdn_switch']!=5){
  77. $v['pull']=PrivateKeyA('rtmp',$v['stream'],0);
  78. }
  79. foreach ($liveclass as $k1 => $v1) {
  80. if($v['liveclassid']==$v1['id']){
  81. $v['liveclassname']=$v1['name'];
  82. break;
  83. }
  84. }
  85. return $v;
  86. });
  87. /*var_dump($lists);
  88. die;*/
  89. $lists->appends($data);
  90. $page = $lists->render();
  91. $this->assign('lists', $lists);
  92. $this->assign("page", $page);
  93. $this->assign("type", $this->getTypes());
  94. return $this->fetch();
  95. }
  96. public function add(){
  97. $this->assign("liveclass", $this->getLiveClass());
  98. return $this->fetch();
  99. }
  100. //添加保存
  101. public function add_post(){
  102. if($this->request->ispost()){
  103. $data=$this->request->param();
  104. $uid=$data['uid'];
  105. $pull=urldecode($data['pull']);
  106. $liveclassid=$data['liveclassid'];
  107. $userinfo=getUserInfo($uid);
  108. if(!$userinfo){
  109. $this->error('用户不存在');
  110. }
  111. $nowtime=time();
  112. if($userinfo['recommend_time']==0){
  113. $userinfo['recommend_time']=$nowtime;
  114. }
  115. $liveinfo=Db::name("user_live")->field("uid,islive")->where(['uid'=>$uid])->find();
  116. if($liveinfo){
  117. $this->error("该用户正在直播");
  118. }
  119. $stream=$uid.'_'.$nowtime;
  120. $data=array(
  121. "uid"=>$uid,
  122. "showid"=>$nowtime,
  123. "starttime"=>$nowtime,
  124. "title"=>'',
  125. "province"=>'',
  126. "city"=>'好像在火星',
  127. "stream"=>$stream,
  128. "thumb"=>'',
  129. "pull"=>$pull,
  130. "isvideo"=>1,
  131. "islive"=>1,
  132. "isrecommend"=>$userinfo['isrecommend'],
  133. "recommend_time"=>$userinfo['recommend_time'],
  134. "liveclassid"=>$liveclassid,
  135. );
  136. if($liveinfo){
  137. $result=Db::name("user_live")->where(['uid'=>$uid])->update($data);
  138. }else{
  139. $result=Db::name("user_live")->insert($data);
  140. }
  141. if($result!==false){
  142. $this->success('添加成功');
  143. }else{
  144. $this->error('添加失败');
  145. }
  146. }
  147. }
  148. public function edit(){
  149. $uid=$this->request->param('uid');
  150. if($uid){
  151. $live=Db::name("user_live")->where("uid={$uid}")->find();
  152. $this->assign('live', $live);
  153. }else{
  154. $this->error('数据传入失败!');
  155. }
  156. $this->assign("liveclass", $this->getLiveClass());
  157. return $this->fetch();
  158. }
  159. public function edit_post(){
  160. if($this->request->ispost()){
  161. $data=$this->request->param();
  162. $pull=$data['pull'];
  163. if($pull==''){
  164. $this->error('请填写视频地址');
  165. }
  166. $uid=$data['uid'];
  167. $result=Db::name("user_live")->where(['uid'=>$uid])->update($data);
  168. if($result!==false){
  169. $this->success('修改成功');
  170. }else{
  171. $this->error('修改失败');
  172. }
  173. }
  174. }
  175. public function del(){
  176. $id=$this->request->param('id',0,'intval');
  177. if($id){
  178. $result=Db::name("user_live")->where(["uid"=>$id])->delete();
  179. if($result){
  180. $this->success('删除成功');
  181. }else{
  182. $this->error('删除失败');
  183. }
  184. }else{
  185. $this->error('数据传入失败!');
  186. }
  187. }
  188. }