优化article model
This commit is contained in:
parent
aa1aa01b5d
commit
8ab12d89fa
101
app/common/lib/Msgres.php
Normal file
101
app/common/lib/Msgres.php
Normal file
@ -0,0 +1,101 @@
|
||||
<?php
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace app\common\lib;
|
||||
|
||||
use think\facade\Lang;
|
||||
|
||||
class Msgres
|
||||
{
|
||||
static protected $res = [];
|
||||
|
||||
/**
|
||||
* 设置状态吗
|
||||
* @return array
|
||||
*/
|
||||
public static function setCodes()
|
||||
{
|
||||
return $res = [
|
||||
'success' => 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);
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -3,6 +3,7 @@ namespace app\common\model;
|
||||
|
||||
use think\Model;
|
||||
use think\model\concern\SoftDelete;
|
||||
use think\facade\Cache;
|
||||
|
||||
class Article extends Model
|
||||
{
|
||||
@ -71,12 +72,84 @@ class Article extends Model
|
||||
return $arts;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取置顶文章
|
||||
* @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;
|
||||
}
|
||||
|
||||
//置顶文章
|
||||
public function artTop()
|
||||
{
|
||||
$artTop = Article::where('status',1)->where('is_top',1)->select();
|
||||
/**
|
||||
* 获取首页文章列表,显示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;
|
||||
}
|
||||
|
||||
return $artTop;
|
||||
}
|
||||
//热议文章
|
||||
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;
|
||||
}
|
||||
}
|
@ -27,4 +27,11 @@ 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;
|
||||
}
|
||||
|
||||
}
|
73
app/common/model/Slider.php
Normal file
73
app/common/model/Slider.php
Normal file
@ -0,0 +1,73 @@
|
||||
<?php
|
||||
namespace app\common\model;
|
||||
|
||||
use think\Model;
|
||||
use think\model\concern\SoftDelete;
|
||||
use think\facade\Cache;
|
||||
|
||||
class Slider extends Model
|
||||
{
|
||||
//protected $pk = 'id'; //主键
|
||||
protected $autoWriteTimestamp = true; //开启自动时间戳
|
||||
protected $createTime = 'create_time';
|
||||
protected $updateTime = 'update_time';
|
||||
//开启自动设置
|
||||
protected $auto = [];
|
||||
|
||||
//仅更新有效
|
||||
protected $update = ['update_time'];
|
||||
|
||||
//软删除
|
||||
use SoftDelete;
|
||||
protected $deleteTime = 'delete_time';
|
||||
protected $defaultSoftDelete = 0;
|
||||
|
||||
/**
|
||||
* 首页幻灯
|
||||
* @return mixed|\think\Collection
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
*/
|
||||
public function getSliderList()
|
||||
{
|
||||
$sliders = Cache::get('slider');
|
||||
if(!$sliders){
|
||||
$sliders = $this::where(['slid_status'=>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';
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
@ -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');
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user