title-color and cache

This commit is contained in:
toogee 2020-04-19 14:29:34 +08:00
parent 449fc474e7
commit 72edb70ede
7 changed files with 75 additions and 16 deletions

View File

@ -40,7 +40,7 @@ class Article extends BaseController
switch ($type) { switch ($type) {
//查询文章,15个分1页 //查询文章,15个分1页
case 'jie': case 'jie':
$artList = ArticleModel::field('id,title,cate_id,user_id,create_time,is_top,is_hot')->with([ $artList = ArticleModel::field('id,title,title_color,cate_id,user_id,create_time,is_top,is_hot')->with([
'cate' => function($query){ 'cate' => function($query){
$query->where('delete_time',0)->field('id,catename'); $query->where('delete_time',0)->field('id,catename');
}, },
@ -51,7 +51,7 @@ class Article extends BaseController
break; break;
case 'hot': case 'hot':
$artList = ArticleModel::field('id,title,cate_id,user_id,create_time,is_top,is_hot')->with([ $artList = ArticleModel::field('id,title,title_color,cate_id,user_id,create_time,is_top,is_hot')->with([
'cate' => function($query){ 'cate' => function($query){
$query->where('delete_time',0)->field('id,catename'); $query->where('delete_time',0)->field('id,catename');
}, },
@ -62,7 +62,7 @@ class Article extends BaseController
break; break;
case 'top': case 'top':
$artList = ArticleModel::field('id,title,cate_id,user_id,create_time,is_top,is_hot')->with([ $artList = ArticleModel::field('id,title,title_color,cate_id,user_id,create_time,is_top,is_hot')->with([
'cate' => function($query){ 'cate' => function($query){
$query->where('delete_time',0)->field('id,catename'); $query->where('delete_time',0)->field('id,catename');
}, },
@ -73,7 +73,7 @@ class Article extends BaseController
break; break;
default: default:
$artList = ArticleModel::field('id,title,cate_id,user_id,create_time,is_top,is_hot')->with([ $artList = ArticleModel::field('id,title,title_color,cate_id,user_id,create_time,is_top,is_hot')->with([
'cate' => function($query){ 'cate' => function($query){
$query->where('delete_time',0)->field('id,catename'); $query->where('delete_time',0)->field('id,catename');
}, },
@ -113,7 +113,7 @@ class Article extends BaseController
$query->field('id,name,nickname,user_img,area_id,vip'); $query->field('id,name,nickname,user_img,area_id,vip');
} }
])->find($id); ])->find($id);
Cache::set('article_'.$id,$article,3600); Cache::tag('tagArtDetail')->set('article_'.$id,$article,3600);
} }
$comments = $article->comments()->where('status',1)->paginate(10); $comments = $article->comments()->where('status',1)->paginate(10);
$article->inc('pv')->update(); $article->inc('pv')->update();
@ -317,6 +317,23 @@ class Article extends BaseController
$res = ['status'=>0,'msg'=>'精贴已取消']; $res = ['status'=>0,'msg'=>'精贴已取消'];
} }
} }
//清除文章缓存
Cache::tag('tagArtDetail')->clear();
return json($res);
}
//改变标题颜色
public function titleColor()
{
$data = Request::param();
$result = ArticleModel::update($data);
if($result){
//清除文章缓存
Cache::tag(['tagArt','tagArtDetail'])->clear();
$res = ['code'=> 0, 'msg'=>'标题颜色设置成功'];
}else{
$res = ['code'=> -1, 'msg'=>'标题颜色设置失败'];
}
return json($res); return json($res);
} }

View File

@ -34,13 +34,13 @@ class Index extends BaseController
$query->field('id,name,nickname,user_img,area_id,vip'); $query->field('id,name,nickname,user_img,area_id,vip');
} }
])->withCount(['comments'])->order('create_time','desc')->limit(5)->select(); ])->withCount(['comments'])->order('create_time','desc')->limit(5)->select();
Cache::set('arttop',$artTop,60); Cache::tag('tagArtDetail')->set('arttop',$artTop,60);
} }
//首页文章显示20条 //首页文章显示20条
$artList = Cache::get('artlist'); $artList = Cache::get('artlist');
if(!$artList){ if(!$artList){
$artList = Article::field('id,title,cate_id,user_id,create_time,is_hot')->with([ $artList = Article::field('id,title,title_color,cate_id,user_id,create_time,is_hot')->with([
'cate' => function($query){ 'cate' => function($query){
$query->where('delete_time',0)->field('id,catename'); $query->where('delete_time',0)->field('id,catename');
}, },
@ -48,7 +48,7 @@ class Index extends BaseController
$query->field('id,name,nickname,user_img,area_id,vip'); $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(); ])->withCount(['comments'])->where(['status'=>1,'delete_time'=>0])->order('create_time','desc')->limit(20)->select();
Cache::set('artlist',$artList,60); Cache::tag('tagArt')->set('artlist',$artList,60);
} }
//热议文章 //热议文章

View File

@ -33,9 +33,11 @@
<input type="hidden" name="user_id" value="{:session('user_id')}"> <input type="hidden" name="user_id" value="{:session('user_id')}">
</div> </div>
</div> </div>
<div class="layui-col-md2"> {if ($user.auth == 1)}
<div class="layui-col-md1">
<div id="color"></div> <div id="color"></div>
</div> </div>
{/if}
</div> </div>
<div class="layui-row layui-col-space15 layui-form-item layui-hide" id="LAY_quiz"> <div class="layui-row layui-col-space15 layui-form-item layui-hide" id="LAY_quiz">
<div class="layui-col-md3"> <div class="layui-col-md3">

View File

@ -19,7 +19,7 @@
</a> </a>
<h2> <h2>
<a class="layui-badge">{$art.cate.catename}</a> <a class="layui-badge">{$art.cate.catename}</a>
<a href="{:url('article/detail',['id' => $art.id])}">{$art.title}</a> <a href="{:url('article/detail',['id' => $art.id])}" style="color:{$art.title_color};">{$art.title}</a>
</h2> </h2>
<div class="fly-list-info"> <div class="fly-list-info">
<a href="{:url('user/home',['id'=>$art.user.id])}" link> <a href="{:url('user/home',['id'=>$art.user.id])}" link>

View File

@ -8,7 +8,10 @@
<div class="layui-row layui-col-space15"> <div class="layui-row layui-col-space15">
<div class="layui-col-md8 content detail"> <div class="layui-col-md8 content detail">
<div class="fly-panel detail-box"> <div class="fly-panel detail-box">
<h1 style="color:{$article.title_color} ;font-weight:bold">{$article.title}</h1> <h1 style="color:{$article.title_color};font-weight:bold">{$article.title}
{if ($user.auth == 1)}<span style="float:right;" id="color"></span>{/if}
</h1>
<div class="fly-detail-info"> <div class="fly-detail-info">
<!-- <span class="layui-badge">审核中</span> --> <!-- <span class="layui-badge">审核中</span> -->
<span class="layui-badge layui-bg-green fly-detail-column">{$article.cate.catename}</span> <span class="layui-badge layui-bg-green fly-detail-column">{$article.cate.catename}</span>
@ -38,6 +41,7 @@
<span class="layui-btn layui-btn-xs jie-admin" type="set" field="hot" rank="0" style="background-color:#ccc;">取消加精</span> <span class="layui-btn layui-btn-xs jie-admin" type="set" field="hot" rank="0" style="background-color:#ccc;">取消加精</span>
{/if} {/if}
{/if} {/if}
</div> </div>
<span class="fly-list-nums"> <span class="fly-list-nums">
@ -267,11 +271,45 @@ layui.config({
,base: '/static/res/mods/' ,base: '/static/res/mods/'
}).extend({ }).extend({
fly: 'index' fly: 'index'
}).use(['fly', 'face'], function(){ }).use(['fly', 'face','colorpicker'], function(){
var $ = layui.$ var $ = layui.$
,fly = layui.fly; ,fly = layui.fly
//如果你是采用模版自带的编辑器,你需要开启以下语句来解析。 ,colorpicker = layui.colorpicker;
//预定义颜色项
colorpicker.render({
elem: '#color'
,color: '#393d49'
,predefine: true // 开启预定义颜色
,size: 'xs'
,done: function(color){
//改变标题颜色
$('h1').css("color", color);
var id = {$article.id};
$.ajax({
type:'post',
url:"{:url('Article/titleColor')}",
data:{id: id,title_color: color},
dataType:'json',
success:function(data){
if(data.code == 0){
layer.msg(data.msg,{
icon:6,
time:2000
});
} else {
layer.open({
content:data.msg,
icon:5,
adim:6
})
}
}
});
}
});
//如果你是采用模版自带的编辑器,你需要开启以下语句来解析。
$('.detail-body').each(function(){ $('.detail-body').each(function(){
var othis = $(this), html = othis.html(); var othis = $(this), html = othis.html();
othis.html(fly.content(html)); othis.html(fly.content(html));

View File

@ -35,10 +35,12 @@
<input type="hidden" name="user_id" value="{$article.user_id}"> <input type="hidden" name="user_id" value="{$article.user_id}">
</div> </div>
</div> </div>
{if ($user.auth == 1)}
<div class="layui-col-md1"> <div class="layui-col-md1">
<div id="color"></div> <div id="color"></div>
<div id="test9" style="margin-left: 30px;"></div> <div id="test9" style="margin-left: 30px;"></div>
</div> </div>
{/if}
</div> </div>
<div class="layui-row layui-col-space15 layui-form-item layui-hide" id="LAY_quiz"> <div class="layui-row layui-col-space15 layui-form-item layui-hide" id="LAY_quiz">
<div class="layui-col-md3"> <div class="layui-col-md3">

View File

@ -38,7 +38,7 @@
</a> </a>
<h2> <h2>
<a class="layui-badge">{$top.cate.catename}</a> <a class="layui-badge">{$top.cate.catename}</a>
<a href="{:url('article/detail',['id' => $top.id])}" style="color:{$top.title_color};font-weight:bold">{$top.title}</a> <a href="{:url('article/detail',['id' => $top.id])}" style="color:{$top.title_color};">{$top.title}</a>
</h2> </h2>
<div class="fly-list-info"> <div class="fly-list-info">
<a href="{:url('user/home',['id'=>$top.user_id])}" link> <a href="{:url('user/home',['id'=>$top.user_id])}" link>
@ -76,7 +76,7 @@
</a> </a>
<h2> <h2>
<a class="layui-badge">{$art.cate.catename}</a> <a class="layui-badge">{$art.cate.catename}</a>
<a href="{:url('article/detail',['id' => $art.id])}">{$art.title}</a> <a href="{:url('article/detail',['id' => $art.id])}" style="color:{$art.title_color};">{$art.title}</a>
</h2> </h2>
<div class="fly-list-info"> <div class="fly-list-info">
<a href="{:url('user/home',['id'=>$art.user.id])}" link> <a href="{:url('user/home',['id'=>$art.user.id])}" link>