diff --git a/app/common/lib/Msgres.php b/app/common/lib/Msgres.php new file mode 100644 index 0000000..1e5597a --- /dev/null +++ b/app/common/lib/Msgres.php @@ -0,0 +1,101 @@ + 0, + 'error' => 1, + 'add_success' => Lang::get('add success'), + 'add_error' => Lang::get('add error'), + 'edit_success' => Lang::get('edit success'), + 'edit_error' => Lang::get('edit error'), + 'delete_success' => Lang::get('delete success'), + 'delete_error' => Lang::get('delete error'), + 'upload_success' => Lang::get('upload success'), + 'upload_error' => Lang::get('upload error'), + 'upgrade_success' => Lang::get('upgrade success'), + 'upgrade_error' => Lang::get('upgrade error'), + 'illegal_request' => Lang::get('illegal request'), + ]; + } + + + /** + * 获取返回码 + * @param string $strCode + * @return mixed string + */ + public static function getCode(string $strCode){ + foreach(self::setCodes() as $k => $v){ + if($k == $strCode){ + return $v; + } + } + } + + /** + * 获取返回信息 如果不存在返回自身 + * @param string $strMsg + * @return mixed|string + */ + public static function getMsg(string $strMsg){ + if(empty($strMsg)){ + return ''; + }; + foreach(self::setCodes() as $k => $v){ + if($k == $strMsg){ + return $v; + } + } + return $strMsg; + } + + /** + * 成功提示 + * @param string $strMsg + * @param string|null $url + * @param string $data + * @return string|\think\response\Json + */ + public static function success(string $strMsg = '',string $url = null, $data = '') { + $result = [ + 'code' => self::getCode('success'), + 'msg' => self::getMsg($strMsg), + 'url' => $url, + 'data' => $data + ]; + return json($result); + } + + /** + * 失败提示 + * @param string $strMsg 消息提示码 + * @param string|null $url 跳转地址 + * @param string $data 返回数据 + * @return string|\think\response\Json + */ + public static function error(string $strMsg = '',string $url = null, $data = ''){ + $result = [ + 'code' => self::getCode('error'), + 'msg' => self::getMsg($strMsg), + 'url' => $url, + 'data' => $data + ]; + return json($result); + } + + +} diff --git a/app/common/model/Article.php b/app/common/model/Article.php index 2f2621d..8cccf56 100644 --- a/app/common/model/Article.php +++ b/app/common/model/Article.php @@ -3,6 +3,7 @@ namespace app\common\model; use think\Model; use think\model\concern\SoftDelete; +use think\facade\Cache; class Article extends Model { @@ -70,13 +71,85 @@ class Article extends Model $arts = Article::all(); return $arts; } - - - //置顶文章 - public function artTop() - { - $artTop = Article::where('status',1)->where('is_top',1)->select(); - - return $artTop; - } + + /** + * 获取置顶文章 + * @return mixed|\think\Collection + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + */ + public function getArtTop($pnum) + { + $artTop = Cache::get('arttop'); + if (!$artTop) { + $artTop = $this::field('id,title,title_color,cate_id,user_id,create_time,is_top,jie,pv')->where(['is_top' => 1, 'status' => 1, 'delete_time' => 0])->with([ + 'cate' => function ($query) { + $query->where('delete_time', 0)->field('id,catename,ename'); + }, + 'user' => function ($query) { + $query->field('id,name,nickname,user_img,area_id,vip'); + } + ])->withCount(['comments'])->order('create_time', 'desc')->limit($pnum)->select(); + Cache::tag('tagArtDetail')->set('arttop', $artTop, 60); + } + return $artTop; + } + + /** + * 获取首页文章列表,显示20个。 + * @return mixed|\think\Collection + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + */ + public function getArtList($pnum) + { + $artList = Cache::get('artlist'); + if(!$artList){ + $artList = $this::field('id,title,title_color,cate_id,user_id,create_time,is_hot,jie,pv') + ->with([ + 'cate' => function($query){ + $query->where('delete_time',0)->field('id,catename,ename'); + }, + 'user' => function($query){ + $query->field('id,name,nickname,user_img,area_id,vip'); + } ]) + ->withCount(['comments'])->where(['status'=>1,'delete_time'=>0])->order('create_time','desc')->limit($pnum)->select(); + Cache::tag('tagArt')->set('artlist',$artList,60); + } + return $artList; + } + + //热议文章 + public function getArtHot($pnum) + { + $artHot = $this::field('id,title') + ->withCount('comments') + ->where(['status'=>1,'delete_time'=>0]) + ->whereTime('create_time', 'year') + ->order('comments_count','desc') + ->limit($pnum) + ->withCache(60)->select(); + return $artHot; + } + + //详情 + public function getArtDetail($id) + { + $article = Cache::get('article_'.$id); + if(!$article){ + //查询文章 + $article = $this::field('id,title,content,status,cate_id,user_id,is_top,is_hot,is_reply,pv,jie,upzip,tags,title_color,create_time')->where('status',1)->with([ + 'cate' => function($query){ + $query->where('delete_time',0)->field('id,catename,ename'); + }, + 'user' => function($query){ + $query->field('id,name,nickname,user_img,area_id,vip'); + } + ])->find($id); + Cache::tag('tagArtDetail')->set('article_'.$id,$article,3600); + } + return $article; + } } \ No newline at end of file diff --git a/app/common/model/Comment.php b/app/common/model/Comment.php index 178f1c1..ddffc05 100644 --- a/app/common/model/Comment.php +++ b/app/common/model/Comment.php @@ -26,5 +26,12 @@ class Comment extends Model //评论关联用户 return $this->belongsTo('User','user_id','id'); } + + //获取评论 + public function getComment($id) + { + $comments = $this::where(['article_id'=>$id,'status'=>1])->order(['cai'=>'asc','create_time'=>'asc'])->paginate(10); + return $comments; + } } \ No newline at end of file diff --git a/app/common/model/Slider.php b/app/common/model/Slider.php new file mode 100644 index 0000000..6b4a23f --- /dev/null +++ b/app/common/model/Slider.php @@ -0,0 +1,73 @@ +1,'delete_time'=>0,'slid_type'=>1])->whereTime('slid_over','>=',time())->select(); + Cache::set('slider',$sliders,3600); + } + return $sliders; + } + + //添加 + public function add($data) + { + $result = $this::save($data); + + if($result) { + return 1; + } else { + return 'add_error'; + } + } + + //文章编辑 + public function edit($data) + { + $slider = $this::find($data['id']); + $result = $slider->save($data); + if($result) { + return 1; + } else { + return 'edit_error'; + } + } + + + + + + + + +} \ No newline at end of file diff --git a/app/index/controller/Article.php b/app/index/controller/Article.php index 3450611..6ac70b1 100644 --- a/app/index/controller/Article.php +++ b/app/index/controller/Article.php @@ -12,7 +12,7 @@ use app\common\model\Article as ArticleModel; use think\exception\ValidateException; use taoler\com\Message; use think\facade\Lang; -use app\common\lib\Msg; +use app\common\lib\Msgres; class Article extends BaseController { @@ -149,28 +149,19 @@ class Article extends BaseController //文章详情页 public function detail($id) { - $article = Cache::get('article_'.$id); - - if(!$article){ - //查询文章 - $article = ArticleModel::field('id,title,content,status,cate_id,user_id,is_top,is_hot,is_reply,pv,jie,upzip,tags,title_color,create_time')->where('status',1)->with([ - 'cate' => function($query){ - $query->where('delete_time',0)->field('id,catename,ename'); - }, - 'user' => function($query){ - $query->field('id,name,nickname,user_img,area_id,vip'); - } - ])->find($id); - Cache::tag('tagArtDetail')->set('article_'.$id,$article,3600); - } - if(!$article){ + $article = new ArticleModel(); + $artDetail = $article->getArtDetail($id); + if(!$artDetail){ // 抛出 HTTP 异常 throw new \think\exception\HttpException(404, '异常消息'); } - $comments = $article->comments()->where('status',1)->order(['cai'=>'asc','create_time'=>'asc'])->paginate(10); - $article->inc('pv')->update(); + $comments = $artDetail->comments()->where('status',1)->order(['cai'=>'asc','create_time'=>'asc'])->paginate(10); + //$comment = new \app\common\model\Comment(); + //$comments = $comment->getComment($id); + //dump($comments); + $artDetail->inc('pv')->update(); $pv = Db::name('article')->field('pv')->where('id',$id)->value('pv'); - $download = $article->upzip ? download($article->upzip,'file') : ''; + $download = $artDetail->upzip ? download($artDetail->upzip,'file') : ''; /* $nt = time(); @@ -188,13 +179,13 @@ class Article extends BaseController */ // 热议文章 - $artHot = ArticleModel::field('id,title')->withCount('comments')->where(['status'=>1,'delete_time'=>0])->whereTime('create_time', 'year')->order('comments_count','desc')->limit(10)->select(); + $artHot = $article->getArtHot(10); //文章广告 $ad_article = Db::name('slider')->where('slid_status',1)->where('delete_time',0)->where('slid_type',4)->whereTime('slid_over','>=',time())->select(); //通用右栏 $ad_comm = Db::name('slider')->where('slid_status',1)->where('delete_time',0)->where('slid_type',2)->whereTime('slid_over','>=',time())->select(); - View::assign(['article'=>$article,'pv'=>$pv,'comments'=>$comments,'artHot'=>$artHot,'ad_art'=>$ad_article,'ad_comm'=>$ad_comm,$download]); + View::assign(['article'=>$artDetail,'pv'=>$pv,'comments'=>$comments,'artHot'=>$artHot,'ad_art'=>$ad_article,'ad_comm'=>$ad_comm,$download]); return View::fetch(); } @@ -244,17 +235,18 @@ class Article extends BaseController $validate = new \app\common\validate\Article; //调用验证器 $result = $validate->scene('Artadd')->check($data); //进行数据验证 if (true !== $result) { - return Msg::error($validate->getError()); + return Msgres::error($validate->getError()); } - $result = ArticleModel::add($data); + $article = new ArticleModel(); + $result = $article->add($data); if ($result == 1) { $aid = Db::name('article')->max('id'); $link = (string)url('article/detail', ['id' => $aid]); //清除文章tag缓存 Cache::tag('tagArtDetail')->clear(); - $res = Msg::success('add_success', $link); + $res = Msgres::success('add_success', $link); } else { - $res = Msg::error('add_error'); + $res = Msgres::error('add_error'); } return $res; } @@ -279,16 +271,16 @@ class Article extends BaseController $res = $validate->scene('Artadd')->check($data); //进行数据验证 if(true !==$res){ - return Msg::error($validate->getError()); + return Msgres::error($validate->getError()); } else { $result = $article->edit($data); if($result == 1) { //删除原有缓存显示编辑后内容 Cache::delete('article_'.$id); $link = (string) url('article/detail',['id'=> $id]); - $editRes = Msg::success('edit_success',$link); + $editRes = Msgres::success('edit_success',$link); } else { - $editRes = Msg::error($result); + $editRes = Msgres::error($result); } return $editRes; } diff --git a/app/index/controller/Index.php b/app/index/controller/Index.php index 545f9c9..3db3b6e 100644 --- a/app/index/controller/Index.php +++ b/app/index/controller/Index.php @@ -8,10 +8,7 @@ use think\facade\Db; use think\facade\Cache; use app\common\model\Article; use app\common\model\User; -use app\common\model\Cate; -use app\common\model\Comment; -use think\facade\Cookie; -use app\common\lib\Msg; +use app\common\lib\Msgres; class Index extends BaseController { @@ -19,42 +16,16 @@ class Index extends BaseController { $types = input('type'); //幻灯 - $sliders = Cache::get('slider'); - if(!$sliders){ - $sliders = Db::name('slider')->where('slid_status',1)->where('delete_time',0)->where('slid_type',1)->whereTime('slid_over','>=',time())->select(); - Cache::set('slider',$sliders,3600); - } - + $slider = new \app\common\model\Slider(); + $sliders = $slider->getSliderList(); + + $article = new Article(); //置顶文章 - $artTop = Cache::get('arttop'); - if(!$artTop){ - $artTop = Article::field('id,title,title_color,cate_id,user_id,create_time,is_top,jie,pv')->where(['is_top'=>1,'status'=>1,'delete_time'=>0])->with([ - 'cate' => function($query){ - $query->where('delete_time',0)->field('id,catename,ename'); - }, - 'user' => function($query){ - $query->field('id,name,nickname,user_img,area_id,vip'); - } - ])->withCount(['comments'])->order('create_time','desc')->limit(5)->select(); - Cache::tag('tagArtDetail')->set('arttop',$artTop,60); - } - - //首页文章显示20条 - $artList = Cache::get('artlist'); - if(!$artList){ - $artList = Article::field('id,title,title_color,cate_id,user_id,create_time,is_hot,jie,pv')->with([ - 'cate' => function($query){ - $query->where('delete_time',0)->field('id,catename,ename'); - }, - 'user' => function($query){ - $query->field('id,name,nickname,user_img,area_id,vip'); - } - ])->withCount(['comments'])->where(['status'=>1,'delete_time'=>0])->order('create_time','desc')->limit(20)->select(); - Cache::tag('tagArt')->set('artlist',$artList,60); - } - - //热议文章 - $artHot = Article::field('id,title')->withCount('comments')->where(['status'=>1,'delete_time'=>0])->whereTime('create_time', 'year')->order('comments_count','desc')->limit(10)->withCache(60)->select(); + $artTop = $article->getArtTop(5); + //首页文章列表,显示20个 + $artList = $article->getArtList(20); + //热议文章 + $artHot = $article->getArtHot(10); //首页赞助 $ad_index = Cache::get('adindex'); @@ -174,7 +145,7 @@ class Index extends BaseController //return Msg::success('') } }else { - return Msg::error('illegal_request'); + return Msgres::error('illegal_request'); } }