Browse Source

动态和话题

lizhen_gitee 1 year ago
parent
commit
5610b5a653

+ 23 - 0
application/api/controller/Banner.php

@@ -0,0 +1,23 @@
+<?php
+
+namespace app\api\controller;
+
+use app\common\controller\Api;
+use think\Db;
+/**
+ * 示例接口
+ */
+class Banner extends Api
+{
+    protected $noNeedLogin = ['*'];
+    protected $noNeedRight = ['*'];
+
+    //轮播图
+    public function banner()
+    {
+        $list = Db::name('banner')->field('id, title, image, url')->where(['status' => 1])->order('weigh', 'desc')->select();
+        $list = list_domain_image($list, ['image']);
+
+        $this->success(1, $list);
+    }
+}

+ 184 - 165
application/api/controller/Topicdongtai.php

@@ -19,7 +19,7 @@ class Topicdongtai extends Api
     public function addone(){
         $content = input('content','');
         $images = input('images','');
-        $topic_id = input('topic_id','');
+        $topic_ids = input('topic_ids','');
         if(!$content && !$images){
             $this->error(__('Invalid parameters'));
         }
@@ -28,7 +28,7 @@ class Topicdongtai extends Api
         $content = Keyworld::sensitive($content);
 
         $data = [
-            'topic_id' => $topic_id,
+            'topic_ids' => $topic_ids,
             'user_id' => $this->auth->id,
             'content' => $content,
             'images' => $images,
@@ -58,34 +58,37 @@ class Topicdongtai extends Api
         }
         $list = Db::name('topic_dongtai')->alias('dt')
             ->join('user','dt.user_id = user.id','LEFT')
-            ->join('topic_hub topic','dt.topic_id = topic.id','LEFT')
-            ->field('dt.*,user.nickname,user.avatar,user.gender,topic.name as topic_name')
+            ->join('user_wallet uw','user.id = uw.user_id','LEFT')
+            ->field('dt.*,user.nickname,user.avatar,user.gender,user.birthday,user.attribute,uw.vip_endtime')
             ->where('dt.user_id',$uid)
             ->order('dt.id desc')->autopage()->select();
-        $list = list_domain_image($list,['images','avatar']);
+        $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']);
+
+                //用户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['images_thumb'] = '';
-                if ($val['type'] == 2) {
-                    $images_url = explode('.', $val['images']);
-                    unset($images_url[count($images_url) - 1]);
-                    $val['images_thumb'] = join('.', $images_url) . '_0.jpg';
-                }
-
                 //时间
                 $val['createtime_text'] = get_last_time($val['createtime']);
 
                 //关注
                 $val['is_follow'] = $this->is_follow($val['user_id'],$this->auth->id);
 
-                //评论
+                //层主评论数量
                 $val['answernumber'] = Db::name('topic_dongtai_answer')->where(['dt_id'=>$val['id'],'level'=>1])->count();
+
+                //话题
+                $val['topic_text'] = Db::name('topic_hub')->where('id','IN',$val['topic_ids'])->column('name');
             }
         }
 
