wupengfei před 2 roky
rodič
revize
8ac87822bf

+ 311 - 0
application/api/controller/Press.php

@@ -0,0 +1,311 @@
+<?php
+namespace app\api\controller;
+use app\common\model\ArticleCate;
+use app\common\model\ArticleComment;
+use app\common\model\ArticleCommentLike;
+use app\common\model\ArticleIntro;
+use app\common\model\ArticleItem;
+use app\common\model\ArticleLike;
+use app\common\model\PlateformComment;
+use app\common\model\PlateformLike;
+use app\common\model\SeriesArticleCate;
+use app\common\model\UserCollect;
+use app\common\model\UserArticle;
+use app\common\model\UserLevelRank;
+use app\common\model\UserSearch;
+use library\tools\Data;
+/**
+ * @title 新闻
+ * @controller Press
+ * @group base
+ */
+class Press extends Base
+{
+    // 需要登录的
+    protected $need_login = [];
+    public function initialize()
+    {
+        parent::initialize();
+        parent::setUid();
+        foreach ($this->need_login as $c) {
+            if($this->request->action() == strtolower($c)) {
+              //  parent::checkLogin();
+            }
+        }
+
+    }
+
+    /**
+     * @title 新闻接口说明
+     * @desc  新闻接口说明
+     * @author  qc
+     * @url /api/Press/classIntro
+     * @method GET
+     */
+     public function classIntro(){}
+
+    /**
+     * @title 获取新闻列表
+     * @desc 获取新闻列表
+     * @author qc
+     * @method GET
+     * @url /api/Press/getPressList
+     * @header name:Authorization require:1 desc:Token
+     * @param  name:title type:string default:-- desc:标题
+     * @param  name:page type:int default:0 desc:页数
+     * @param  name:page_num type:int default:20 desc:每页数
+     * @return name:title type:string default:-- desc:标题
+     * @return name:images type:string default:-- desc:图片多张|隔开
+     * @return name:cover type:string default:-- desc:视频封面
+     * @return name:video_url type:string default:-- desc:视频
+     * @return name:content type:string default:-- desc:详情
+     * @return name:create_at type:string default:-- desc:时间
+     * @return name:transmit_num type:int default:-- desc:转发量
+     * @return name:read_num type:int default:-- desc:阅读
+     */
+    public function getPressList(){
+        $title = input('get.title');
+        $list = \app\common\model\Press::where(['is_deleted'=>0])->when($title,function ($query)use($title){
+            if($title) $query->where('title','like','%.'.$title.'%');
+        })->order('sort desc,id desc')->limit($this->off_set,$this->page_num)->select()->toArray();
+        foreach ($list as &$v)
+        {
+            $v['comment_num'] = PlateformComment::where(['first_id'=>$v['id'],'type'=>1])->count();
+            $v['is_collect'] = UserCollect::checkCollectByType($this->user_id,1,$detail['id'],$dv['id']);
+            $dv['collect_num'] = UserCollect::where(['user_id'=>$this->user_id,'coll_type'=>1,'coll_id'=>$detail['id'],'children_id'=>$dv['id']])->count();
+            $dv['is_praise'] = VideoCommentLike::where(['user_id'=>$this->user_id,'type'=>2,'like_id'=>$dv['id']])->count();
+            $dv['praise_num'] = VideoCommentLike::where(['type'=>2,'like_id'=>$dv['id']])->count();
+        }
+        $this->success('ok',['list'=>$list]);
+    }
+
+    /**
+     * @title 获取新闻详情
+     * @desc 获取新闻详情
+     * @author qc
+     * @method GET
+     * @url /api/Press/getPressDetail
+     * @header name:Authorization require:1 desc:Token
+     * @param  name:id type:int default:0 desc:新闻id
+     * @return name:title type:string default:-- desc:标题
+     * @return name:images type:string default:-- desc:图片多张|隔开
+     * @return name:cover type:string default:-- desc:视频封面
+     * @return name:video_url type:string default:-- desc:视频
+     * @return name:content type:string default:-- desc:详情
+     * @return name:create_at type:string default:-- desc:时间
+     * @return name:transmit_num type:int default:-- desc:转发量
+     * @return name:read_num type:int default:-- desc:阅读
+     */
+    public function getPressDetail()
+    {
+        $sel_where[]  = ['id','=',input('get.id')];
+        $detail = \app\common\model\Press::where($sel_where)->find()->toArray();
+        $this->success('ok',['detail'=>$detail]);
+    }
+
+    /**
+     * @title 获取新闻评论列表
+     * @desc 获取新闻评论列表
+     * @author qc
+     * @method GET
+     * @url /api/Press/getPressCommentList
+     * @header name:Authorization require:1 desc:Token
+     * @param name:article_id type:int default:-- desc:新闻id
+     * @param name:item_id type:int default:-- desc:item_id
+     * @param name:sort_type type:int default:1 desc:排序规则1时间2热点
+     * @param name:page type:int default:-- desc:页数
+     * @return name:name type:string default:-- desc:会员名称
+     * @return name:headimg type:string default:-- desc:会员头像
+     * @return name:content type:string default:-- desc:内容
+     * @return name:parent type:array default:-- desc:评论上一级(数据同上【一维数组】)
+     * @return name:children type:array default:-- desc:二级评论(数据同上【二维数组】)
+     * @return name:like_num type:int default:-- desc:点赞数量
+     * @return name:is_like type:int default:-- desc:是否点赞(0否1是)
+     * @return name:total_num type:int default:-- desc:总评论数(仅一级)
+     */
+    public function getPressCommentList()
+    {
+        $where = [];
+        $where[] = ['c.article_id','=',input('get.article_id')];
+        $where[] = ['c.is_deleted','=',0];
+        $user_id = $this->user_id;
+        $sort_type= input('sort_type',1);
+        if(input('get.item_id')) $where[] = ['c.item_id','=',input('get.item_id')];
+        $order = $sort_type == 1 ? 'c.id desc' : 'c.like_times desc';
+        $list  =ArticleComment::where($where)
+            ->alias('c')
+            ->field('c.*,u.name,u.headimg')
+            ->leftJoin('store_member u','u.id = c.user_id')
+            ->order($order)
+            ->limit($this->off_set,$this->page_num)
+            ->select()->toArray();
+
+        array_walk($list,function (&$val,$k)use($user_id){
+            $children = ArticleComment::where(['c.pid'=>$val['id'],'c.is_deleted'=>0])
+                ->alias('c')->field('c.*,u.name,u.headimg')
+                ->leftJoin('store_member u','u.id = c.user_id')
+                ->order('c.id asc')
+                ->select()->toArray();
+            $val['children'] =  $children;
+            $val['like_num'] = ArticleCommentLike::where('like_id',$val['id'])->count();
+            $val['is_like'] =  ArticleCommentLike::where(['like_id'=>$val['id'],'user_id'=>$user_id])->count();
+            $parent  = ArticleComment::where(['c.id'=>$val['pid']])
+                ->alias('c')->field('c.*,u.name,u.headimg')
+                ->leftJoin('store_member u','u.id = c.user_id')
+                ->find();
+            $val['parent']  = $parent ? $parent->toArray() : null;
+        });
+        $total_num  = ArticleComment::where($where)->alias('c')->count();
+        $this->success('ok',['list'=>$list,'total_num'=>$total_num]);
+    }
+    
+    
+    /**
+     * @title 以下接口调用验证登录【需要header传Authorization】
+     * @desc 转发成功后调用
+     * @author qc
+     * @method
+     * @url /api/Press/needLogin()
+     */
+    public function needLogin(){}
+
+    /**
+     * @title 收藏||取消收藏
+     * @desc 用户收藏
+     * @author qc
+     * @method POST
+     * @url /api/Press/pressCollect
+     * @header name:Authorization require:1 desc:Token
+     * @param name:id type:int require:1 default:-- desc:新闻id
+     * @param name:status type:int default:0 desc:收藏状态(0取消收藏【未收藏】,1收藏)
+     */
+    public function pressCollect(){
+        $coll_status = UserCollect::plateCollect($this->user_id,4,input('post.id',0));
+        $coll_status ?  $this->success('收藏成功',['status'=>1]) :    $this->success('取消收藏成功',['status'=>0]);
+    }
+
+    /**
+     * @title 新闻转发(转发数量统计)
+     * @desc 转发成功后调用
+     * @author qc
+     * @method POST
+     * @url /api/Press/pressTransmit
+     * @header name:Authorization require:1 desc:Token
+     * @param name:id type:int default:-- desc:新闻id
+     */
+    public function pressTransmit()
+    {
+        $press_id = input('post.id',0);
+        \app\common\model\Press::where('id',$press_id)->setInc('transmit_num');
+        $this->success('ok');
+    }
+
+    /**
+     * @title 新闻阅读(阅读数量统计)
+     * @desc 新闻阅读
+     * @author qc
+     * @method POST
+     * @url /api/Press/pressRead
+     * @header name:Authorization require:1 desc:Token
+     * @param name:id type:int default:-- desc:新闻id
+     */
+    public function pressRead()
+    {
+        $press_id = input('post.id',0);
+        \app\common\model\Press::where('id',$press_id)->setInc('read_num');
+        $this->success('ok');
+    }
+
+    /**
+     * @title 新闻点赞||取消点赞
+     * @desc 点赞||取消点赞
+     * @author qc
+     * @method POST
+     * @url /api/Press/PressTags
+     * @header name:Authorization require:1 desc:Token
+     * @param name:id type:int default:1 desc:新闻id
+     * @return name:tags type:int default:1 desc:0取消成功,1点赞成功
+     */
+    public function pressTags()
+    {
+        $ret_val = PlateformLike::userTags($this->user_id,input('post.id'),1);
+        $this->success($ret_val == 1 ?'点赞成功':'取消成功',['tags'=>$ret_val]);
+    }
+
+    /**
+     * @title 新闻评论
+     * @desc 新闻评论
+     * @author qc
+     * @method POST
+     * @url /api/Press/pressComment
+     * @header name:Authorization require:1 desc:Token
+     * @param name:id type:int default:1 desc:新闻id
+     * @param name:content type:string default:-- desc:内容
+     * @return name:name type:string default:-- desc:会员名称
+     * @return name:headimg type:string default:-- desc:会员头像
+     * @return name:content type:string default:-- desc:内容
+     * @return name:create_at type:string default:-- desc:时间
+     */
+    public function pressComment()
+    {
+        $id = input('post.id');
+        $content = input('post.content',0);
+        if(!$content) $this->error('请输入评论内容');
+        $res = PlateformComment::create(['user_id'=>$this->user_id,'content'=>$content,'first_id'=>$id,'type'=>1]);
+        $detail  =PlateformComment::where('p.id',$res->id)
+            ->alias('p')
+            ->field('p.id,p.content,p.create_at,u.name,u.headimg')
+            ->leftJoin('store_member u','u.id = c.user_id')
+            ->find()->toArray();
+        $this->success('评论成功',['detail'=>$detail]);
+    }
+
+    /**
+     * @title 新闻二级评论
+     * @desc 新闻二级评论
+     * @author qc
+     * @method POST
+     * @url /api/Press/pressCommend
+     * @header name:Authorization require:1 desc:Token
+     * @param name:id type:int default:1 desc:评论id
+     * @param name:content type:string default:-- desc:内容
+     * @return name:name type:string default:-- desc:会员名称
+     * @return name:headimg type:string default:-- desc:会员头像
+     * @return name:content type:string default:-- desc:内容
+     */
+    public function pressCommend()
+    {
+        $comment = PlateformComment::where('id',input('post.id'))->find()->toArray();
+        unset($comment['id']);
+        unset($comment['create_at']);
+        $comment['pid'] = input('post.id');
+        $comment['user_id'] = $this->user_id;
+        $comment['content'] = input('post.content');
+        $comment['lev']++;
+        if(!$comment['source_id']) $comment['source_id'] = input('post.id');
+        $res = PlateformComment::create($comment);
+        $detail  = PlateformComment::where('p.id',$res->id)
+            ->alias('p')
+            ->field('p.id,p.content,p.create_at,u.name,u.headimg')
+            ->leftJoin('store_member u','u.id = c.user_id')
+            ->find()->toArray();
+        $this->success('评论成功',['detail'=>$detail]);
+    }
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+}

