Skip to content

Commit 5ee48ac

Browse files
committed
feat: 增加博客归档功能
1 parent e6fa797 commit 5ee48ac

File tree

170 files changed

+2166
-61732
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

170 files changed

+2166
-61732
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
4+
namespace Module\Blog\Api\Controller;
5+
6+
7+
use Illuminate\Routing\Controller;
8+
use ModStart\Core\Input\InputPackage;
9+
use ModStart\Core\Input\Response;
10+
use ModStart\Core\Util\ArrayUtil;
11+
use ModStart\Core\Util\TimeUtil;
12+
use Module\Blog\Model\Blog;
13+
14+
15+
class ArchiveController extends Controller
16+
{
17+
public function get()
18+
{
19+
$input = InputPackage::buildFromInput();
20+
$page = $input->getPage();
21+
$pageSize = 50;
22+
$year = $input->getInteger('year');
23+
if (empty($year)) {
24+
$year = date('Y');
25+
}
26+
$timeStart = date($year . '-01-01 00:00:00');
27+
$timeEnd = date($year . '-12-31 23:59:59');
28+
29+
$month = $input->getInteger('month');
30+
if ($month > 0 && $month <= 12) {
31+
$timeStart = date($year . '-' . $month . '-01 00:00:00');
32+
$timeEnd = date($year . '-' . $month . '-31 23:59:59');
33+
}
34+
35+
$result = Blog::where('postTime', '<', date('Y-m-d H:i:s'))
36+
->where('postTime', '>=', date('Y-m-d H:i:s', strtotime($timeStart)))
37+
->where('postTime', '<=', date('Y-m-d H:i:s', strtotime($timeEnd)))
38+
->orderBy('postTime', 'asc')
39+
->paginate($pageSize, ['id', 'title','isTop','isHot','isRecommend'], 'page', $page)->toArray();
40+
$paginateData = [
41+
'records' => $result['data'],
42+
'total' => $result['total'],
43+
];
44+
45+
$pageTitle = $year . '';
46+
if ($month > 0 && $month <= 12) {
47+
$pageTitle .= $month . '';
48+
}
49+
$pageTitle .= '博客归档';
50+
$pageKeywords = $pageTitle . ',博客归档';
51+
$pageDescription = $pageTitle . ',共' . $paginateData['total'] . '篇文章';
52+
53+
$yearCounts = \MBlog::archiveYearCounts();
54+
$monthCounts = \MBlog::archiveMonthCounts($year);
55+
$yearCount = array_sum(ArrayUtil::flatItemsByKey($monthCounts, 'total'));
56+
57+
$data = [];
58+
$data['page'] = $page;
59+
$data['pageSize'] = $pageSize;
60+
$data['records'] = $paginateData['records'];
61+
$data['total'] = $paginateData['total'];
62+
63+
$data['year'] = $year;
64+
$data['month'] = $month;
65+
$data['monthCounts'] = $monthCounts;
66+
$data['yearCount'] = $yearCount;
67+
$data['yearCounts'] = $yearCounts;
68+
$data['pageTitle'] = $pageTitle;
69+
$data['pageKeywords'] = $pageKeywords;
70+
$data['pageDescription'] = $pageDescription;
71+
72+
return Response::generateSuccessData($data);
73+
}
74+
}

module/Blog/Core/MBlog.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

