123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- <?php
- namespace app\api\controller;
- use app\common\controller\Api;
- use think\Db;
- use app\common\library\Keyworld;
- /**
- * 视频
- */
- class Video extends Api
- {
- protected $noNeedLogin = ['typelist','lists','info','good'];
- protected $noNeedRight = ['*'];
- public function typelist(){
- $list = Db::name('video_type')->where('is_show',1)->order('weigh desc')->select();
- $this->success('success',$list);
- }
- public function lists(){
- $type_id = input('type_id',0);
- $where = [
- 'is_show' => 1
- ];
- if($type_id){
- $where['type_id'] = $type_id;
- }
- $list = Db::name('video')->where($where)->order('weigh desc')->autopage()->select();
- $list = list_domain_image($list,['video_file','video_image']);
- if(!empty($list)){
- if($this->auth->isLogin()){
- $vidoe_ids = Db::name('video_good')->where('user_id',$this->auth->id)->column('video_id');
- }else{
- $vidoe_ids = [];
- }
- foreach($list as $key => &$val){
- $val['is_good'] = 0;
- if(in_array($val['id'],$vidoe_ids)){
- $val['is_good'] = 1;
- }
- }
- }
- $this->success('success',$list);
- }
- //获取详情
- public function info(){
- $id = input('id',0);
- $info = Db::name('video')->where('id',$id)->find();
- $info = info_domain_image($info,['video_file','video_image']);
- $info['is_good'] = 0;
- if($this->auth->isLogin()){
- $is_good = Db::name('video_good')->where('video_id',$id)->where('user_id',$this->auth->id)->find();
- if($is_good){
- $info['is_good'] = 1;
- }
- }
- $this->success('success',$info);
- }
- //评论列表
- public function answer_list(){
- $id = input('id',0);
- $list = Db::name('video_answer')->alias('va')
- ->field('va.info,va.createtime,user.nickname,user.avatar')
- ->join('user','va.user_id = user.id','LEFT')
- ->where('va.video_id',$id)
- ->order('va.id desc')->autopage()->select();
- if(!empty($list)){
- foreach($list as $key => &$val){
- $val['createtime'] = get_last_time($val['createtime']);
- $val['avatar'] = localpath_to_netpath($val['avatar']);
- }
- }
- $this->success(1,$list);
- }
- //点赞,取消点赞
- public function good(){
- $id = input('id');
- $where = [
- 'video_id' => $id,
- 'user_id' => $this->auth->id,
- ];
- $check = Db::name('video_good')->where($where)->find();
- $good_number = Db::name('video')->where('id',$id)->value('goodnum');
- if($check){
- $good_number -= 1;
- if($good_number < 0){$good_number = 0;}
- Db::startTrans();
- $rs = Db::name('video_good')->where($where)->delete();
- if(!$rs){
- Db::rollback();
- $this->success('已取消点赞',$good_number);
- }
- $down = Db::name('video')->where('id',$id)->setDec('goodnum');
- if($down === false){
- Db::rollback();
- $this->success('已取消点赞',$good_number);
- }
- Db::commit();
- $this->success('已取消点赞',$good_number);
- }else{
- $good_number += 1;
- Db::startTrans();
- $rs = Db::name('video_good')->insertGetId($where);
- if(!$rs){
- Db::rollback();
- $this->success('点赞成功',$good_number);
- }
- $up = Db::name('video')->where('id',$id)->setInc('goodnum');
- if($up === false){
- Db::rollback();
- $this->success('点赞成功',$good_number);
- }
- Db::commit();
- $this->success('点赞成功',$good_number);
- }
- }
- //评论
- public function answer(){
- $id = input('id',0);
- $info = input('info','');
- if(empty($info) || empty($id)){
- $this->error();
- }
- //关键字替换
- $info = Keyworld::sensitive($info);
- //data
- $data = [
- 'video_id' => $id,
- 'user_id' => $this->auth->id,
- 'info' => $info,
- 'createtime' => time(),
- 'updatetime' => time(),
- ];
- Db::startTrans();
- $rs = Db::name('video_answer')->insertGetId($data);
- $answernum = Db::name('video')->where('id',$id)->setInc('answernum');
- Db::commit();
- $this->success('评价成功');
- }
- }
|