Skip to content

Commit 2fa0b32

Browse files
committed
新增:博客分类支持二级分类,博客列表页面支持二级分类筛选
1 parent 1c1955b commit 2fa0b32

File tree

28 files changed

+281
-74
lines changed

28 files changed

+281
-74
lines changed

module/Blog/Admin/Controller/BlogCategoryController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,6 @@ protected function crud(AdminCRUDBuilder $builder)
4242
})
4343
->title('博客分类')
4444
->asTree()
45-
->treeMaxLevel(1);
45+
->treeMaxLevel(2);
4646
}
4747
}

module/Blog/Api/Controller/BlogController.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,12 @@ public function paginate()
103103
}
104104
}
105105

106-
107106
$category = null;
107+
$childCategories = [];
108+
$categoryChain = BlogCategoryUtil::categoryChainWithItems($categoryId);
108109
if ($categoryId > 0) {
109110
$category = \MBlog::getCategory($categoryId);
111+
$childCategories = \MBlog::listChildCategories($categoryId);
110112
BizException::throwsIfEmpty('分类不存在', $category);
111113
$pageTitle = $category['title'];
112114
$pageKeywords = $category['keywords'];
@@ -119,6 +121,8 @@ public function paginate()
119121
'page' => $page,
120122
'pageSize' => $pageSize,
121123
'category' => $category,
124+
'childCategories' => $childCategories,
125+
'categoryChain' => $categoryChain,
122126
'keywords' => $keywords,
123127
'markKeywords' => $markKeywords,
124128
'records' => $paginateData['records'],

module/Blog/Core/MBlog.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,32 @@ public static function getCategory($categoryId)
573573
return BlogCategoryUtil::get($categoryId);
574574
}
575575

