diff --git a/app/admin/controller/Admin.php b/app/admin/controller/Admin.php index f60e53b..3d7e2f9 100644 --- a/app/admin/controller/Admin.php +++ b/app/admin/controller/Admin.php @@ -17,6 +17,7 @@ use think\facade\Db; use think\facade\Session; use think\exception\ValidateException; use app\common\model\User as UserModel; +use taoler\com\Files; class Admin extends AdminController { @@ -188,4 +189,18 @@ class Admin extends AdminController View::assign('admin',$admin); return View::fetch('set/user/repass'); } + + //清除缓存Cache + public function clearCache(){ + $atemp = app()->getRootPath().'runtime/admin/temp/'; + $itemp = app()->getRootPath().'runtime/index/temp/'; + $cache = app()->getRootPath().'runtime/cache/'; + Files::delDirAndFile($atemp); + Files::delDirAndFile($itemp); + if(is_dir($cache) && Files::delDirAndFile($cache)){ + return json(['code'=>0,'msg'=>'清除缓存成功']); + } else { + return json(['code'=>-1,'msg'=>'清除缓存失败']); + } + } } \ No newline at end of file diff --git a/app/common.php b/app/common.php index 41ad68b..bb63e4f 100644 --- a/app/common.php +++ b/app/common.php @@ -4,6 +4,7 @@ use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\Exception; use think\facade\Request; use think\facade\Db; +use think\facade\Cache; use think\facade\Session; use taoser\think\Auth; // 应用公共文件 @@ -75,7 +76,13 @@ if(!function_exists('getUserImg')) //根据文章分类ID查询分类名 function getCateName($cate_ename) { - return Db::name('cate')->where('ename',$cate_ename)->value('catename'); + $cname = Cache::get('cname'); + if(!$cname){ + $cname = Db::name('cate')->where('ename',$cate_ename)->value('catename'); + Cache::set('cname',$cname,3600); + } + return $cname; + // return Db::name('cate')->where('ename',$cate_ename)->value('catename'); } diff --git a/app/common/controller/AdminController.php b/app/common/controller/AdminController.php index 8d47113..b82ee09 100644 --- a/app/common/controller/AdminController.php +++ b/app/common/controller/AdminController.php @@ -14,6 +14,7 @@ use think\facade\Cache; use think\facade\View; use think\facade\Db; use taoser\think\Auth; +use taoler\com\Files; /** * 控制器基础类 @@ -234,5 +235,16 @@ abstract class AdminController } } } + //清除缓存Cache + public function clearData(){ + $dir = app()->getRootPath().'runtime/admin/temp'; + $cache = app()->getRootPath().'runtime/cache'; + if(is_dir($cache)){ + Files::delDirs($cache); + } + if(Files::delDirs($dir) ){ + return json(['code'=>0,'msg'=>'清除成功']); + } + } } \ No newline at end of file diff --git a/app/common/controller/BaseController.php b/app/common/controller/BaseController.php index 7a7afa1..255a329 100644 --- a/app/common/controller/BaseController.php +++ b/app/common/controller/BaseController.php @@ -226,7 +226,7 @@ abstract class BaseController protected function showNav() { //1.查询分类表获取所有分类 - $cateList = Db::name('cate')->where(['status'=>1,'delete_time'=>0])->order('sort','asc')->select(); + $cateList = Db::name('cate')->where(['status'=>1,'delete_time'=>0])->order('sort','asc')->cache(120)->select(); //2.将catelist变量赋给模板 公共模板nav.html View::assign('cateList',$cateList); @@ -236,13 +236,10 @@ abstract class BaseController //显示用户 protected function showUser() { - $user['user_id'] = Session::get('user_id'); //1.查询用户 - $user = Db::name('user')->field('id,name,nickname,user_img,sex,auth,city,email,sign,point')->where('id',$user['user_id'])->find(); - - //2.将User变量赋给模板 公共模板nav.html - View::assign('user',$user); - + $user = Db::name('user')->field('id,name,nickname,user_img,sex,auth,city,email,sign,point')->where('id',Session::get('user_id'))->cache(120)->find(); + //2.将User变量赋给模板 公共模板nav.html + View::assign('user',$user); } //显示网站设置 diff --git a/app/index/controller/Index.php b/app/index/controller/Index.php index dfd7650..4a72708 100644 --- a/app/index/controller/Index.php +++ b/app/index/controller/Index.php @@ -5,6 +5,7 @@ use app\common\controller\BaseController; use think\facade\View; use think\facade\Request; use think\facade\Db; +use think\facade\Cache; use app\common\model\Article; use app\common\model\User; use app\common\model\Cate; @@ -17,39 +18,63 @@ class Index extends BaseController { $types = input('type'); //幻灯 - $sliders = Db::name('slider')->where('slid_status',1)->where('delete_time',0)->where('slid_type',1)->whereTime('slid_over','>=',time())->select(); - + $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); + } + //置顶文章 - $artTop = Article::field('id,title,cate_id,user_id,create_time,is_top')->where(['is_top'=>1,'status'=>1,'delete_time'=>0])->with([ + $artTop = Cache::get('arttop'); + if(!$artTop){ + $artTop = Article::field('id,title,cate_id,user_id,create_time,is_top')->where(['is_top'=>1,'status'=>1,'delete_time'=>0])->with([ 'cate' => function($query){ $query->where('delete_time',0)->field('id,catename'); }, 'user' => function($query){ $query->field('id,name,nickname,user_img,area_id'); } - ])->withCount(['comments'])->order('create_time','desc')->limit(5)->withCache(30)->select(); - - //首页文章显示15条 - $artList = Article::field('id,title,cate_id,user_id,create_time,is_hot')->with([ + ])->withCount(['comments'])->order('create_time','desc')->limit(5)->select(); + Cache::set('arttop',$artTop,60); + } + + //首页文章显示20条 + $artList = Cache::get('artlist'); + if(!$artList){ + $artList = Article::field('id,title,cate_id,user_id,create_time,is_hot')->with([ 'cate' => function($query){ $query->where('delete_time',0)->field('id,catename'); }, 'user' => function($query){ $query->field('id,name,nickname,user_img,area_id'); } - ])->withCount(['comments'])->where(['status'=>1,'delete_time'=>0])->order('create_time','desc')->limit(15)->withCache(30)->select(); - + ])->withCount(['comments'])->where(['status'=>1,'delete_time'=>0])->order('create_time','desc')->limit(20)->select(); + Cache::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)->select(); - + //首页赞助 - $ad_index = Db::name('slider')->where(['slid_status'=>1,'delete_time'=>0,'slid_type'=>3])->whereTime('slid_over','>=',time())->select(); + $ad_index = Cache::get('adindex'); + if(!$ad_index){ + $ad_index = Db::name('slider')->where(['slid_status'=>1,'delete_time'=>0,'slid_type'=>3])->whereTime('slid_over','>=',time())->select(); + Cache::set('adindex',$ad_index,3600); + } //首页右栏 - $ad_comm = Db::name('slider')->where(['slid_status'=>1,'delete_time'=>0,'slid_type'=>2])->whereTime('slid_over','>=',time())->select(); + $ad_comm = Cache::get('adcomm'); + if(!$ad_comm){ + $ad_comm = Db::name('slider')->where(['slid_status'=>1,'delete_time'=>0,'slid_type'=>2])->whereTime('slid_over','>=',time())->select(); + Cache::set('adcomm',$ad_comm,3600); + } //友情链接 - $friend_links = Db::name('slider')->where(['slid_status'=>1,'delete_time'=>0,'slid_type'=>6])->whereTime('slid_over','>=',time())->field('slid_name,slid_href')->select(); + $friend_links = Cache::get('flinks'); + if(!$friend_links){ + $friend_links = Db::name('slider')->where(['slid_status'=>1,'delete_time'=>0,'slid_type'=>6])->whereTime('slid_over','>=',time())->field('slid_name,slid_href')->select(); + Cache::set('flinks',$friend_links,3600); + } $vs = [ 'slider' => $sliders, diff --git a/extend/taoler/com/Files.php b/extend/taoler/com/Files.php new file mode 100644 index 0000000..5f23988 --- /dev/null +++ b/extend/taoler/com/Files.php @@ -0,0 +1,281 @@ +isDir()) { + $sontDir = $dest . DIRECTORY_SEPARATOR . $iterator->getSubPathName(); + if (!is_dir($sontDir)) { + self::mkdirs($sontDir, 0755, true); + } + } else { + copy($item, $dest . DIRECTORY_SEPARATOR . $iterator->getSubPathName()); + } + } + return true; + } + + /*д + * @param string $type 1 Ϊɿ 2 ģ + */ + + public static function filePutContents($content,$filepath,$type){ + if($type==1){ + $str = file_get_contents($filepath); + $parten = '/\s\/\*+start\*+\/(.*)\/\*+end\*+\//iUs'; + preg_match_all($parten,$str,$all); + $ext_content = ''; + if($all[0]){ + foreach($all[0] as $key=>$val){ + $ext_content .= $val."\n\n"; + } + } + $content .= $ext_content."\n\n"; + $content .="}\n\n"; + } + ob_start(); + echo $content; + $_cache=ob_get_contents(); + ob_end_clean(); + if($_cache){ + $File = new \think\template\driver\File(); + $File->write($filepath, $_cache); + } + } + /** + * ȡļдС + * + * @param string $dir ļ· + * @return int + */ + public static function getDirSize($dir) + { + if(!is_dir($dir)){ + return false; + } + $handle = opendir($dir); + $sizeResult = 0; + while (false !== ($FolderOrFile = readdir($handle))) { + if ($FolderOrFile != "." && $FolderOrFile != "..") { + if (is_dir("$dir/$FolderOrFile")) { + $sizeResult += self::getDirSize("$dir/$FolderOrFile"); + } else { + $sizeResult += filesize("$dir/$FolderOrFile"); + } + } + } + + closedir($handle); + return $sizeResult; + } + + /** + * ļ + * + * @param $files + */ + public static function createFile($file,$content) + { + + $myfile = fopen($file, "w") or die("Unable to open file!"); + fwrite($myfile, $content); + fclose($myfile); + return true; + } + /** + * 鴴Ŀ¼ + * + * @param $files + */ + public static function createDirOrFiles($files) + { + foreach ($files as $key => $value) { + if (substr($value, -1) == '/') { + mkdir($value); + } else { + file_put_contents($value, ''); + } + } + } + + // жļĿ¼ǷдȨ + public static function isWritable($file) + { + if (DIRECTORY_SEPARATOR == '/' AND @ ini_get("safe_mode") == FALSE) { + return is_writable($file); + } + if (!is_file($file) OR ($fp = @fopen($file, "r+")) === FALSE) { + return FALSE; + } + fclose($fp); + return TRUE; + } + + /** + * д־ + * + * @param $path + * @param $content + * @return bool|int + */ + public static function writeLog($path, $content) + { + self::mkdirs(dirname($path)); + return file_put_contents($path, "\r\n" . $content, FILE_APPEND); + } +} \ No newline at end of file diff --git a/public/static/admin/lib/admin.js b/public/static/admin/lib/admin.js index 8ab78cc..27e9ce3 100644 --- a/public/static/admin/lib/admin.js +++ b/public/static/admin/lib/admin.js @@ -450,6 +450,33 @@ layui.define('view', function(exports){ iconElem.addClass(SCREEN_FULL).removeClass(SCREEN_REST); } } + + //清除缓存 + ,clearcache: function(){ + loading = layer.load(2, { + shade: [0.2, '#000'] + }); + admin.req({ + type: "post" + ,url: '/admin/Admin/clearCache' + ,success: function(data){ + if (data.code == 0) { + layer.close(loading); + layer.msg(data.msg,{ + icon:6, + time:2000 + }); + } else { + layer.close(loading); + layer.open({ + content:data.msg, + icon:5, + anim:6 + }); + } + } + }); + } //弹出关于面板 ,about: function(){ diff --git a/view/admin/public/header.html b/view/admin/public/header.html index abb3863..09ae347 100644 --- a/view/admin/public/header.html +++ b/view/admin/public/header.html @@ -46,7 +46,7 @@
  • - +
  • diff --git a/view/index/index/index.html b/view/index/index/index.html index 3cdffa4..5e59648 100644 --- a/view/index/index/index.html +++ b/view/index/index/index.html @@ -1,7 +1,6 @@ - {extend name="public:base" /} -{block name="title"}{$sysInfo.webname} - {$sysInfo.webtitle}{/block} +{block name="title"}{$sysInfo.webname}-{$sysInfo.webtitle}{/block} {block name="keywords"}{$sysInfo.keywords}{/block} {block name="description"} {$sysInfo.descript}{/block} {block name="column"}{include file="public/column" /}{/block} @@ -71,7 +70,7 @@ {volist name="artList" id="art" empty="还没有帖子"}
  • - {$art.user.name} + {$art.user.name}

    {$art.cate.catename} @@ -338,8 +337,8 @@ layui.cache.page = 'index'; layui.cache.user = { username: '{$user.name??'游客'}' - ,uid: {$user.id ? 168*$user.id : -1} - ,avatar: '{if condition="$user['user_img'] neq ''"}{$user['user_img']}{else /}/static/res/images/avatar/00.jpg{/if}' + ,uid: {$user.id ? $user.id : -1} + ,avatar: '{$user.name? $user['user_img']:'/static/res/images/avatar/00.jpg'}' ,experience: 83 ,sex: '{if condition="$user['sex'] eq 0"}男{else/}女{/if}' };