3+
use Illuminate\Support\Facades\DB;
34
use ModStart\Core\Assets\AssetsUtil;
45
use ModStart\Core\Dao\ModelUtil;
56
use ModStart\Core\Util\HtmlUtil;
@@ -174,4 +175,30 @@ public static function tagRecords()
174175
{
175176
return BlogTagUtil::records();
176177
}
178+
179+
180+
public static function archiveMonthCounts($year)
181+
{
182+
$archiveCounts = Blog::query()
183+
->where('postTime', '<', date('Y-m-d H:i:s'))
184+
->where('postTime', '>=', $year . '-01-01 00:00:00')
185+
->where('postTime', '<=', $year . '-12-31 23:59:59')
186+
->select([DB::raw("DATE_FORMAT(`postTime`,'%m') AS `month`"), DB::raw("COUNT(*) AS total")])
187+
->groupBy('month')
188+
->orderBy('month', 'desc')
189+
->get()->toArray();
190+
return $archiveCounts;
191+
}
192+
193+
194+
public static function archiveYearCounts()
195+
{
196+
$archiveCounts = Blog::query()
197+
->where('postTime', '<', date('Y-m-d H:i:s'))
198+
->select([DB::raw("DATE_FORMAT(`postTime`,'%Y') AS `year`"), DB::raw("COUNT(*) AS total")])
199+
->groupBy('year')
200+
->orderBy('year', 'desc')
201+
->get()->toArray();
202+
return $archiveCounts;
203+
}
177204
}

module/Blog/Core/ModuleServiceProvider.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public function boot(Dispatcher $events)
3434
['博客首页', modstart_web_url('blog')],
3535
['博客列表', modstart_web_url('blogs')],
3636
['博客留言', modstart_web_url('blog/message')],
37+
['博客归档', modstart_web_url('blog/archive')],
3738
['博客标签', modstart_web_url('blog/tags')],
3839
['关于博主', modstart_web_url('blog/about')],
3940
], $categories));

module/Blog/Docs/module/content.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
图文博客
88
博客分类
99
博客标签
10+
博客归档
11+
博客评论
1012
友情链接
1113
留言本
1214
关于博主

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.0.0
2+
3+
- 新增:博客归档页面和归档功能
4+
5+
---
6+
17
## 2.9.0 优化:博客主题色自动切换优化
28

