Skip to content

Commit 6d5ed88

Browse files
committed
优化:博客自动发布逻辑优化调整,支持定时发布
1 parent 43c814e commit 6d5ed88

File tree

118 files changed

+150
-3492
lines changed

Some content is hidden

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

118 files changed

+150
-3492
lines changed

module/Blog/Admin/Controller/BlogController.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,15 @@ protected function crud(AdminCRUDBuilder $builder)
5757
$builder->images('images', '图片')->listable(false);
5858
$builder->text('seoKeywords', 'SEO关键词')->listable(false);
5959
$builder->textarea('seoDescription', 'SEO描述')->listable(false);
60-
$builder->datetime('postTime', '发布时间')->defaultValue(date('Y-m-d H:i:s'));
6160
$builder->switch('isTop', '置顶')->gridEditable(true);
6261
$builder->switch('isHot', '热门')->gridEditable(true);
6362
$builder->switch('isRecommend', '推荐')->gridEditable(true);
64-
$builder->switch('isPublished', '发布')->optionsYesNo()->defaultValue(true);
63+
$builder->switch('isPublished', '发布')
64+
->optionsYesNo()->defaultValue(true)
65+
->when('=', false, function ($builder) {
66+
/** @var HasFields $builder */
67+
$builder->datetime('postTime', '发布时间')->defaultValue(date('Y-m-d H:i:s'));
68+
});
6569
$builder->radio('visitMode', '访问模式')
6670
->optionType(BlogVisitMode::class)
6771
->defaultValue(BlogVisitMode::OPEN)

module/Blog/Api/Controller/ArchiveController.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ public function get()
3434
$timeEnd = date($year . '-' . $month . '-31 23:59:59');
3535
}
3636