@@ -94,105 +97,145 @@ class Topicdongtai extends Api
 
     //动态删除
     public function delete(){
-        try {
-            $id = input('id',0);
-            $where['id'] = $id;
-            $where['user_id'] = $this->auth->id;
-            $dongtai = Db::name('topic_dongtai')->field('id,topic_id')->where($where)->find();
-            if (empty($dongtai)) {
-                throw new Exception('未找到动态信息');
-            }
-            $delRes = Db::name('topic_dongtai')->where('id',$id)->where('user_id',$this->auth->id)->delete();
-            if (!$delRes) {
-                throw new Exception('动态删除失败');
-            }
-            //圈子新增一个贴
-            if (!empty($dongtai['topic_id'])) {
-                $res = Db::name('topic_hub')->where('id',$dongtai['topic_id'])->setDec('t_number');
-                if (!$res) {
-                    throw new Exception('更新话题数量失败');
-                }
+
+        $id = input('id',0);
+        $where['id'] = $id;
+        $where['user_id'] = $this->auth->id;
+        $dongtai = Db::name('topic_dongtai')->field('id,topic_ids')->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('动态删除失败');
+        }
+        //话题少一个贴
+        if (!empty($dongtai['topic_ids'])) {
+            $res = Db::name('topic_hub')->where('id','IN',$dongtai['topic_ids'])->setDec('t_number');
+            if (!$res) {
+                Db::rollback();
+                $this->error('更新话题数量失败');
             }
-            $this->success('删除成功');
-        } catch (Exception $e) {
-            $this->error($e->getMessage());
         }
+        //删除对应的评论,点赞,评论点赞
+
+        Db::commit();
+        $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;
+
+
+    //某个圈子里的动态列表,关注,最新,附近
+    public function topic_list(){
+        $where = [];
+
+        //话题
+        $topic_id = input('topic_id',0);
+        $where_exp = [];
+        if($topic_id){
+//            $where['dt.topic_id'] = $topic_id;
+            $where_exp[] = ['exp',Db::raw("FIND_IN_SET('".$topic_id."',dt.topic_ids)")];
         }
-    }
-    //回复是否点赞
-    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;
+        //最新
+        $order = input('orderby','new');
+        $orderby  = 'dt.id desc';
+        //关注
+        if($order == 'follow'){
+            $follow_user_ids = Db::name('user_follow')->where(['uid'=>$this->auth->id])->column('follow_uid');
+            $where['dt.user_id'] = ['IN',$follow_user_ids];
+        }
+        //附近,同城
+        if($order == 'near'){
+            $where['dt.cityname'] = $this->auth->cityname;
         }
-    }
 
-    //是否关注
-    private function is_follow($user_id,$fans_id){
-        $where = [
-            'user_id' => $user_id,
-            'fans_id' => $fans_id,
-        ];
-        $check = Db::name('user_fans_follow')->where($where)->find();
-        if($check){
-            return 1;
-        }else{
-            return 0;
+        //排除黑名单的
+        $where2 = [];
+        $black_ids = Db::name('user_black')->where(['uid'=>$this->auth->id])->column('black_uid');
+        if(!empty($black_ids)){
+            $where2['dt.user_id'] = ['NOTIN',$black_ids];
         }
-    }
 
+        //列表
+        $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.nickname,user.avatar,user.gender,user.birthday,user.attribute,uw.vip_endtime')
+            ->where($where)
+            ->where($where2)
+            ->where($where_exp)
+            ->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']);
+
+                //用户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($val['user_id'],$this->auth->id);
+
+                //层主评论数量
+                $val['answernumber'] = Db::name('topic_dongtai_answer')->where(['dt_id'=>$val['id'],'level'=>1])->count();
+
+                //话题
+                $val['topic_text'] = Db::name('topic_hub')->where('id','IN',$val['topic_ids'])->column('name');
+            }
+        }
+
+        $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')
-            ->join('topic_hub topic','dt.topic_id = topic.id','LEFT')
-            ->field('dt.*,user.nickname,user.avatar,user.gender,topic.name as topic_name')
+            ->join('user_wallet uw','user.id = uw.user_id','LEFT')
+            ->field('dt.*,user.nickname,user.avatar,user.gender,user.birthday,user.attribute,uw.vip_endtime')
             ->where('dt.id',$id)->find();
-        $info = info_domain_image($info,['images','avatar']);
+        $info = info_domain_image($info,['images','audio_file','avatar']);
 
 
         if($info){
+
+            //用户年龄
+            $info['age'] = birthtime_to_age($info['birthday']);
+            unset($info['birthday']);
+
+            //用户vip
+            $info['is_vip'] = $info['vip_endtime'] > time() ? 1 : 0;
+            unset($info['vip_endtime']);
+
             //是否点赞过
             $info['isgood'] = $this->is_good($id,$this->auth->id);
 
-            //创建视频缩略图
-            $info['images_thumb'] = '';
-            if ($info['type'] == 2) {
-                $images_url = explode('.', $info['images']);
-                unset($images_url[count($images_url) - 1]);
-                $info['images_thumb'] = join('.', $images_url) . '_0.jpg';
-            }
-
             //时间
             $info['createtime_text'] = get_last_time($info['createtime']);
 
             //关注
             $info['is_follow'] = $this->is_follow($info['user_id'],$this->auth->id);
 
-            //评论
+            //层主评论数量
             $info['answernumber'] = Db::name('topic_dongtai_answer')->where(['dt_id'=>$id,'level'=>1])->count();
-            //$info['answer'] = $this->answer_list($id);
+
+            //话题
+            $info['topic_text'] = Db::name('topic_hub')->where('id','IN',$info['topic_ids'])->column('name');
         }
 
         $this->success('success',$info);
