Video.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. use app\common\library\Keyworld;
  6. /**
  7. * 视频
  8. */
  9. class Video extends Api
  10. {
  11. protected $noNeedLogin = ['typelist','lists','info','good'];
  12. protected $noNeedRight = ['*'];
  13. public function typelist(){
  14. $list = Db::name('video_type')->where('is_show',1)->order('weigh desc')->select();
  15. $this->success('success',$list);
  16. }
  17. public function lists(){
  18. $type_id = input('type_id',0);
  19. $where = [
  20. 'is_show' => 1
  21. ];
  22. if($type_id){
  23. $where['type_id'] = $type_id;
  24. }
  25. $list = Db::name('video')->where($where)->order('weigh desc')->autopage()->select();
  26. $list = list_domain_image($list,['video_file','video_image']);
  27. if(!empty($list)){
  28. if($this->auth->isLogin()){
  29. $vidoe_ids = Db::name('video_good')->where('user_id',$this->auth->id)->column('video_id');
  30. }else{
  31. $vidoe_ids = [];
  32. }
  33. foreach($list as $key => &$val){
  34. $val['is_good'] = 0;
  35. if(in_array($val['id'],$vidoe_ids)){
  36. $val['is_good'] = 1;
  37. }
  38. }
  39. }
  40. $this->success('success',$list);
  41. }
  42. //获取详情
  43. public function info(){
  44. $id = input('id',0);
  45. $info = Db::name('video')->where('id',$id)->find();
  46. $info = info_domain_image($info,['video_file','video_image']);
  47. $info['is_good'] = 0;
  48. if($this->auth->isLogin()){
  49. $is_good = Db::name('video_good')->where('video_id',$id)->where('user_id',$this->auth->id)->find();
  50. if($is_good){
  51. $info['is_good'] = 1;
  52. }
  53. }
  54. $this->success('success',$info);
  55. }
  56. //评论列表
  57. public function answer_list(){
  58. $id = input('id',0);
  59. $list = Db::name('video_answer')->alias('va')
  60. ->field('va.info,va.createtime,user.nickname,user.avatar')
  61. ->join('user','va.user_id = user.id','LEFT')
  62. ->where('va.video_id',$id)
  63. ->order('va.id desc')->autopage()->select();
  64. if(!empty($list)){
  65. foreach($list as $key => &$val){
  66. $val['createtime'] = get_last_time($val['createtime']);
  67. $val['avatar'] = localpath_to_netpath($val['avatar']);
  68. }
  69. }
  70. $this->success(1,$list);
  71. }
  72. //点赞,取消点赞
  73. public function good(){
  74. $id = input('id');
  75. $where = [
  76. 'video_id' => $id,
  77. 'user_id' => $this->auth->id,
  78. ];
  79. $check = Db::name('video_good')->where($where)->find();
  80. $good_number = Db::name('video')->where('id',$id)->value('goodnum');
  81. if($check){
  82. $good_number -= 1;
  83. if($good_number < 0){$good_number = 0;}
  84. Db::startTrans();
  85. $rs = Db::name('video_good')->where($where)->delete();
  86. if(!$rs){
  87. Db::rollback();
  88. $this->success('已取消点赞',$good_number);
  89. }
  90. $down = Db::name('video')->where('id',$id)->setDec('goodnum');
  91. if($down === false){
  92. Db::rollback();
  93. $this->success('已取消点赞',$good_number);
  94. }
  95. Db::commit();
  96. $this->success('已取消点赞',$good_number);
  97. }else{
  98. $good_number += 1;
  99. Db::startTrans();
  100. $rs = Db::name('video_good')->insertGetId($where);
  101. if(!$rs){
  102. Db::rollback();
  103. $this->success('点赞成功',$good_number);
  104. }
  105. $up = Db::name('video')->where('id',$id)->setInc('goodnum');
  106. if($up === false){
  107. Db::rollback();
  108. $this->success('点赞成功',$good_number);
  109. }
  110. Db::commit();
  111. $this->success('点赞成功',$good_number);
  112. }
  113. }
  114. //评论
  115. public function answer(){
  116. $id = input('id',0);
  117. $info = input('info','');
  118. if(empty($info) || empty($id)){
  119. $this->error();
  120. }
  121. //关键字替换
  122. $info = Keyworld::sensitive($info);
  123. //data
  124. $data = [
  125. 'video_id' => $id,
  126. 'user_id' => $this->auth->id,
  127. 'info' => $info,
  128. 'createtime' => time(),
  129. 'updatetime' => time(),
  130. ];
  131. Db::startTrans();
  132. $rs = Db::name('video_answer')->insertGetId($data);
  133. $answernum = Db::name('video')->where('id',$id)->setInc('answernum');
  134. Db::commit();
  135. $this->success('评价成功');
  136. }
  137. }