576+
/**
577+
* @Util 获取子分类信息
578+
* @param $parentCategoryId int 分类ID
579+
* @return array 数组
580+
* @returnExample
581+
* [
582+
* {
583+
* "id": 1, // 分类ID
584+
* "created_at": "2022-05-27 14:51:09",
585+
* "updated_at": "2022-09-21 14:55:33",
586+
* "pid": 0,
587+
* "sort": 1,
588+
* "title": "分类名称",
589+
* "blogCount": 1,
590+
* "cover": "https://example.com/xxx.jpg",
591+
* "keywords": "分类关键词",
592+
* "description": "分类描述"
593+
* },
594+
* // ...
595+
* ]
596+
*/
597+
public static function listChildCategories($parentCategoryId)
598+
{
599+
return BlogCategoryUtil::listChildCategories($parentCategoryId);
600+
}
601+
576602
/**
577603
* @Util 获取所有博客标签信息
578604
* @param $limit int 限制数量,0为不限制

module/Blog/Docs/release.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## 3.5.0
2+
3+
- 新增:博客分类支持二级分类,博客列表页面支持二级分类筛选
4+
5+
---
6+
17
## 3.4.0 搜索中心重建,首页SEO标题、描述可独立设置
28

39
- 新增:搜索中心内容索引重建统一入口,安装 SuperSearch 模块

module/Blog/Util/BlogCategoryUtil.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public static function clearCache()
1919
public static function all()
2020
{
2121
return Cache::rememberForever('BlogCategories', function () {
22-
$records = ModelUtil::all('blog_category', [], ['*'], ['sort', 'desc']);
22+
$records = ModelUtil::all('blog_category', [], ['*'], ['sort', 'asc']);
2323
AssetsUtil::recordsFixFullOrDefault($records, 'cover', 'asset/image/none.png');
2424
foreach ($records as $k => $v) {
2525
$records[$k]['_url'] = UrlUtil::category($v);
@@ -34,6 +34,12 @@ public static function categoryTree()
3434
return TreeUtil::nodesToTree($nodes);
3535
}
3636

37+
public static function categoryChainWithItems($categoryId)
38+
{
39+
$records = self::all();
40+
return TreeUtil::nodesChainWithItems($records, $categoryId);
41+
}
42+
3743
public static function get($id)
3844
{
3945
foreach (self::all() as $one) {
@@ -44,6 +50,15 @@ public static function get($id)
4450
return null;
4551
}
4652

53+
public static function listChildCategories($categoryId)
54+
{
55+
$records = self::all();
56+
$records = array_filter($records, function ($item) use ($categoryId) {
57+
return $item['pid'] == $categoryId;
58+
});
59+
return array_values($records);
60+
}
61+
4762
public static function childrenIds($categoryId)
4863
{
4964
if ($categoryId <= 0) {

module/Blog/Util/UrlUtil.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,17 @@ class UrlUtil
88
{
99
public static function category($category)
1010
{
11-
return modstart_web_url('blogs', [
12-
'categoryId' => $category['id'],
13-
]);
11+
if (is_array($category)) {
12+
return modstart_web_url('blogs', [
13+
'categoryId' => $category['id'],
14+
]);
15+
}
16+
if ($category > 0) {
17+
return modstart_web_url('blogs', [
18+
'categoryId' => $category,
19+
]);
20+
}
21+
return modstart_web_url('blogs');
1422
}
1523

1624
public static function blog($blog)

module/Blog/View/pc/blog/inc/categories.blade.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
@foreach(\MBlog::categoryTree() as $t)
1010
<div class="col-6">
1111
<a href="{{modstart_web_url('blogs',['categoryId'=>$t['id']])}}"
12-
class="hover:tw-shadow tw-block ub-content-block tw-rounded tw-leading-10 tw-mb-3 tw-px-2 tw-truncate @if(!empty($category)&&$category['id']==$t['id']) ub-bg-primary @endif">
13-
<i class="iconfont icon-angle-right ub-text-muted"></i>
12+
class="hover:tw -shadow btn btn-block btn-round tw-mb-3 tw-px-2 tw-truncate @if(!empty($category)&&$category['id']==$t['id']) btn-primary @endif">
13+
<i class="iconfont icon-angle-right"></i>
1414
{{$t['title']}}
1515
{{$t['blogCount']?$t['blogCount']:0}}
1616
</a>

module/Blog/View/pc/blog/list.blade.php

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,25 @@
3232
</div>
3333
</div>
3434
@endif
35-
@if($category)
36-
<div class="ub-content-box margin-bottom">
37-
<div class="tw-p-3">
38-
<i class="iconfont icon-category"></i>
39-
<a href="{{modstart_web_url('blogs')}}" class="tw-text-lg tw-text-gray-400">
40-
全部
41-
</a>
42-
<i class="iconfont icon-angle-right ub-text-muted"></i>
43-
<a href="{{modstart_web_url('blogs',['categoryId'=>$category])}}" class="tw-text-lg tw-text-gray-800">
44-
{{$category['title']}}
45-
</a>
46-
</div>
35+
<div class="ub-content-box margin-bottom">
36+
<div class="tw-px-3 tw-pt-3">
37+
@foreach($categoryChain as $catIndex=>$catItem)
38+
<div class="margin-bottom">
39+
<a href="?{{\ModStart\Core\Input\Request::mergeQueries(['categoryId'=>$catItem['pid']?$catItem['pid']:null])}}"
40+
class="btn btn-round @if($catItem['id']<=0) btn-primary @endif">
41+
全部
42+
</a>
43+
@foreach($catItem['_items'] as $item)
44+
<a href="?{{\ModStart\Core\Input\Request::mergeQueries(['categoryId'=>$item['id']?$item['id']:null])}}"
45+
class="btn btn-round @if($catItem['id']==$item['id']) btn-primary @endif"
46+
>
47+
{{$item['title']}}
48+
</a>
49+
@endforeach
50+
</div>
51+
@endforeach
4752
</div>
48-
@endif
53+
</div>
4954
<div class="ub-content-box margin-bottom">
5055
<div class="tw-p-3">
5156

module/Blog/config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"tags": [
1818
"博客"
1919
],
20-
"version": "3.4.0",
20+
"version": "3.5.0",
2121
"author": "ModStart",
2222
"description": "提供一个基础的博客系统",
2323
"suggest": [

module/Vendor/Command/ScheduleRunAllCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ function () use ($dir) {
4040
$start = TimeUtil::millitime();
4141
$command = "$php {$project['pathname']}/artisan schedule:run";
4242
Log::info("Vendor.ScheduleRunAllCommand.Run - {$command}");
43-
$result = ShellUtil::run($command, false);
43+
$result = ShellUtil::runInNewProcess($command, false);
4444
$result = str_replace([
4545
"\r"
4646
], "", $result);

0 commit comments

Comments
 (0)