@@ -215,15 +258,21 @@ class Topicdongtai extends Api
         Db::startTrans();
         $where['createtime'] = time();
         $rs = Db::name('topic_dongtai_good')->insertGetId($where);
-        $up = Db::name('topic_dongtai')->where('id',$id)->setInc('goodnum');
+        if(!$rs){
+            Db::rollback();
+            $this->error('点赞失败');
+        }
 
-        if($rs && $up !== false){
-            \app\common\model\TaskLog::tofinish($this->auth->id,"VpXtablCsZ",1);
-            Db::commit();
-            $this->success('点赞成功');
+        $up = Db::name('topic_dongtai')->where('id',$id)->setInc('goodnum');
+        if($up === false){
+            Db::rollback();
+            $this->error('点赞失败');
         }
-        Db::rollback();
-        $this->error('点赞失败');
+
+//        \app\common\model\TaskLog::tofinish($this->auth->id,"VpXtablCsZ",1);
+
+        Db::commit();
+        $this->success('点赞成功');
     }
 
     //评论
@@ -328,7 +377,7 @@ class Topicdongtai extends Api
     }
 
     //举报
-    /*public function report(){
+    public function report(){
         $field = ['dt_id','type','content','images'];
         $data = request_post_hub($field);
 
@@ -340,10 +389,10 @@ class Topicdongtai extends Api
 
         Db::name('topic_dongtai_report')->insertGetId($data);
         $this->success('举报成功');
-    }*/
+    }
 
     //不感兴趣,屏蔽某条
-    public function screen(){
+    /*public function screen(){
         $data = [
             'user_id' => $this->auth->id,
             'dt_id'   => input('dt_id',0),
@@ -356,7 +405,7 @@ class Topicdongtai extends Api
         Db::name('topic_dongtai_screen')->insertGetId($data);
 
         $this->success('操作成功');
-    }
+    }*/
 
     //评论列表
     public function answer_list(){
@@ -440,77 +489,7 @@ class Topicdongtai extends Api
 
     }
 
-    //某个圈子里的动态列表,最新,推荐
-    public function topic_list(){
-        $topic_id = input('topic_id',0);
-        $order = input('orderby','new');
-
-        $orderby  = 'dt.id desc';
-        if($order == 'hot'){
-            $orderby = 'dt.goodnum desc';
-        }
-
-        $where = [];
-        if($topic_id){
-            $where['dt.topic_id'] = $topic_id;
-        }
-
-        if($order == 'follow'){
-            $follow_user_ids = Db::name('user_fans_follow')->where(['fans_id'=>$this->auth->id])->column('user_id');
-            $where['dt.user_id'] = ['IN',$follow_user_ids];
-        }
-
-        //排除屏蔽的
-        $screen_ids = Db::name('topic_dongtai_screen')->where('user_id',$this->auth->id)->column('dt_id');
-        if(!empty($screen_ids)){
-            $where['dt.id'] = ['NOTIN',$screen_ids];
-        }
-        //排除黑名单的
-        $where2 = [];
-        $black_ids = Db::name('user_blacklist')->where('user_id',$this->auth->id)->column('black_user_id');
-        if(!empty($black_ids)){
-            $where2['dt.user_id'] = ['NOTIN',$black_ids];
-        }
-
-        //
-        $list = Db::name('topic_dongtai')->alias('dt')
-            ->join('user','dt.user_id = user.id','LEFT')
-            ->join('topic_hub topic','dt.topic_id = topic.id','LEFT')
-            ->field('dt.*,user.nickname,user.avatar,user.gender,topic.name as topic_name')
-            ->where($where)
-            ->where($where2)
-            ->order($orderby)->autopage()->select();
-        $list = list_domain_image($list,['images','avatar']);
-
-        if(!empty($list)){
-            foreach($list as $key => &$val){
-
-                //追加点赞
-                $val['isgood'] = $this->is_good($val['id'],$this->auth->id);
-
-                //创建视频缩略图
-                $val['images_thumb'] = '';
-                if ($val['type'] == 2) {
-                    $images_url = explode('.', $val['images']);
-                    unset($images_url[count($images_url) - 1]);
-                    $val['images_thumb'] = join('.', $images_url) . '_0.jpg';
-                }
-
-                //时间
-                $val['createtime_text'] = get_last_time($val['createtime']);
 
-                //关注
-                $val['is_follow'] = $this->is_follow($val['user_id'],$this->auth->id);
-
-                //评论
-                $val['answernumber'] = Db::name('topic_dongtai_answer')->where(['dt_id'=>$val['id'],'level'=>1])->count();
-            }
-        }
-
-
-
-        $this->success('success',$list);
-    }
 
     ////////////////////////////////////////////////////////////
 
@@ -560,5 +539,45 @@ class Topicdongtai extends Api
         $this->success(1,$list);
     }
 