+ 8 - 0
application/common/model/PlateformComment.php

@@ -0,0 +1,8 @@
+<?php
+namespace app\common\model;
+use think\Model;
+// 平台评论
+class PlateformComment extends Model
+{
+
+}

+ 33 - 0
application/common/model/PlateformLike.php

@@ -0,0 +1,33 @@
+<?php
+namespace app\common\model;
+use library\tools\Data;
+use think\Model;
+// 平台点赞
+class PlateformLike extends Model
+{
+
+    // 验证是否点赞
+    public static function checkTags($user_id,$id,$type){
+        return self::where(['user_id'=>$user_id,'like_id'=>$id,'type'=>$type])->value('id') ? 1:0;
+    }
+    /**
+     * @param $user_id
+     * @param $id
+     * @param $type 点赞类型 1新闻点赞2新闻评论点赞
+     */
+    public static function userTags($user_id,$id,$type)
+    {
+        $ret_val = 0;
+        $check_tags =  self::checkTags($user_id,$id,$type);
+        if($check_tags) {
+            ArticleLike::where(['user_id'=>$user_id,'like_id'=>$id,'type'=>$type])->delete();
+        }else{
+            Data::save('ArticleLike',
+                ['user_id'=>$user_id,'like_id'=>$id,'type'=>$type],'user_id',
+                ['user_id'=>$user_id,'like_id'=>$id,'type'=>$type]);
+            $ret_val = 1;
+        }
+        return $ret_val;
+    }
+
+}

