123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361 |
- <?php
- namespace 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(){
- $content = input('content','');
- $images = input('images','');
- $audio_file = input('audio_file','');
- $audio_second = input('audio_second',0);
- $video_file = input('video_file',0);
- $type = input('type',1);//媒体类型:1=图片,2=语音,3=视频
- if(!$content && !$images && !$audio_file && !$video_file){
- $this->error(__('Invalid parameters'));
- }
- //关键字替换
- $content = Keyworld::sensitive($content);
- //只保留一个
- if($type == 1){
- $audio_file = '';
- $audio_second = 0;
- $video_file = '';
- }elseif($type == 2){
- $images = '';
- $video_file = '';
- }else{
- $audio_file = '';
- $audio_second = 0;
- $images = '';
- }
- $data = [
- 'user_id' => $this->auth->id,
- 'content' => $content,
- 'images' => $images,
- 'audio_file' => $audio_file,
- 'audio_second' => $audio_second,
- 'video_file' => $video_file,
- 'type' => $type,
- 'createtime' => time(),
- 'updatetime' => time(),
- 'auditstatus' => 1, //默认通过
- ];
- //如果强制审核,默认不通过
- $dongtai_audit_switch = config('site.dongtai_audit_switch');
- if($dongtai_audit_switch == 1){
- $data['auditstatus'] = 0;
- }
- Db::startTrans();
- $id = Db::name('topic_dongtai')->insertGetId($data);
- //task任务
- //发第一条动态
- if($data['auditstatus'] == 1){
- $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,10);
- if($task_rs === false){
- Db::rollback();
- $this->error('完成任务失败');
- }
- }
- Db::commit();
- $this->success('发布成功',$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; //不是自己的,就只能看审核通过的
- }
- $field = 'dt.*,user.username,user.nickname,user.avatar,user.gender,user.birthday,user.idcard_status,user.real_status';
- $list = Db::name('topic_dongtai')->alias('dt')
- ->join('user','dt.user_id = user.id','LEFT')
- ->field($field)
- ->where($where)
- ->order('dt.id desc')->autopage()->select();
- $list = list_domain_image($list,['images','audio_file','avatar','video_file']);
- if(!empty($list)){
- foreach($list as $key => &$val){
- //用户年龄
- $val['age'] = birthtime_to_age($val['birthday']);
- unset($val['birthday']);
- //追加点赞
- $val['isgood'] = $this->is_good($val['id'],$this->auth->id);
- //时间
- $val['createtime_text'] = get_last_time($val['createtime']);
- }
- }
- $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)->delete();
- if (!$delRes) {
- Db::rollback();
- $this->error('动态删除失败');
- }
- //点赞
- Db::name('topic_dongtai_good')->where('dt_id',$id)->delete();
- Db::commit();
- $this->success('删除成功');
- }
- //某个圈子里的动态列表,关注,最新,附近
- public function topic_list(){
- $where = [
- 'dt.auditstatus' => 1,
- ];
- //最新
- $order = input('orderby','new');
- $orderby = 'dt.goodnum desc,dt.id desc';
- //关注
- $where_follow = '';
- if($order == 'follow'){
- //关注的人
- $follow_user_ids = Db::name('user_follow')->where(['uid'=>$this->auth->id])->column('follow_uid');
- if(!empty($follow_user_ids)){
- $where_follow .= '(dt.user_id IN ('.implode(',',$follow_user_ids).'))';
- }
- //默认
- if($where_follow == ''){
- $where_follow = 'dt.id = 0';
- }
- }
- //性别
- $where['user.gender'] = ['neq',$this->auth->gender];
- //排除黑名单的
- $where_black = [];
- $black_ids = Db::name('user_black')->where(['uid'=>$this->auth->id])->column('black_uid');
- if(!empty($black_ids)){
- $where_black['dt.user_id'] = ['NOTIN',$black_ids];
- }
- //列表
- $field = 'dt.*,user.username,user.nickname,user.avatar,user.gender,user.birthday,user.idcard_status,user.real_status';
- $list = Db::name('topic_dongtai')->alias('dt')
- ->join('user','dt.user_id = user.id','LEFT')
- ->field($field)
- ->where($where)
- ->where($where_follow)
- ->where($where_black)
- ->order($orderby)
- ->autopage()->select();
- $list = list_domain_image($list,['images','audio_file','avatar']);
- if(!empty($list)){
- foreach($list as $key => &$val){
- //用户年龄
- $val['age'] = birthtime_to_age($val['birthday']);
- unset($val['birthday']);
- //追加点赞
- $val['isgood'] = $this->is_good($val['id'],$this->auth->id);
- //时间
- $val['createtime_text'] = get_last_time($val['createtime']);
- }
- }
- $this->success('success',$list);
- }
- //详情
- public function info(){
- $id = input('id');
- $field = 'dt.*,user.username,user.nickname,user.avatar,user.gender,user.birthday,user.idcard_status,user.real_status';
- $info = Db::name('topic_dongtai')->alias('dt')
- ->join('user','dt.user_id = user.id','LEFT')
- ->field($field)
- ->where('dt.id',$id)->find();
- $info = info_domain_image($info,['images','audio_file','avatar','video_file']);
- if($info){
- //用户年龄
- $info['age'] = birthtime_to_age($info['birthday']);
- unset($info['birthday']);
- //是否点赞过
- $info['isgood'] = $this->is_good($id,$this->auth->id);
- //时间
- $info['createtime_text'] = get_last_time($info['createtime']);
- }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');
- $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){
- //入库
- $data = [];
- $data['user_id'] = $dt_user_id;
- $data['dt_id'] = $id;
- $data['from_user_id'] = $this->auth->id;
- $data['title'] = '点赞了你的动态';
- $data['createtime'] = time();
- Db::name('topic_dongtai_message')->insertGetId($data);
- }
- Db::commit();
- $this->success('点赞成功');
- }
- }
- //动态通知
- public function message(){
- $list = Db::name('topic_dongtai_message')->alias('msg')->field('msg.*,user.avatar,user.nickname')
- ->join('user','msg.from_user_id = user.id','LEFT')
- ->where('msg.user_id',$this->auth->id)->autopage()->select();
- $list = list_domain_image($list,['avatar']);
- $this->success(1,$list);
- }
- //举报
- public function report(){
- $dt_id = input('dt_id',0);
- $check = Db::name('topic_dongtai')->where('id',$dt_id)->find();
- if(empty($check)){
- $this->error('不存在的动态');
- }
- $data['dt_id'] = $dt_id;
- $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('举报成功');
- }
- //是否点赞
- 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;
- }
- }
- }
|