更改文章主题等获取机制为直接主题目录对应文件夹获取

修正主题切换不生效的问题
一些细节优化
增加模板用户信息获取函数
This commit is contained in:
yumo 2024-02-06 15:51:33 +08:00
parent d69b4983cb
commit b02b2f39a8
13 changed files with 127 additions and 80 deletions

5
TODO.md Normal file
View File

@ -0,0 +1,5 @@
待完成功能列表
- 完善euapi主题
- 完善获取分类,分类下的数量列表,现在只实现当前分类,没有包含下钻分类
- 完善用户发帖数量等字段
- doc文档上输出

View File

@ -157,6 +157,9 @@ if (!function_exists('get_template_list')) {
{
$themeList = [];
$themeDir = public_path('template') . system_config('web_template') . '/pc/' . $type;
if (!file_exists($themeDir)){
return [];
}
if ($dh = opendir($themeDir)) {
while (($file = readdir($dh)) !== false) {
@ -217,4 +220,4 @@ if (!function_exists('cross')) {
else $str .= "--";
return $str . " ";
}
}
}

View File

@ -105,14 +105,6 @@ class Category extends AuthController
*/
public function add(string $pid = ''): string
{
$templatePath = system_config('web_template');
$themeInfoFile = public_path('template' . DIRECTORY_SEPARATOR . $templatePath) . 'info.json';
if (file_exists($themeInfoFile)) {
$themeInfo = json_decode(file_get_contents($themeInfoFile), true);
$themeList = $themeInfo['category_list'];
} else {
$themeList = [];
}
$where = Util::postMore([
'name' => '',
'status' => '',
@ -121,6 +113,10 @@ class Category extends AuthController
$category = get_tree_list($category);
$this->assign("category", $category);
$this->assign("pid", $pid);
$themeList = get_template_list(Data::DOCUMENT_CATEGORY);
if (empty($themeList)){
$this->error("模板文件不存在,请检查主题目录或主题配置");
}
$this->assign("template_list", $themeList);
return $this->fetch();
}
@ -134,14 +130,6 @@ class Category extends AuthController
*/
public function edit(Request $request): string
{
$templatePath = system_config('web_template');
$themeInfoFile = public_path('template' . DIRECTORY_SEPARATOR . $templatePath) . 'info.json';
if (file_exists($themeInfoFile)) {
$themeInfo = json_decode(file_get_contents($themeInfoFile), true);
$themeList = $themeInfo['category_list'];
} else {
$themeList = [];
}
$where = Util::postMore([
'name' => '',
'status' => '',
@ -151,6 +139,10 @@ class Category extends AuthController
$info = aModel::find($request->param(['id']));
$this->assign("category", $category);
$this->assign("info", $info);
$themeList = get_template_list(Data::DOCUMENT_CATEGORY);
if (empty($themeList)){
$this->error("模板文件不存在,请检查主题目录或主题配置");
}
$this->assign("template_list", $themeList);
return $this->fetch();
}

View File

@ -134,6 +134,9 @@ class Page extends AuthController
{
// 获取页面模板列表
$templateList = get_template_list(Data::DOCUMENT_TYPE_PAGE);
if (empty($themeList)){
$this->error("模板文件不存在,请检查主题目录或主题配置");
}
$this->assign("template_list", $templateList);
return $this->fetch();
}
@ -157,6 +160,9 @@ class Page extends AuthController
}
// 获取页面模板列表
$templateList = get_template_list(Data::DOCUMENT_TYPE_PAGE);
if (empty($themeList)){
$this->error("模板文件不存在,请检查主题目录或主题配置");
}
$this->assign("template_list", $templateList);
$this->assign("info", $info);
return $this->fetch();

View File

@ -63,14 +63,14 @@ class SystemConfig extends AuthController
public function clearCache(Request $request)
{
if ($request->isPost()) {
// $adminPath = config("cache.runtime") . "/admin/";
$adminPath = config("cache.runtime") . "/admin/temp";
$commonPath = config("cache.runtime") . "/cache/";
// $indexPath = config("cache.runtime") . "/index/";
// $apiPath = config("cache.runtime") . "/api/";
$indexPath = config("cache.runtime") . "/index/temp";
$apiPath = config("cache.runtime") . "/api/temp";
Cache::clear();
// remove_cache($adminPath);
// remove_cache($indexPath);
// remove_cache($apiPath);
remove_cache($adminPath);
remove_cache($indexPath);
remove_cache($apiPath);
remove_cache($commonPath);
return app("json")->success("操作成功");
}

View File

@ -38,9 +38,9 @@ class Theme extends AuthController
}
$info = json_decode(file_get_contents($themeInfoFile), true);
unset($themeInfoFile);
$info['root_dir'] = $themeDir . "/" . $file . '/';
$info['root_dir'] = $themeDir . "/" . $file ;
$info['dir_name'] = $file;
$info['preview_file'] = file_cdn($themeDir . "/" . $file . '/' . $info['preview_file']);
$info['preview_file'] = file_cdn($themeDir ."/". $file . '/' . $info['preview_file']);
$info['is_default'] = $file == $defaultTheme ? 1 : 0;//检查是不是默认主题
$themeList[] = $info;
}
@ -58,7 +58,7 @@ class Theme extends AuthController
* @author 木子的忧伤
* @date 2021-02-17 11:40
*/
public function change_theme(): string
public function change_theme()
{
$data = Util::postMore([
['value', ''],

View File

@ -41,7 +41,7 @@
<div class="form-group form-controls">
<select name="template" class="form-control">
{volist name="template_list" id="vo"}
<option value="{$vo.template}">{$vo.name}</option>
<option value="{$vo}">{$vo}</option>
{/volist}
</select>
</div>

View File

@ -42,7 +42,7 @@
<div class="form-group form-controls">
<select name="template" class="form-control">
{volist name="template_list" id="vo"}
<option value="{$vo.template}" {notempty name="info.template" }{if $vo.template== $info.template}selected{/if}{/notempty}>{$vo.name}</option>
<option value="{$vo}" {notempty name="info.template" }{if $vo == $info.template}selected{/if}{/notempty}>{$vo}</option>
{/volist}
</select>
</div>

View File

@ -22,7 +22,7 @@ class Ape extends TagLib
'article' => ['attr' => 'id,void,model', 'close' => 1],
'comment' => ['attr' => 'typeId,void,type,pageSize,orderBy', 'close' => 1],
'relevant' => ['attr' => 'id,model,void,row', 'close' => 1],
'tags' => ['attr' => 'tags,void', 'close' => 1],
'tags' => ['attr' => 'tags,void,row', 'close' => 1],
'archive' => ['attr' => 'type,format,void', 'close' => 1],
'nav' => ['attr' => 'type,typeId,row,void,where,orderBy,display', 'close' => 1],
'table' => ['attr' => 'name,where,orderby,row,pagesize,void', 'close' => 1]
@ -265,14 +265,12 @@ class Ape extends TagLib
*/
public function tagTags($tag, $content)
{
if (!isset($tag['tags'])) {
return false;
}
$tags = $tag['tags'];
$row = $tag['row'] ?? 10;
$tags = $tag['tags']??"";
$void = $tag['void'] ?? 'field';
$parse = '<?php ';
$parse .= '$__LIST__ =' . "tpl_get_tags_list($tags);";
$parse .= '$__LIST__ =' . "tpl_get_tags_list(\"$tags\",$row);";
$parse .= ' ?>';
$parse .= '{volist name="$__LIST__" id="' . $void . '"}';
$parse .= $content;

View File

@ -8,6 +8,7 @@ use app\common\model\Advert;
use app\common\model\FriendLink;
use app\common\model\Tag;
use app\common\model\Nav;
use app\common\model\Tag as TagModel;
use think\App;
use think\Collection;
use think\db\exception\DataNotFoundException;
@ -43,20 +44,55 @@ function get_document_category_list()
{
//缓存文章菜单
$documentCategory = cache(Data::DATA_DOCUMENT_CATEGORY_LIST);
$documentCategory = null;
if ($documentCategory === null) {
$documentCategoryList = DocumentCategory::where('status', 1)->order('sort asc')->select()->toArray();
//转换让id作为数组的键
$documentCategory = [];
foreach ($documentCategoryList as $item) {
$documentCategory = DocumentCategory::where('status', 1)->order('sort asc')->select()->toArray();
$documentList = Document::where('display', 1)
->where('status', 1)
->where("category_id","in",array_column($documentCategory,"id"))
->group("category_id")
->column('count(*) as count', 'category_id');
//TODO 需要实现包含下级分类所有数据的逻辑 使用下面两个函数实现,暂未完成
///转换让id作为数组的键
foreach ($documentCategory as &$item) {
//根据栏目类型生成栏目url
$item['url'] = make_category_url($item);
$documentCategory[$item['id']] = $item;
$item['dc_count'] = $documentList[$item['id']] ?? 0;
}
cache(Data::DATA_DOCUMENT_CATEGORY_LIST, $documentCategory);
}
return $documentCategory;
}
// 构建树形结构的函数
function buildTree($list, $pid = 0) {
$tree = [];
foreach ($list as $item) {
if ($item['pid'] == $pid) {
$children = buildTree($list, $item['id']);
if ($children) {
$item['children'] = $children;
}
$item['count'] += array_sum(array_column($children,"count"));
$tree[] = $item;
}
}
return $tree;
}
// 生成映射数据的函数
function generateMap($tree, &$map = []) {
foreach ($tree as $item) {
$map[$item['id']] = $item;
if (isset($item['children'])) {
generateMap($item['children'], $map);
}
}
return $map;
}
/**
* 获取一个文章分类
*/
@ -146,7 +182,7 @@ function tpl_get_channel($type, $typeId, $row = 100, $where = '', $orderby = '')
{
switch ($type) {
case "all":
//获取顶级分类
//获取所有
return get_document_category_all();
break;
case 'top':
@ -421,10 +457,18 @@ function tpl_get_list($orderBy, int $pageSize, $cid, $type, $table = 'article',
} else {
$documentListModel = $documentListModel->paginate($pageSize);
}
//获取评论数 跟在文章后面
if (!empty($item)){
$commentModel = (new Comment())->field('document_id as id,count(*) as comment')
->where("id","in" ,array_column($documentListModel->toArray(),'id'))
->group('id')
->select();
}
$lists = [];
foreach ($documentListModel as $key => $item) {
//生成文章url
$item['url'] = make_detail_url($item);
$item['comment'] = $commentModel[$item['id']]['comment'] ?? 0;
$lists[$key] = $item;
}
$re = [
@ -548,10 +592,18 @@ function tpl_get_article_list($cid, $row, $orderby, $table = 'article', $type =
}
$documentListModel = $documentListModel->order($orderby)->select();
//获取评论数 跟在文章后面
if (!empty($item)){
$commentModel = (new Comment())->field('document_id as id,count(*) as comment')
->where("id","in" ,array_column($documentListModel->toArray(),'id'))
->group('id')
->select();
}
$lists = [];
foreach ($documentListModel as $key => $item) {
//生成文章url
$item['url'] = make_detail_url($item);
$item['comment'] = $commentModel[$item['id']]['comment'] ?? 0;
$lists[$key] = $item;
}
return $lists;
@ -697,19 +749,18 @@ if (!function_exists('web_config')) {
* @date 2021-11-12 0:34
*/
if (!function_exists('tpl_get_tags_list')) {
function tpl_get_tags_list($tags)
function tpl_get_tags_list($tag ='', $limit = 10)
{
if (!$tags) {
return false;
$where =[
'name'=> $tag,
'page'=> 1,
'limit'=> $limit
];
try {
return TagModel::getList($where);
} catch (DbException $e) {
return [];
}
$tagArr = explode(',', $tags);
$tagTemp = [];
foreach ($tagArr as $item) {
$data['title'] = $item;
$data['url'] = url('/article/tag', ["t" => $item])->build();
array_push($tagTemp, $data);
}
return $tagTemp;
}
}
@ -1195,6 +1246,21 @@ function get_nav($x, $field = false)
}
}
function tpl_get_admin($id,$field=""){
$info = cache("admin_{$id}");
if (is_null($info)){
try {
//TODO 这里考虑是否会泄露用户信息问题,并且是否会影响性能,后续需要完善发帖数量等等
$info = \app\common\model\User::where('id', $id)->field('nickname,avatar,email,ip,remark')->find();
} catch (DbException $e) {
return "";
}
cache("admin_{$id}", $info, 1*60*60);
}
return $info[$field] ?? '';
}
/**
* 获取导航列表
*/

View File

@ -5,9 +5,10 @@
//第一步判断选择的模板
$theme = system_config('web_template');
//检查主题目录是否存在,不存在则更新为默认目录
if (file_exists(public_path("template/{$theme}"))) {
if (!file_exists(public_path("template/{$theme}"))) {
$theme = 'default';
}
//检查是否切换模板
if (is_mobile() == "wap" && file_exists(public_path("template/{$theme}/mobile"))) {
$mode = "mobile";

View File

@ -1,33 +1,9 @@
{
"title":"默认主题",
"author":"木子的忧伤",
"demo_url":"http://default.lcm.wang/",
"category_list":[
{
"name": "默认列表",
"template":"list_default.html"
},
{
"name": "两排列表",
"template":"list_double.html"
}
],
"article_list":[
{
"name": "普通文章",
"template":"page.html"
},
{
"name": "关于我们",
"template":"page_about.html"
},
{
"name": "产品文章",
"template":"page_product.html"
}
],
"info_file":"全部都是可视化的 没什么可说明的",
"update_url":"http://www.lcm.wang/",
"demo_url":"https://blog.apecloud.ltd/",
"description":"默认主题,精美响应式主题",
"update_url":"https://blog.apecloud.ltd/",
"preview_file":"preview.jpg",
"versions":"1.0"
}

View File

@ -131,7 +131,7 @@
</div>
<div class="item-post-meta-other">
<span><i class="fas fa-eye" aria-hidden="true"></i>{$field['view']}</span>
<!-- <span><i class="fas fa-comment-alt-lines"></i>0</span>-->
<span><i class="fas fa-comment-alt-lines"></i>{$field['comment']}</span>
</div>
</div>
</div>