39
- 优化:博客主题色自动切换优化
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
@extends($_viewFrame)
2+
3+
@section('pageTitle'){{$pageTitle?$pageTitle.' | '.modstart_config('siteName'):modstart_config('siteName')}}@endsection
4+
@section('pageKeywords'){{$pageKeywords}}@endsection
5+
@section('pageDescription'){{$pageDescription}}@endsection
6+
7+
{!! \ModStart\ModStart::js('asset/common/scrollAnimate.js') !!}
8+
9+
@include('module::Blog.View.pc.blog.inc.theme')
10+
11+
@section('bodyContent')
12+
13+
<div class="ub-container margin-top">
14+
<div class="row">
15+
<div class="col-md-8">
16+
<div class="ub-content-box margin-bottom">
17+
<div class="tw-p-3">
18+
<a href="?{{\ModStart\Core\Input\Request::mergeQueries(['month'=>null])}}"
19+
class="btn btn-round {{!$month?'btn-primary':''}}">
20+
{{$year}}年全部({{$yearCount}})
21+
</a>
22+
@foreach($monthCounts as $mc)
23+
<a href="?{{\ModStart\Core\Input\Request::mergeQueries(['month'=>$mc['month']])}}"
24+
class="btn btn-round {{$month==$mc['month']?'btn-primary':''}}">
25+
{{$mc['month']}}月({{$mc['total']}})
26+
</a>
27+
@endforeach
28+
</div>
29+
</div>
30+
<div class="ub-content-box margin-bottom">
31+
<div class="tw-p-3">
32+
33+
@include('module::Blog.View.pc.blog.inc.blogItemsTitle')
34+
35+
<div>
36+
<div class="ub-page">
37+
{!! $pageHtml !!}
38+
</div>
39+
</div>
40+
</div>
41+
</div>
42+
</div>
43+
<div class="col-md-4 margin-bottom">
44+
45+
<div class="ub-content-box margin-bottom">
46+
<div class="tw-p-3">
47+
<div class="tw-text-lg">
48+
<i class="iconfont icon-category"></i>
49+
年份归档
50+
</div>
51+
<div class="tw-mt-4">
52+
<div class="row">
53+
@foreach($yearCounts as $yc)
54+
<div class="col-12">
55+
<a href="{{modstart_web_url('blog/archive',['year'=>$yc['year']])}}"
56+
class="hover:tw-shadow tw-block ub-content-block tw-rounded tw-leading-10 tw-mb-3 tw-px-2 tw-truncate @if($year==$yc['year']) ub-text-primary @endif">
57+
<i class="iconfont icon-angle-right ub-text-muted"></i>
58+
{{$yc['year']}}
59+
{{$yc['total']}}
60+
</a>
61+
</div>
62+
@endforeach
63+
</div>
64+
</div>
65+
</div>
66+
</div>
67+
68+
69+
</div>
70+
</div>
71+
</div>
72+
73+
@endsection
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
@if(empty($records))
2+
<div class="ub-empty">
3+
<div class="icon">
4+
<div class="iconfont icon-empty-box"></div>
5+
</div>
6+
<div class="text">
7+
暂无数据
8+
</div>
9+
</div>
10+
@endif
11+
@foreach($records as $record)
12+
<div class="ub-text-blog tw-border-0 ub-border-bottom tw-py-2">
13+
<div>
14+
<div class="lg:tw-truncate">
15+
<i class="iconfont icon-dot-sm"></i>
16+
@if($record['isTop'])
17+
<span class="tw tw-align-top tw-inline-block tw-leading-6 tw-px-3 tw-rounded tw-bg-blue-100 tw-text-blue-500">置顶</span>
18+
@endif
19+
@if($record['isHot'])
20+
<span class="tw tw-align-top tw-inline-block tw-leading-6 tw-px-3 tw-rounded tw-bg-red-100 tw-text-red-500">
21+
<i class="fa fa-fire"></i>
22+
热门
23+
</span>
24+
@endif
25+
@if($record['isRecommend'])
26+
<span class="tw tw-align-top tw-inline-block tw-leading-6 tw-px-3 tw-rounded tw-bg-yellow-100 tw-text-yellow-500">
27+
<i class="iconfont icon-thumb-up"></i>
28+
推荐
29+
</span>
30+
@endif
31+
<a href="{{modstart_web_url('blog/'.$record['id'])}}"
32+
class="pb-keywords-highlight tw-align-top tw-inline-block tw-leading-6 default">
33+
{{$record['title']}}
34+
</a>
35+
</div>
36+
</div>
37+
</div>
38+
@endforeach
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
4+
namespace Module\Blog\Web\Controller;
5+
6+
7+
use ModStart\Core\Input\Request;
8+
use ModStart\Core\Input\Response;
9+
use ModStart\Core\Util\PageHtmlUtil;
10+
use ModStart\Module\ModuleBaseController;
11+
12+
class ArchiveController extends ModuleBaseController
13+
{
14+
public function index(\Module\Blog\Api\Controller\ArchiveController $api)
15+
{
16+
$viewData = Response::tryGetData($api->get());
17+
$viewData['pageHtml'] = PageHtmlUtil::render($viewData['total'], $viewData['pageSize'], $viewData['page'], '?' . Request::mergeQueries(['page' => ['{page}']]));
18+
return $this->view('blog.archive', $viewData);
19+
}
20+
21+
}

module/Blog/Web/routes.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
$router->match(['get'], 'blog/about', 'AboutController@index');
1414
$router->match(['get'], 'blog/message', 'MessageController@index');
1515
$router->match(['get'], 'blog/tags', 'TagsController@index');
16+
$router->match(['get'], 'blog/archive', 'ArchiveController@index');
1617

1718
$router->match(['get'], 'blogs', 'BlogController@index');
1819
$router->match(['get'], 'blog/{id}', 'BlogController@show');

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": "2.9.0",
20+
"version": "3.0.0",
2121
"author": "ModStart",
2222
"description": "提供一个基础的博客系统",
2323
"suggest": [

0 commit comments

Comments
 (0)