+ 9 - 0
application/common/model/Press.php

@@ -0,0 +1,9 @@
+<?php
+namespace app\common\model;
+use think\Model;
+// 新闻
+class Press extends Model
+{
+
+
+}

+ 32 - 0
application/common/model/UserCollect.php

@@ -1,5 +1,6 @@
 <?php
 namespace app\common\model;
+use library\tools\Data;
 use think\Model;
 // 收藏
 class UserCollect extends Model
@@ -16,6 +17,34 @@ class UserCollect extends Model
         return self::where($where)->value('id') ? 1:0;
     }
 
+    /**
+     * @param $user_id
+     * @param $type 1平台视频,2资料,3图文4新闻
+     * @param $id
+     * @param int $children_id
+     * @return int
+     * @throws \think\Exception
+     * @throws \think\exception\PDOException
+     */
+    public static function plateCollect($user_id,$type,$id,$children_id = 0)
+    {
+        $check_collect = self::checkCollectByType($user_id,$type,$id,$children_id);
+        $ret_val = 0;
+        if($check_collect) {
+            self::where(['user_id'=>$user_id,'coll_type'=>$type,'coll_id'=>$id,'children_id'=>$children_id])->delete();
+        }else{
+            Data::save('UserCollect', [
+                'user_id'=>$user_id,
+                'coll_type'=>$type,
+                'coll_id'=>$id,
+                'children_id'=>$children_id,
+                'create_int'=>time()],'user_id',['user_id'=>$user_id,'coll_type'=>$type, 'coll_id'=>$id,'children_id'=>$children_id]);
+            $ret_val =  1;
+        }
+        return  $ret_val;
+    }
+
+
 
     public function videoItem()
     {
@@ -33,4 +62,7 @@ class UserCollect extends Model
     }
 
 
+
+
+
 }