37-
$result = Blog::where('postTime', '<', date('Y-m-d H:i:s'))
38-
->where('postTime', '>=', date('Y-m-d H:i:s', strtotime($timeStart)))
39-
->where('postTime', '<=', date('Y-m-d H:i:s', strtotime($timeEnd)))
40-
->orderBy('postTime', 'asc')
41-
->paginate($pageSize, ['id', 'title','isTop','isHot','isRecommend'], 'page', $page)->toArray();
37+
$result = Blog::where('created_at', '<', date('Y-m-d H:i:s'))
38+
->where('created_at', '>=', date('Y-m-d H:i:s', strtotime($timeStart)))
39+
->where('created_at', '<=', date('Y-m-d H:i:s', strtotime($timeEnd)))
40+
->orderBy('id', 'asc')
41+
->paginate($pageSize, ['id', 'title', 'isTop', 'isHot', 'isRecommend'], 'page', $page)->toArray();
4242
$paginateData = [
4343
'records' => $result['data'],
4444
'total' => $result['total'],

module/Blog/Api/Controller/BlogController.php

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use ModStart\Core\Util\HtmlUtil;
1616
use ModStart\Core\Util\TagUtil;
1717
use Module\Blog\Core\BlogSuperSearchBiz;
18+
use Module\Blog\Model\Blog;
1819
use Module\Blog\Type\BlogCommentStatus;
1920
use Module\Blog\Type\BlogVisitMode;
2021
use Module\Blog\Util\BlogCategoryUtil;
@@ -61,11 +62,9 @@ public function paginate()
6162
} else {
6263
$query = [
6364
['isPublished' => ['eq' => 1]],
64-
['postTime' => ['lt' => date('Y-m-d H:i:s')]],
6565
];
6666
$order = [
6767
['isTop', 'desc'],
68-
['postTime', 'desc'],
6968
['id', 'desc'],
7069
];
7170
if ($categoryId) {
@@ -138,13 +137,14 @@ public function get()
138137
{
139138
$input = InputPackage::buildFromInput();
140139
$id = $input->getInteger('id');
141-
$record = ModelUtil::get('blog', ['id' => $id]);
140+
$record = Blog::published()->where(['id' => $id])->first();
142141
BizException::throwsIfEmpty('记录不存在', $record);
142+
$record = $record->toArray();
143143
ModelUtil::decodeRecordJson($record, ['images']);
144144
$record['images'] = AssetsUtil::fixFull($record['images']);
145145
$record['tag'] = TagUtil::string2Array($record['tag']);
146146
$record['_category'] = BlogCategoryUtil::get($record['categoryId']);
147-
$record['_date'] = date('Y-m-d', strtotime($record['postTime']));
147+
$record['_date'] = date('Y-m-d', strtotime($record['created_at']));
148148
$summary = $record['seoDescription'];
149149
$images = $record['images'];
150150
if (isset($record['content'])) {
@@ -209,18 +209,16 @@ public function get()
209209
}
210210

211211

212-
$recordNext = ModelUtil::model('blog')
213-
->where('postTime', '<', $record['postTime'])
214-
->orderBy('postTime', 'desc')
212+
$recordNext = Blog::published()
213+
->orderBy('id', 'desc')
215214
->limit(1)->first();
216215
if ($recordNext) {
217216
$recordNext = ArrayUtil::keepKeys($recordNext->toArray(), ['id', 'title']);
218217
$recordNext['_url'] = UrlUtil::blog($recordNext);
219218
}
220219

221-
$recordPrev = ModelUtil::model('blog')
222-
->where('postTime', '>', $record['postTime'])
223-
->orderBy('postTime', 'desc')
220+
$recordPrev = Blog::published()
221+
->orderBy('id', 'asc')
224222
->limit(1)->first();
225223
if ($recordPrev) {
226224
$recordPrev = ArrayUtil::keepKeys($recordPrev->toArray(), ['id', 'title']);
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Module\Blog\Core;
4+
5+
use Module\Blog\Model\Blog;
6+
use Module\Vendor\Provider\Schedule\AbstractScheduleBiz;
7+
8+
class BlogAutoPostScheduleBiz extends AbstractScheduleBiz
9+
{
10+
const NAME = 'Blog';
11+
12+
public function cron()
13+
{
14+
return $this->cronEveryMinute();
15+
}
16+
17+
public function title()
18+
{
19+
return '博客自动发布';
20+
}
21+
22+
public function run()
23+
{
24+
Blog::unPublished()
25+
->where('postTime', '<=', date('Y-m-d H:i:s'))
26+
->update(['isPublished' => true]);
27+
}
28+
29+
}

module/Blog/Core/BlogSuperSearchBiz.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ public function fields()
3737
'id' => ['type' => FieldTypes::F_LONG],
3838
'isPublished' => ['type' => FieldTypes::F_LONG],
3939
'isTop' => ['type' => FieldTypes::F_LONG],
40-
'postTime' => ['type' => FieldTypes::F_DATETIME],
4140
'categoryId' => ['type' => FieldTypes::F_LONG],
4241
'title' => ['type' => FieldTypes::F_TEXT],
4342
'summary' => ['type' => FieldTypes::F_TEXT],
@@ -83,7 +82,6 @@ public static function syncUpsert($records, $checkExists = true)
8382
'id' => intval($record['id']),
8483
'isPublished' => intval($record['isPublished']),
8584
'isTop' => intval($record['isTop']),
86-
'postTime' => $record['postTime'],
8785
'categoryId' => intval($record['categoryId']),
8886
'title' => $record['title'],
8987
'summary' => $record['summary'],

module/Blog/Core/MBlog.php

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -455,9 +455,9 @@ public static function randomBlog($limit)
455455
* @example
456456
* // $option 说明
457457
* // 发布时间倒序
458-
* $option = [ 'order'=>['postTime', 'desc'] ];
458+
* $option = [ 'order'=>['id', 'desc'] ];
459459
* // 发布时间顺序
460-
* $option = [ 'order'=>['postTime', 'asc'] ];
460+
* $option = [ 'order'=>['id', 'asc'] ];
461461
* // 增加检索条件
462462
* $option = [ 'where'=>['id'=>1] ];
463463
*/
@@ -522,15 +522,14 @@ public static function listBlogByYear($option = [])
522522
{
523523

524524
$records = Blog::query()->where(['isPublished' => true])
525-
->where('postTime', '<', date('Y-m-d H:i:s'))
526-
->orderBy('postTime', 'desc')
527-
->get(['id', 'images', 'tag', 'title', 'categoryId', 'postTime'])
525+
->orderBy('id', 'desc')
526+
->get(['id', 'images', 'tag', 'title', 'categoryId', 'created_at'])
528527
->toArray();
529528
$records = self::buildRecords($records);
530529

531530
$yearRecords = [];
532531
foreach ($records as $i => $v) {
533-
$year = date('Y', strtotime($v['postTime']));
532+
$year = date('Y', strtotime($v['created_at']));
534533
if (!isset($yearRecords[$year])) {
535534
$yearRecords[$year] = [
536535
'count' => 0,
@@ -653,10 +652,10 @@ public static function tagRecords()
653652
public static function archiveMonthCounts($year)
654653
{
655654
$archiveCounts = Blog::query()
656-
->where('postTime', '<', date('Y-m-d H:i:s'))
657-
->where('postTime', '>=', $year . '-01-01 00:00:00')
658-
->where('postTime', '<=', $year . '-12-31 23:59:59')
659-
->select([DB::raw("DATE_FORMAT(`postTime`,'%m') AS `month`"), DB::raw("COUNT(*) AS total")])
655+
->where('created_at', '<', date('Y-m-d H:i:s'))
656+
->where('created_at', '>=', $year . '-01-01 00:00:00')
657+
->where('created_at', '<=', $year . '-12-31 23:59:59')
658+
->select([DB::raw("DATE_FORMAT(`created_at`,'%m') AS `month`"), DB::raw("COUNT(*) AS total")])
660659
->groupBy('month')
661660
->orderBy('month', 'desc')
662661
->get()->toArray();
@@ -680,9 +679,8 @@ public static function archiveMonthCounts($year)
680679
*/
681680
public static function archiveYearCounts()
682681
{
683-
$archiveCounts = Blog::query()
684-
->where('postTime', '<', date('Y-m-d H:i:s'))
685-
->select([DB::raw("DATE_FORMAT(`postTime`,'%Y') AS `year`"), DB::raw("COUNT(*) AS total")])
682+
$archiveCounts = Blog::published()
683+
->select([DB::raw("DATE_FORMAT(`created_at`,'%Y') AS `year`"), DB::raw("COUNT(*) AS total")])
686684
->groupBy('year')
687685
->orderBy('year', 'desc')
688686
->get()->toArray();

module/Blog/Core/ModuleServiceProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Module\Vendor\Admin\Widget\AdminWidgetDashboard;
2323
use Module\Vendor\Admin\Widget\AdminWidgetLink;
2424
use Module\Vendor\Provider\HomePage\HomePageProvider;
25+
use Module\Vendor\Provider\Schedule\ScheduleBiz;
2526
use Module\Vendor\Provider\SearchBox\SearchBoxProvider;
2627
use Module\Vendor\Provider\SiteUrl\SiteUrlBiz;
2728
use Module\Vendor\Provider\SuperSearch\SuperSearchBiz;
@@ -128,6 +129,7 @@ public function boot(Dispatcher $events)
128129
}
129130
});
130131
}
132+
ScheduleBiz::register(BlogAutoPostScheduleBiz::class);
131133
}
132134

133135
/**

module/Blog/Docs/release.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
## 3.5.0
22

3+
- 优化:博客自动发布逻辑优化调整,支持定时发布
34
- 新增:博客分类支持二级分类,博客列表页面支持二级分类筛选
5+
- 修复:博客编辑状态下可浏览,标签和博客分类数量计算数量异常
6+
47

58
---
69

module/Blog/Migrate/2017_01_01_000000_create_blog.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function up()
3030
$table->timestamp('postTime')->nullable()->comment('发布时间');
3131
$table->integer('clickCount')->nullable()->comment('点击数');
3232

33-
$table->index(['postTime']);
33+
$table->index(['created_at']);
3434

3535
});
3636
}

module/Blog/Model/Blog.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,19 @@
99
class Blog extends Model
1010
{
1111
protected $table = 'blog';
12+
13+
14+
public static function published()
15+
{
16+
return self::where([
17+
'isPublished' => true,
18+
]);
19+
}
20+
21+
public static function unPublished()
22+
{
23+
return self::where([
24+
'isPublished' => false,
25+
]);
26+
}
1227
}

0 commit comments

Comments
 (0)