+    //是否点赞
+    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_follow($uid,$follow_uid){
+        $where = [
+            'uid' => $uid,
+            'follow_uid' => $follow_uid,
+        ];
+        $check = Db::name('user_follow')->where($where)->find();
+        if($check){
+            return 1;
+        }else{
+            return 0;
+        }
+    }
 
 }

+ 3 - 4
application/api/controller/Topichub.php

@@ -13,14 +13,14 @@ class Topichub extends Api
     protected $noNeedRight = ['*'];
 
 
-    //圈子广场,全部圈子
+    //话题列表
     public function lists(){
         $list = Db::name('topic_hub')->where('status',1)->order('weight desc,id desc')->autopage()->select();
         $list = list_domain_image($list,['image']);
         $this->success('success',$list);
     }
 
-    //某个圈子详情
+    //某个话题详情
     public function info(){
         $id   = input('id',0);
         $info = Db::name('topic_hub')->where(['status'=>1,'id'=>$id])->find();
@@ -30,7 +30,6 @@ class Topichub extends Api
     }
 
 
-    //某个圈子里的动态列表,全部,最新,最热
-    //放到 topicdongtai.php里了
+
 
 }

+ 45 - 4
application/common.php

@@ -511,24 +511,25 @@ function info_domain_image($data,$field){
     }
     foreach($data as $key => $val){
         if(in_array($key,$field)){
-            $data[$key] = one_domain_image($val);
+            $more = strpos($key,'images');
+            $data[$key] = one_domain_image($val,$more);
         }
     }
     return $data;
 }
 //支持单个字段,需要增加domain_cdnurl
 //支持image,images
-function one_domain_image($one){
+function one_domain_image($one,$more = false){
     if(!$one){
         return $one;
     }
-    if(strpos($one,',')){
+    if(strpos($one,',') || $more !== false){
         //逗号隔开的多个图片
         $one = explode(',',$one);
         foreach($one as $k => $v){
             $one[$k] = localpath_to_netpath($v);
         }
-        $one = implode(',',$one);
+//        $one = implode(',',$one);
     }else{
         $one = localpath_to_netpath($one);
     }
@@ -755,4 +756,44 @@ function createUniqueNo($prifix = 'P',$id = 0)
     $rt = $prifix.date('ymdHis', $s).$ms.$id; //年月日时分秒.用户id对10取余.微秒
 
     return $rt;
+}
+/**
+ * 时间转换
+ * @param null $time
+ * @return false|string
+ */
+function get_last_time($time = NULL) {
+    $text = '';
+    $nowtime = time();
+    $time = ($time === NULL || empty($time) || $time > $nowtime) ? $nowtime : intval($time);
+    $t = $nowtime - $time; //时间差 (秒)
+    $y = date('Y', $time)-date('Y', $nowtime);//是否跨年
+    switch($t){
+        case $t == 0:
+            $text = '刚刚';
+            break;
+        case $t < 60:
+            $text = $t . '秒前'; // 一分钟内
+            break;
+        case $t < 60 * 60:
+            $text = floor($t / 60) . '分钟前'; //一小时内
+            break;
+        case $t < 60 * 60 * 24:
+            $text = floor($t / (60 * 60)) . '小时前'; // 一天内
+            break;
+        case $t < 60 * 60 * 24 * 3:
+            $text = floor($time/(60*60*24)) ==1 ?'昨天 ' . date('H:i', $time) : '前天 ' . date('H:i', $time) ; //昨天和前天
+            break;
+        case $t < 60 * 60 * 24 * 30:
+            $text = date('m月d日 H:i', $time); //一个月内
+            break;
+        case $t < 60 * 60 * 24 * 365&&$y==0:
+            $text = date('m月d日', $time); //一年内
+            break;
+        default:
+            $text = date('Y年m月d日', $time); //一年以前
+            break;
+    }
+
+    return $text;
 }