order('sort','desc')->select(); $this->success('',$list); } /** * @Title("列表") * @Param ("page",desc="第几页") * @Param ("cid",desc="分类ID") * @Param ("keyword",desc="搜索关键字") * @Param ("exad",desc="之前加载的列表中所有的广告ID,用,隔开",default="") * @Returned ("id",desc="id") * @Returned ("title",desc="标题") * @Returned ("images",desc="封面,用的|隔开的") * @Returned ("has_video",desc="是否含有视频") * @Returned ("source",desc="来源") * @Returned ("ctype",desc="1新闻2广告") * @Returned ("create_time",desc="创建时间") * @Returned ("content",desc="内容") * @Returned ("likes_count",desc="点赞数") * @Returned ("is_liked",desc="是否已点赞") * @Returned ("user.avatar",desc="发布者用户头像") * @Returned ("user.username",desc="发布者用户名") */ public function index(){ $page=input('page/d')?:1; $exad=input('exad'); $cid=input('cid'); $keyword=input('keyword'); list($limit,$adNum,$splitNum)=$this->getLimit(); $xw=DataXw::show() ->when($cid,function (Query $query) use ($cid) { $query->where('category_id',$cid); }) ->when($keyword,function (Query $query) use ($keyword) { $query->whereLike('title',"%$keyword%"); }) ->with('category') ->withCount('comments') ->hidden(['content']) ->order('id','desc') ->paginate($limit); $xwArr=$xw->chunk($splitNum); if($xw->isEmpty()){ $this->success('',$xw); } $ads=DataXwAd::show() ->order('sort','desc') ->when($exad,function (Query $query)use ($exad){ $query->whereNotIn('id',$exad); }) ->limit($adNum) ->select(); $newList=[]; foreach ($xwArr as $key=>$xwOne){ $ad=$ads[$key]??null; foreach ($xwOne as $one){ $one['ctype']=1; $newList[]=$one; } if($ad && count($xwOne)>=$splitNum){ $ad['ctype']=2; $newList[]=$ad; } } $this->success('成功',[ 'data'=>$newList, 'total'=>$xw->total(), 'per_page'=>$limit, 'current_page'=>$page, 'last_page'=>$xw->lastPage(), ]); } protected function getLimit(){ $splitNum=sysconf('config_xw.split_num')?:3; $min=15; $limit=1; while ($limit*$splitNum<$min){ $limit++; } return [$limit*$splitNum,$limit,$splitNum]; } /** * @Title("详情") * @Param ("id",desc="新闻id") * @Returned ("见列表") */ public function show(){ $xw=DataXw::show() ->withCount(['comments','likes']) ->where('id',input('id'))->findOrFail(); $xw->append(['user']); $xw->is_liked=$xw->isLiked($this->getUser()); $this->success('',$xw); } /** * @Title("广告详情") * @Param ("id",desc="广告id") * @Returned ("见列表") */ public function ad_show(){ $xw=DataXwAd::show() ->where('id',input('id'))->findOrFail(); $this->success('',$xw); } /** * @Title("点赞或取消") * @Param ("id",desc="新闻id") */ public function like(){ $xw=DataXw::show() ->where('id',input('id'))->findOrFail(); $res = $xw->makeLike($this->getUser()); $this->success($res); } /** * @Title("评论列表") * @Param ("id",desc="新闻id") * @Param ("page",desc="第几页") * @Param ("limit",desc="每页数量") * @Returned ("id",desc="评论ID") * @Returned ("user.nickname",desc="发布者用户昵称") * @Returned ("user.headimg",desc="发布者用户头像") * @Returned ("create_time",desc="发布时间") * @Returned ("content",desc="发布内容") */ public function comments(){ $this->_vali([ 'id.require'=>'新闻有误', 'id.integer'=>'新闻有误', 'id.gt:0'=>'新闻有误', ],'get'); $xw=DataXw::show() ->where('id',input('id'))->findOrFail(); $comments=$xw ->comments() ->with(['user']) ->paginate(input('limit',15)); $this->success('',$comments); } /** * @Title("评论") * @Method("POST") * @Param ("id",desc="新闻id") * @Param ("content",desc="内容") */ public function comment(){ $data=$this->_vali([ 'id.require'=>'新闻有误', 'id.integer'=>'新闻有误', 'id.gt:0'=>'新闻有误', 'content.require'=>'内容必须', 'content.max:250'=>'内容过多', ]); $xw=DataXw::show()->where('id',$data['id'])->findOrFail(); $user=$this->getUser(); $comment=$xw->comments()->save([ 'content'=>$data['content'], 'uuid'=>$user['id'], ]); $this->success('评论成功',$comment); } /** * @Title("删除评论") * @Method("POST") * @Param ("id",desc="评论id") */ public function del_comment(){ $data=$this->_vali([ 'id.require'=>'评论有误', 'id.integer'=>'评论有误', 'id.gt:0'=>'评论有误', ]); DataXwComment::where('id',$data['id'])->findOrFail(); // $user=$this->getUser(); DataXwComment::where('id',$data['id'])->delete(); $this->success('删除成功'); } }