| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742 | <?phpnamespace app\api\controller;use app\common\controller\Api;use think\Db;use app\common\library\Keyworld;use think\Exception;/** * 圈子动态 */class Topicdongtai extends Api{    protected $noNeedLogin = [];    protected $noNeedRight = ['*'];    //发布动态    public function addone(){        $type = input('type',1);        $content = input('content','');        $images = input('images','');        $video_file = input('video_file','');        if(!$content && !$images && !$video_file){            $this->error(__('Invalid parameters'));        }        $images_arr = explode(',',$images);        if(count($images_arr) > 9){            $this->error('最多只能传9张图片');        }        //关键字替换        $content = Keyworld::sensitive($content);        //只保留一个        if($type == 1){            $video_file = '';        }else{            $images = '';        }        $data = [            'user_id' => $this->auth->id,            'type'  => $type,            'content' => $content,            'images' => $images,            'video_file' => $video_file,            'createtime' => time(),            'updatetime' => time(),            'auditstatus' => 1, //默认通过        ];        $msg = '发布成功';        //如果强制审核,默认不通过        $dongtai_audit_switch = config('site.dongtai_audit_switch');        if($dongtai_audit_switch == 1){            $data['auditstatus'] = 0;            $msg = '发布成功,请等待审核!';        }        $id = Db::name('topic_dongtai')->insertGetId($data);        $this->success($msg,$id);    }    //自己看列表    //某用户的帖子列表    public function my_lists(){        $uid = input('uid',$this->auth->id);        if (empty($uid)) {            $uid = $this->auth->id;        }        $where = [            'dt.user_id'=>$uid,        ];        if($uid != $this->auth->id){            $where['dt.auditstatus'] = 1;  //不是自己的,就只能看审核通过的        }        $list = Db::name('topic_dongtai')->alias('dt')            ->join('user','dt.user_id = user.id','LEFT')            ->field('dt.*,user.nickname,user.avatar')            ->where($where)            ->order('dt.id desc')->select();        $list = list_domain_image($list,['images','video_file','avatar']);        if(!empty($list)){            foreach($list as $key => &$val){                //追加点赞                $val['isgood'] = $this->is_good($val['id'],$this->auth->id);                //时间                $val['createtime_text'] = get_last_time($val['createtime']);                //层主评论数量                $val['answernumber'] = Db::name('topic_dongtai_answer')->where(['dt_id'=>$val['id'],'level'=>1])->count();            }        }        $this->success('success',$list);    }    //动态删除    public function delete(){        $id = input('id',0);        $where['id'] = $id;        $where['user_id'] = $this->auth->id;        $dongtai = Db::name('topic_dongtai')->field('id')->where($where)->find();        if (empty($dongtai)) {            $this->error('未找到动态信息');        }        Db::startTrans();        $delRes = Db::name('topic_dongtai')->where('id',$id)->where('user_id',$this->auth->id)->delete();        if (!$delRes) {            Db::rollback();            $this->error('动态删除失败');        }        //删除对应的评论,        Db::name('topic_dongtai_answer')->where('dt_id',$id)->delete();        //点赞,        Db::name('topic_dongtai_good')->where('dt_id',$id)->delete();        //评论点赞        Db::name('topic_answer_good')->where('dt_id',$id)->delete();        Db::commit();        $this->success('删除成功');    }    //某个圈子里的动态列表,关注,最新,附近    public function topic_list(){        $where = [            'dt.auditstatus' => 1,        ];        $orderby  = 'dt.id desc';        //列表        $field = 'dt.*,user.nickname,user.avatar';               $list = Db::name('topic_dongtai')->alias('dt')            ->join('user','dt.user_id = user.id','LEFT')            ->field($field)            ->where($where)            ->order($orderby)            ->autopage()->select();        $list = list_domain_image($list,['images','video_file','avatar']);        if(!empty($list)){            foreach($list as $key => &$val){                //追加点赞                $val['isgood'] = $this->is_good($val['id'],$this->auth->id);                //时间                $val['createtime_text'] = get_last_time($val['createtime']);                //层主评论数量                $val['answernumber'] = Db::name('topic_dongtai_answer')->where(['dt_id'=>$val['id'],'level'=>1])->count();            }        }        $this->success('success',$list);    }    //详情    public function info(){        $id = input('id');        $info = Db::name('topic_dongtai')->alias('dt')            ->join('user','dt.user_id = user.id','LEFT')            ->field('dt.*,user.nickname,user.avatar')            ->where('dt.id',$id)->find();        $info = info_domain_image($info,['images','video_file','avatar']);        if($info){            //是否点赞过            $info['isgood'] = $this->is_good($id,$this->auth->id);            //时间            $info['createtime_text'] = get_last_time($info['createtime']);            //层主评论数量            $info['answernumber'] = Db::name('topic_dongtai_answer')->where(['dt_id'=>$id,'level'=>1])->count();        }else{            $this->error('此动态已被删除');        }        $this->success('success',$info);    }    //点赞,取消点赞    public function good(){        $id = input('id');        $where = [            'dt_id' => $id,            'user_id'  => $this->auth->id,        ];        $check = Db::name('topic_dongtai_good')->where($where)->find();        $dt_user_id = Db::name('topic_dongtai')->where('id',$id)->value('user_id');        if($check){            Db::name('topic_dongtai_good')->where($where)->delete();            $down = Db::name('topic_dongtai')->where('id',$id)->setDec('goodnum');            //删除消息通知            Db::name('message')->where('dt_id',$id)->where('user_id',$this->auth->id)->where('infotype','dongtai_good')->delete();            $this->success('已取消点赞');        }else{            Db::startTrans();            $where['createtime'] = time();            $rs = Db::name('topic_dongtai_good')->insertGetId($where);            if(!$rs){                Db::rollback();                $this->error('点赞失败');            }            $up = Db::name('topic_dongtai')->where('id',$id)->setInc('goodnum');            if($up === false){                Db::rollback();                $this->error('点赞失败');            }            //系统消息            if($dt_user_id != $this->auth->id){                $msg_id = \app\common\model\Message::addMessage($id,$dt_user_id,$dt_user_id, $this->auth->id,'动态点赞','点赞了你的帖子','dongtai_good',$id);            }            Db::commit();            $this->success('点赞成功');        }    }    //评论    public function answer(){        /*if($this->apiLimit(1,5000) == false){            $this->error('抱歉,您在5秒内只能进行一次回复');        }*/        $id = input('id',0);        $content = input('content','');        $to_user_id = input('to_user_id',0);        $level = input('level',1);  //回复类型:1=层主回复楼主,2=层中回复        $floor = input('floor',0);        if(empty($content) || empty($id)){            $this->error();        }        //判断        if($level == 2 && $floor == 0){            $this->error('楼层错误');        }        $topic_dongtai = Db::name('topic_dongtai')->where('id',$id)->find();        if(empty($topic_dongtai)){            $this->error('帖子不存在');        }        //关键字替换        $content = Keyworld::sensitive($content);        //回复楼主,最新楼层        if($level == 1 || $floor == 0){            $to_user_id = 0;            $floor = 1;  //默认一楼            $last_floor = Db::name('topic_dongtai_answer')->where(['dt_id'=>$id,'level'=>1])->order('floor desc')->value('floor');            if($last_floor){                $floor = $last_floor + 1;            }        }        //判断user_id        if($to_user_id){            $to_user = Db::name('user')->where('id',$to_user_id)->value('id');            if(empty($to_user)){                $this->error('被回复的用户不存在');            }        }        //data        $data = [            'dt_id' => $id,            'floor' => $floor,            'user_id' => $this->auth->id,            'content' => $content,            'to_user_id' => $to_user_id,            'level' => $level,            'createtime' => time(),            'updatetime' => time(),        ];        Db::startTrans();        $rs = Db::name('topic_dongtai_answer')->insertGetId($data);        Db::name('topic_dongtai')->where('id',$id)->setInc('answernum');        //系统消息        if($level == 1){            //发给动态用户            $msg_user_id = $topic_dongtai['user_id'];            $msg_title = '动态评论';            $msg_content = $this->auth->nickname.'评论了你的动态';            $infotype_id = $rs;        }else{            //发给层主            $answer_info = Db::name('topic_dongtai_answer')->where(['dt_id'=>$id,'level'=>1,'floor'=>$floor])->find();            $msg_user_id = $answer_info['user_id'];            $msg_title = '动态评论点评';            $msg_content = $this->auth->nickname.'点评了你的动态评论';            $infotype_id = $answer_info['id'];        }        if($topic_dongtai['user_id'] != $this->auth->id){            $msg_id = \app\common\model\Message::addMessage($id,$topic_dongtai['user_id'],$msg_user_id,$this->auth->id,$msg_title,$msg_content,'dongtai_answer',$infotype_id);        }        Db::commit();        $this->success('评论成功');    }    //对评论点赞    public function answer_good(){        $dt_id = input('dt_id',0);        $answer_id = input('answer_id',0);        $where = [            'dt_id'     => $dt_id,            'answer_id' => $answer_id,            'user_id'   => $this->auth->id,        ];        $check = Db::name('topic_answer_good')->where($where)->find();        if($check){            Db::name('topic_answer_good')->where($where)->delete();            Db::name('topic_dongtai_answer')->where('id',$answer_id)->setDec('goodnum');            $this->success('已取消点赞');        }else{            Db::startTrans();            $where['createtime'] = time();            $rs = Db::name('topic_answer_good')->insertGetId($where);            $up = Db::name('topic_dongtai_answer')->where('id',$answer_id)->setInc('goodnum');            if($rs && $up !== false){                Db::commit();                $this->success('点赞成功');            }            Db::rollback();            $this->error('点赞失败');        }    }    //评论列表    public function answer_list(){        $dt_id = input('dt_id',0);        //楼        $floor_list = Db::name('topic_dongtai_answer')            ->alias('a')            ->field('a.*,user.nickname,user.avatar')            ->join('user','a.user_id = user.id','LEFT')            ->where(['a.dt_id'=>$dt_id,'a.level'=>1])->order('a.id desc')->autopage()->select();        $floor_list = list_domain_image($floor_list,['avatar']);        //追加子评论        if(!empty($floor_list)){            foreach($floor_list as $key => &$val){                //下面几条子回复,字符串                $val['childremark'] = '';                $map = [                    'a.dt_id' => $dt_id,                    'a.floor' => $val['floor'],                    'a.level' => 2,                ];                $number = Db::name('topic_dongtai_answer')->alias('a')->where($map)->count();                if($number > 0){                    $answer_info = Db::name('topic_dongtai_answer')                        ->alias('a')                        ->field('user.nickname')                        ->join('user','a.user_id = user.id','LEFT')                        ->where($map)->order('a.id desc')->find();                    $val['childremark'] = $answer_info['nickname'].'...等人,共'.$number.'条回复';                }                //时间处理                $val['createtime_text'] = get_last_time($val['createtime']);                //回复是否已赞                $val['is_good'] = $this->answer_is_good($val['id'],$this->auth->id);            }        }        $this->success(1,$floor_list);    }    //单独某一层的详细    public function answer_info(){        $answer_id = input('answer_id');        //楼        $floor_info = Db::name('topic_dongtai_answer')            ->alias('a')            ->field('a.*,user.username,user.nickname,user.avatar')            ->join('user','a.user_id = user.id','LEFT')            ->where(['a.id'=>$answer_id])->find();        if(empty($floor_info)){            $this->success(1,[]);        }        $floor_info = info_domain_image($floor_info,['avatar']);        $floor_info['createtime_text'] = get_last_time($floor_info['createtime']);        //回复是否已赞        $floor_info['is_good'] = $this->answer_is_good($answer_id,$this->auth->id);        $floor_info['answernumber'] = Db::name('topic_dongtai_answer')->where(['dt_id'=>$floor_info['dt_id'],'floor'=>$floor_info['floor'],'level'=>2])->count();        //层        $floors = $floor_info['floor'];        $child_lists = Db::name('topic_dongtai_answer')->alias('a')            ->field('a.*,user.username,user.nickname,user.avatar,tu.nickname as to_user_nickname')            ->join('user','a.user_id = user.id','LEFT')            ->join('user tu','a.to_user_id = tu.id','LEFT')            ->where(['a.dt_id'=>$floor_info['dt_id'],'a.floor'=>$floors,'a.level'=>2])->order('a.id desc')->autopage()->select();        $child_lists = list_domain_image($child_lists,['avatar','to_avatar']);        if(!empty($child_lists)){            foreach($child_lists as $key => &$answer){                $answer['is_good'] = $this->answer_is_good($answer['id'],$this->auth->id);                $answer['createtime_text'] = get_last_time($answer['createtime']);            }        }        //合并        $floor_info['child'] = $child_lists;        $this->success('success',$floor_info);    }    //是否点赞    private function is_good($dt_id,$uid){        $where = [            'dt_id' => $dt_id,            'user_id'  => $uid,        ];        $check = Db::name('topic_dongtai_good')->where($where)->find();        if($check){            return 1;        }else{            return 0;        }    }    //回复是否点赞    private function answer_is_good($answer_id,$uid){        $where = [            'answer_id' => $answer_id,            'user_id'  => $uid,        ];        $check = Db::name('topic_answer_good')->where($where)->find();        if($check){            return 1;        }else{            return 0;        }    }    //动态是否收藏    private function is_collect($dt_id,$uid){        $where = [            'user_id'  => $uid,            'table'    => 'topic_dongtai',            'table_id' => $dt_id,        ];        $check = Db::name('user_collect')->where($where)->find();        if($check){            return 1;        }else{            return 0;        }    }    //用户是否拉黑    protected function is_black($uid,$black_uid){        $where = [            'uid'       => $uid,            'black_uid' => $black_uid,        ];        $check = Db::name('user_black')->where($where)->find();        if($check){            return 1;        }else{            return 0;        }    }    //我的评论    public function my_answer(){        $map = [            'a.user_id' => $this->auth->id,            'a.level'    => 1,        ];        $list = Db::name('topic_dongtai_answer')->alias('a')            ->field('a.id,a.createtime,a.content,a.dt_id,            dt.images,dt.content as dt_content,dt.type as dt_type,dtuser.nickname as dtuser_nickname,dtuser.username as dtuser_username,usera.username,            usera.nickname,usera.avatar')            ->join('topic_dongtai dt','a.dt_id = dt.id','LEFT')            ->join('user dtuser','dt.user_id = dtuser.id','LEFT')            ->join('user usera','a.user_id = usera.id','LEFT')            ->where($map)->order('a.id desc')->autopage()->select();        $list = list_domain_image($list,['avatar']);        $this->success(1,$list);    }    //删除我的某个评论    public function delete_answer(){        $id = input('id',0);        if(!$id){            $this->error();        }        Db::startTrans();        $info = Db::name('topic_dongtai_answer')->where('id',$id)->where('user_id',$this->auth->id)->find();        if(!$info){            $this->error('不存在的动态评论');        }        if($info['level'] == 1){            //楼层内都删            $louceng_id = Db::name('topic_dongtai_answer')->where('dt_id',$info['dt_id'])->where('level',2)->where('floor',$info['floor'])->column('id');            if(!empty($louceng_id)){                Db::name('topic_dongtai')->where('id',$info['dt_id'])->setDec('answernum',count($louceng_id));//回复数减1                Db::name('topic_dongtai_answer')->where('id','IN',$louceng_id)->delete();//评论删掉                Db::name('topic_answer_good')->where('answer_id','IN',$louceng_id)->delete();//评论点赞删掉            }        }        Db::name('topic_dongtai')->where('id',$info['dt_id'])->setDec('answernum');//回复数减1        Db::name('topic_dongtai_answer')->where('id',$id)->delete();  //评论删掉        Db::name('topic_answer_good')->where('answer_id',$id)->delete();//评论点赞删掉        Db::commit();        $this->success();    }///////////////////////////////////////////////////////////////////////////////////////////////////////////////////    //我点赞的动态    public function my_good(){        $where = ['good.user_id'=>$this->auth->id];        $list = Db::name('topic_dongtai')->alias('dt')            ->join('user','dt.user_id = user.id','LEFT')            ->join('user_wallet uw','user.id = uw.user_id','LEFT')            ->join('topic_dongtai_good good','dt.id = good.dt_id','LEFT')            ->field('dt.*,dt.id as dt_id,user.username,user.nickname,user.avatar,user.gender,user.birthday,user.attribute,user.idcard_status,uw.vip_endtime')            ->where($where)            ->order('dt.id desc')->autopage()->select();        $list = list_domain_image($list,['images','video_file','avatar']);        if(!empty($list)){            foreach($list as $key => &$val){                $val['aite'] = json_decode($val['aite'],true);                //用户年龄                $val['age'] = birthtime_to_age($val['birthday']);                unset($val['birthday']);                //用户vip                $val['is_vip'] = $val['vip_endtime'] > time() ? 1 : 0;                unset($val['vip_endtime']);                //追加点赞                $val['isgood'] = $this->is_good($val['id'],$this->auth->id);                //时间                $val['createtime_text'] = get_last_time($val['createtime']);                //关注                $val['is_follow'] = $this->is_follow($this->auth->id,$val['user_id']);                //收藏                $val['is_collect'] = $this->is_collect($val['id'],$this->auth->id);                //拉黑                $val['is_black'] = $this->is_black($this->auth->id,$val['user_id']);                //层主评论数量                $val['answernumber'] = Db::name('topic_dongtai_answer')->where(['dt_id'=>$val['id'],'level'=>1])->count();            }        }        $this->success('success',$list);    }    //举报枚举    /*public function report_enum(){        $arr = [            '侮辱谩骂',            '色情低俗',            '政治敏感',            '违法违规',            '其他',        ];        $this->success(1,$arr);    }*/    //举报    public function report(){        $dt_id = input('dt_id',0);        $data['dt_id'] = $dt_id;        $check = Db::name('topic_dongtai')->where('id',$data['dt_id'])->find();        if(empty($check)){            $this->error('不存在的动态');        }        $data['user_id']    = $this->auth->id;        $data['to_user_id'] = $check['user_id'];        $data['createtime'] = time();        Db::name('topic_dongtai_report')->insertGetId($data);        $this->success('举报成功');    }    //收藏,取消收藏    public function collect(){        $where = [            'user_id'  => $this->auth->id,            'table'    => 'topic_dongtai',            'table_id' => input('id',0),        ];        $check = Db::name('user_collect')->where($where)->find();        if($check){            Db::name('user_collect')->where($where)->delete();            $this->success('已取消收藏');        }else{            Db::name('user_collect')->insertGetId($where);            $this->success('收藏成功');        }    }    //我的收藏    public function my_collect(){        $collect_id = Db::name('user_collect')->where(['table'=>'topic_dongtai','user_id'=>$this->auth->id])->column('table_id');        $where = ['dt.id'=>['IN',$collect_id]];        $list = Db::name('topic_dongtai')->alias('dt')            ->join('user','dt.user_id = user.id','LEFT')            ->join('user_wallet uw','user.id = uw.user_id','LEFT')            ->field('dt.*,user.username,user.nickname,user.avatar,user.gender,user.birthday,user.attribute,user.idcard_status,uw.vip_endtime')            ->where($where)            ->order('dt.id desc')->autopage()->select();        $list = list_domain_image($list,['images','video_file','avatar']);        if(!empty($list)){            foreach($list as $key => &$val){                $val['aite'] = json_decode($val['aite'],true);                //用户年龄                $val['age'] = birthtime_to_age($val['birthday']);                unset($val['birthday']);                //用户vip                $val['is_vip'] = $val['vip_endtime'] > time() ? 1 : 0;                unset($val['vip_endtime']);                //追加点赞                $val['isgood'] = $this->is_good($val['id'],$this->auth->id);                //时间                $val['createtime_text'] = get_last_time($val['createtime']);                //关注                $val['is_follow'] = $this->is_follow($this->auth->id,$val['user_id']);                //收藏                $val['is_collect'] = $this->is_collect($val['id'],$this->auth->id);                //拉黑                $val['is_black'] = $this->is_black($this->auth->id,$val['user_id']);                //层主评论数量                $val['answernumber'] = Db::name('topic_dongtai_answer')->where(['dt_id'=>$val['id'],'level'=>1])->count();            }        }        $this->success('success',$list);    }    //不感兴趣,屏蔽某条    /*public function screen(){        $data = [            'user_id' => $this->auth->id,            'dt_id'   => input('dt_id',0),        ];        $check = Db::name('topic_dongtai_screen')->where($data)->find();        if($check){            $this->success('操作成功');        }        Db::name('topic_dongtai_screen')->insertGetId($data);        $this->success('操作成功');    }*/}
 |