优化网站性能;

This commit is contained in:
2022-03-18 23:09:28 +08:00
parent d2cb402f71
commit 370a6cdc4d
6 changed files with 437 additions and 404 deletions

View File

@@ -1,6 +1,6 @@
<?php
return [
'\app\middleware\PermissionRecord',
'\app\middleware\AdminLog',
// '\app\middleware\PermissionRecord',
// '\app\middleware\AdminLog',
];

View File

@@ -24,167 +24,166 @@ use think\app\Url;
function json_message($data = [], $code = 0, $msg = '')
{
if (is_string($data)) {
if (is_string($data)) {
if(strpos($data,'http') === 0 || strpos($data,'/') === 0){
$data = [
'jump_to_url'=>$data
];
}else{
if (strpos($data, 'http') === 0 || strpos($data, '/') === 0) {
$data = [
'jump_to_url' => $data
];
} else {
$code = $code === 0 ? 500 : $code;
$msg = $data;
$data = [];
$code = $code === 0 ? 500 : $code;
$msg = $data;
$data = [];
}
} else if ($data instanceof Url) {
$data = [
'jump_to_url' => (string)$data
];
}
}else if($data instanceof Url){
$data = [
'jump_to_url'=>(string)$data
];
}
return json([
'code' => $code,
'msg' => $msg,
'data' => $data
]);
return json([
'code' => $code,
'msg' => $msg,
'data' => $data
]);
}
function get_system_config($name = '', $default = '')
{
$list = Cache::get('system_config');
$list = Cache::get('system_config');
if (empty($list)) {
try {
$list = SystemConfig::column('value', 'name');
Cache::set('system_config',$list);
} catch (\Throwable $th) {
return $default;
if (empty($list)) {
try {
$list = SystemConfig::column('value', 'name');
Cache::set('system_config', $list);
} catch (\Throwable $th) {
return $default;
}
}
}
if ($name === '') {
return $list;
}
if ($name === '') {
return $list;
}
if (isset($list[$name])) {
return $list[$name];
}
if (isset($list[$name])) {
return $list[$name];
}
return $default;
return $default;
}
function get_source_link($url)
{
if(empty($url)){
$url = '/static/images/avatar.jpeg';
}
if (strpos($url, '/') === 0) {
return $url;
}
if (strpos($url, 'http') === 0) {
return $url;
} else {
$resource_domain = get_system_config('resource_domain');
if (empty($resource_domain)) {
$resource_domain = request()->domain();
if (empty($url)) {
$url = '/static/images/avatar.jpeg';
}
if (strpos($url, '/') === 0) {
return $url;
}
if (strpos($url, 'http') === 0) {
return $url;
} else {
$resource_domain = get_system_config('resource_domain');
if (empty($resource_domain)) {
$resource_domain = request()->domain();
}
return $resource_domain . '/' . $url;
}
return $resource_domain . '/' . $url;
}
}
function de_source_link($url)
{
$domain = get_system_config('resource_domain') . '/';
if (strpos($url, $domain) === 0) {
return str_replace($domain, '', $url);
}
return false;
$domain = get_system_config('resource_domain') . '/';
if (strpos($url, $domain) === 0) {
return str_replace($domain, '', $url);
}
return false;
}
function save_url_file($url, $type)
{
$file_data = geturl($url);
$file_data = geturl($url);
$mime_type = MimeType::detectByContent($file_data);
$mime_type = MimeType::detectByContent($file_data);
$ext_name = array_search($mime_type, MimeType::getExtensionToMimeTypeMap());
$temp_file = tempnam(app()->getRuntimePath(), 'url_save_') . '.' . $ext_name;
file_put_contents($temp_file, $file_data);
$file = new File($temp_file);
$ext_name = array_search($mime_type, MimeType::getExtensionToMimeTypeMap());
$temp_file = tempnam(app()->getRuntimePath(), 'url_save_') . '.' . $ext_name;
file_put_contents($temp_file, $file_data);
$file = new File($temp_file);
$save_name = Filesystem::putFile('wx_public_account/qrcode_url', $file, 'unique');
$save_name = Filesystem::putFile('wx_public_account/qrcode_url', $file, 'unique');
$model_file = new UploadFiles();
$model_file->file_name = $file->getFilename();
$model_file->mime_type = $mime_type;
$model_file->ext_name = $file->extension();
$model_file->file_size = $file->getSize();
$model_file->file_md5 = $file->md5();
$model_file->file_sha1 = $file->sha1();
$model_file->create_time = time();
$model_file->type = $type;
$model_file = new UploadFiles();
$model_file->file_name = $file->getFilename();
$model_file->mime_type = $mime_type;
$model_file->ext_name = $file->extension();
$model_file->file_size = $file->getSize();
$model_file->file_md5 = $file->md5();
$model_file->file_sha1 = $file->sha1();
$model_file->create_time = time();
$model_file->type = $type;
$model_file->save_name = $save_name;
$model_file->save();
$model_file->save_name = $save_name;
$model_file->save();
return $save_name;
return $save_name;
}
function geturl($url)
{
$headerArray = array();
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headerArray);
$output = curl_exec($ch);
curl_close($ch);
$headerArray = array();
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headerArray);
$output = curl_exec($ch);
curl_close($ch);
return $output;
return $output;
}
function posturl($url, $data)
{
$data = json_encode($data);
$headerArray = array();
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headerArray);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($curl);
curl_close($curl);
return $output;
$data = json_encode($data);
$headerArray = array();
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headerArray);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($curl);
curl_close($curl);
return $output;
}
function format_size($filesize)
{
if ($filesize >= 1073741824) {
if ($filesize >= 1073741824) {
$filesize = round($filesize / 1073741824 * 100) / 100 . ' GB';
} elseif ($filesize >= 1048576) {
$filesize = round($filesize / 1073741824 * 100) / 100 . ' GB';
} elseif ($filesize >= 1048576) {
$filesize = round($filesize / 1048576 * 100) / 100 . ' MB';
} elseif ($filesize >= 1024) {
$filesize = round($filesize / 1048576 * 100) / 100 . ' MB';
} elseif ($filesize >= 1024) {
$filesize = round($filesize / 1024 * 100) / 100 . ' KB';
} else {
$filesize = round($filesize / 1024 * 100) / 100 . ' KB';
} else {
$filesize = $filesize . ' 字节';
}
$filesize = $filesize . ' 字节';
}
return $filesize;
return $filesize;
}
@@ -199,65 +198,65 @@ function format_size($filesize)
function array2level($array, $pid = 0, $level = 1)
{
static $list = [];
if ($level == 0) {
$list = [];
$level = 1;
}
foreach ($array as $v) {
if ($v['pid'] == $pid) {
$v['level'] = $level;
$list[] = $v;
array2level($array, $v['id'], $level + 1);
static $list = [];
if ($level == 0) {
$list = [];
$level = 1;
}
}
// halt($list);
foreach ($array as $v) {
if ($v['pid'] == $pid) {
$v['level'] = $level;
$list[] = $v;
array2level($array, $v['id'], $level + 1);
}
}
// halt($list);
return $list;
return $list;
}
function check_permission($key,$admin_id = null)
function check_permission($key, $admin_id = null)
{
if(is_null($admin_id)){
$admin_id = Session::get('admin_id');
}
if (is_null($admin_id)) {
$admin_id = Session::get('admin_id');
}
if(empty($admin_id)){
return true;
}
if (empty($admin_id)) {
return true;
}
if($admin_id == 1){
return true;
}
if ($admin_id == 1) {
return true;
}
$model_admin = Admin::cache(60)->find($admin_id);
$model_admin = Admin::cache(60)->find($admin_id);
if(empty($model_admin->getData('group_id'))){
return true;
}
if (empty($model_admin->getData('group_id'))) {
return true;
}
$cache_key = 'permission_'.$key;
$cache_key = 'permission_' . $key;
$model_permission = Cache::get($cache_key);
if (empty($model_permission)) {
$model_permission = AdminPermission::where('key',$key)->find();
Cache::set($cache_key,$model_permission);
}
$model_permission = Cache::get($cache_key);
if (empty($model_permission)) {
$model_permission = AdminPermission::where('key', $key)->find();
Cache::set($cache_key, $model_permission);
}
if (empty($model_permission)) {
$model_permission = AdminPermission::create([
'key'=>$key
]);
Cache::set($cache_key,$model_permission,60);
}
if (empty($model_permission)) {
$model_permission = AdminPermission::create([
'key' => $key
]);
Cache::set($cache_key, $model_permission, 60);
}
if(in_array($model_permission->id,$model_admin->group->permissions)){
return true;
}
if (in_array($model_permission->id, $model_admin->group->permissions)) {
return true;
}
return false;
return false;
}
@@ -275,5 +274,5 @@ function app_url(string $url = '', array $vars = [], $suffix = true, $domain = f
{
return url($url, $vars, $suffix, $domain);
return url($url, $vars, $suffix, $domain);
}

View File

@@ -5,136 +5,151 @@ namespace app\index\controller;
use app\model\Category;
use app\model\Post;
use app\model\PostCategory;
use think\facade\Cache;
use think\facade\Session;
use think\facade\View;
use think\Request;
class Index extends Common
{
/**
* 显示资源列表
*
* @return \think\Response
*/
public function index()
{
//
$sub_category = [];
$current_category = [];
/**
* 显示资源列表
*
* @return \think\Response
*/
public function index()
{
if (!empty($this->request->param('category_id'))) {
$sub_category = Category::where('pid', $this->request->param('category_id'))->where('type', 3)->order('sort asc')->select();
$current_category = Category::find($this->request->param('category_id'));
if (empty($this->request->param('sub_category_id'))) {
$categorys = [$this->request->param('category_id')];
$categorys = array_merge($categorys, array_column((array)Category::getListLevel($this->request->param('category_id')), 3));
$page_cache_key = md5($this->request->url());
$categorys_where = PostCategory::whereIn('category_id', $categorys);
$model_post = Post::hasWhere('categorys', $categorys_where)->where('status', 1)->order('id desc');
} else {
$model_post = Post::hasWhere('categorys', ['category_id' => $this->request->param('sub_category_id')])->where('status', 1)->order('id desc');
}
} else {
$content = Cache::get($page_cache_key);
$model_post = Post::where('status', 1)->order('id desc');
}
if (!empty($content)) {
return $content;
}
$model_post->where('type', 3);
//
$sub_category = [];
$current_category = [];
$keywords = $this->request->param('keywords');
if (!empty($this->request->param('category_id'))) {
$sub_category = Category::where('pid', $this->request->param('category_id'))->where('type', 3)->order('sort asc')->select();
$current_category = Category::find($this->request->param('category_id'));
if (empty($this->request->param('sub_category_id'))) {
$categorys = [$this->request->param('category_id')];
if (!empty($keywords)) {
$model_post->whereLike('title|desc', "%$keywords%");
$categorys = array_merge($categorys, array_column((array)Category::getListLevel($this->request->param('category_id')), 3));
$categorys_where = PostCategory::whereIn('category_id', $categorys);
$model_post = Post::hasWhere('categorys', $categorys_where)->where('status', 1)->order('id desc');
} else {
$model_post = Post::hasWhere('categorys', ['category_id' => $this->request->param('sub_category_id')])->where('status', 1)->order('id desc');
}
} else {
$model_post = Post::where('status', 1)->order('id desc');
}
$model_post->where('type', 3);
$keywords = $this->request->param('keywords');
if (!empty($keywords)) {
$model_post->whereLike('title|desc', "%$keywords%");
}
$list_post = $model_post->paginate([
'url' => 'Index/index',
'query' => $this->request->get(),
'list_rows' => 10
]);
View::assign('current_category', $current_category);
View::assign('sub_category', $sub_category);
View::assign('list_post', $list_post);
$content = View::fetch();
Cache::tag('page_cache')->set($page_cache_key, $content);
}
$list_post = $model_post->paginate([
'url' => 'Index/index',
'query' => $this->request->get()
]);
public function logout()
{
Session::clear();
View::assign('current_category', $current_category);
View::assign('sub_category', $sub_category);
$back_url = $this->request->param('back_url', '/');
return $this->success('退出成功', $back_url);
}
View::assign('list_post', $list_post);
/**
* 显示创建资源表单页.
*
* @return \think\Response
*/
public function create()
{
//
}
return View::fetch();
}
/**
* 保存新建的资源
*
* @param \think\Request $request
* @return \think\Response
*/
public function save(Request $request)
{
//
}
/**
* 显示指定的资源
*
* @param int $id
* @return \think\Response
*/
public function read($id)
{
//
}
public function logout()
{
Session::clear();
/**
* 显示编辑资源表单页.
*
* @param int $id
* @return \think\Response
*/
public function edit($id)
{
//
}
$back_url = $this->request->param('back_url','/');
return $this->success('退出成功',$back_url);
}
/**
* 保存更新的资源
*
* @param \think\Request $request
* @param int $id
* @return \think\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* 显示创建资源表单页.
*
* @return \think\Response
*/
public function create()
{
//
}
/**
* 保存新建的资源
*
* @param \think\Request $request
* @return \think\Response
*/
public function save(Request $request)
{
//
}
/**
* 显示指定的资源
*
* @param int $id
* @return \think\Response
*/
public function read($id)
{
//
}
/**
* 显示编辑资源表单页.
*
* @param int $id
* @return \think\Response
*/
public function edit($id)
{
//
}
/**
* 保存更新的资源
*
* @param \think\Request $request
* @param int $id
* @return \think\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* 删除指定资源
*
* @param int $id
* @return \think\Response
*/
public function delete($id)
{
//
}
/**
* 删除指定资源
*
* @param int $id
* @return \think\Response
*/
public function delete($id)
{
//
}
}

View File

@@ -20,7 +20,11 @@ class Category extends Base
[
'name' => 'category_type_list',
'field' => 'type'
]
],
[
'type' => 'tag',
'name' => 'page_cache'
],
];

View File

@@ -18,7 +18,11 @@ class Nav extends Base
'type' => 'key',
'name' => 'type_list',
'field' => 'type'
]
],
[
'type' => 'tag',
'name' => 'page_cache'
],
];
public static $statusName = [

View File

@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace app\model;
use app\common\model\Base;
use think\facade\Cache;
use think\facade\Request;
use think\Model;
@@ -13,161 +14,171 @@ use think\Paginator;
/**
* @mixin think\Model
*/
class Post extends Model
class Post extends Base
{
//
//
public static $stausNameList = [
0 => '不发布',
1 => '发布'
];
public static $autoClearCache = [
[
'name' => 'top_post'
],
[
'type' => 'tag',
'name' => 'page_cache'
],
];
use SoftDelete;
public static $stausNameList = [
0 => '不发布',
1 => '发布'
];
protected $defaultSoftDelete = 0;
use SoftDelete;
public function categorys()
{
return $this->hasMany(PostCategory::class, 'post_id');
}
protected $defaultSoftDelete = 0;
public function tags()
{
return $this->hasMany(PostTag::class, 'post_id');
}
public function comments()
{
return $this->hasMany(PostComment::class,'post_id');
}
public function getCommentCountAttr()
{
return PostComment::getPostCommentsCount($this->getData('id'));
}
public function setPublishTimeAttr($value)
{
return strtotime($value);
}
public function getPublishTimeTextAttr()
{
$value = $this->getData('publish_time');
return date('Y-m-d', $value);
}
public function getPublishTimeDatetimeAttr()
{
$value = $this->getData('publish_time');
return date('Y-m-d H:i:s', $value);
}
public function getCategorysListAttr()
{
$list_post_categorys = $this->getAttr('categorys');
$list = array_column($list_post_categorys->append(['category'])->toArray(), 'category');
$list = array2level($list, 0, 0);
return $list;
}
public function getTagsListAttr()
{
$list_post_tags = $this->getAttr('tags');
$list = array_column($list_post_tags->append(['tag'])->toArray(), 'tag');
return $list;
}
public function getDescShortAttr()
{
$desc = $this->getData('desc');
if (strlen($desc) > 100) {
$desc = mb_substr($desc, 0, 100) . '...';
public function categorys()
{
return $this->hasMany(PostCategory::class, 'post_id');
}
return $desc;
}
public function getDescListAttr()
{
$desc = $this->getData('desc');
if (empty($desc)) {
return '';
}
$list = explode("\n", $desc);
return $list;
}
public function getDescHtmlAttr()
{
$desc = $this->getData('desc');
if (empty($desc)) {
return '';
public function tags()
{
return $this->hasMany(PostTag::class, 'post_id');
}
return str_replace("\n", '<br>', $desc);
}
public function getStatusNameAttr()
{
return self::$stausNameList[$this->getData('status')];
}
public function setPubishTimeAttr($value)
{
return strtotime($value);
}
public function setContentAttr($value)
{
return json_encode($value);
}
public function setContentHtmlAttr($value)
{
return trim($value);
}
public function getContentAttr($value)
{
return json_decode($value, true);
}
public function getPosterAttr($value)
{
if (empty($value)) {
$value = '/static/images/avatar.jpeg';
public function comments()
{
return $this->hasMany(PostComment::class, 'post_id');
}
return get_source_link($value);
}
public function getReadUrlAttr()
{
public function getCommentCountAttr()
{
return Request::domain() . '/index/a' . $this->getData('uid') . '.html';
}
public function getShareTextAttr()
{
$share_text = get_system_config('post_share_text_tpl');
$share_text = str_replace('%post_title%', $this->getAttr('title'), $share_text);
$share_text = str_replace('%post_desc%', $this->getAttr('desc'), $share_text);
$share_text = str_replace('%post_url%', $this->getAttr('read_url'), $share_text);
return PostComment::getPostCommentsCount($this->getData('id'));
}
return $share_text;
}
public function setPublishTimeAttr($value)
{
return strtotime($value);
}
public function getPublishTimeTextAttr()
{
$value = $this->getData('publish_time');
return date('Y-m-d', $value);
}
public function getPublishTimeDatetimeAttr()
{
$value = $this->getData('publish_time');
return date('Y-m-d H:i:s', $value);
}
public function getCategorysListAttr()
{
$list_post_categorys = $this->getAttr('categorys');
$list = array_column($list_post_categorys->append(['category'])->toArray(), 'category');
$list = array2level($list, 0, 0);
return $list;
}
public function getTagsListAttr()
{
$list_post_tags = $this->getAttr('tags');
$list = array_column($list_post_tags->append(['tag'])->toArray(), 'tag');
return $list;
}
public function getDescShortAttr()
{
$desc = $this->getData('desc');
if (strlen($desc) > 100) {
$desc = mb_substr($desc, 0, 100) . '...';
}
return $desc;
}
public function getDescListAttr()
{
$desc = $this->getData('desc');
if (empty($desc)) {
return '';
}
$list = explode("\n", $desc);
return $list;
}
public function getDescHtmlAttr()
{
$desc = $this->getData('desc');
if (empty($desc)) {
return '';
}
return str_replace("\n", '<br>', $desc);
}
public function getStatusNameAttr()
{
return self::$stausNameList[$this->getData('status')];
}
public function setPubishTimeAttr($value)
{
return strtotime($value);
}
public function setContentAttr($value)
{
return json_encode($value);
}
public function setContentHtmlAttr($value)
{
return trim($value);
}
public function getContentAttr($value)
{
return json_decode($value, true);
}
public function getPosterAttr($value)
{
if (empty($value)) {
$value = '/static/images/avatar.jpeg';
}
return get_source_link($value);
}
public function getReadUrlAttr()
{
return Request::domain() . '/index/a' . $this->getData('uid') . '.html';
}
public function getShareTextAttr()
{
$share_text = get_system_config('post_share_text_tpl');
$share_text = str_replace('%post_title%', $this->getAttr('title'), $share_text);
$share_text = str_replace('%post_desc%', $this->getAttr('desc'), $share_text);
$share_text = str_replace('%post_url%', $this->getAttr('read_url'), $share_text);
return $share_text;
}
}