forked from modstart/ModStartBlog
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMBlog.php
More file actions
704 lines (678 loc) · 21.2 KB
/
Copy pathMBlog.php
File metadata and controls
704 lines (678 loc) · 21.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
<?php
use Illuminate\Support\Facades\DB;
use Module\Blog\Model\Blog;
use Module\Blog\Util\BlogCategoryUtil;
use Module\Blog\Util\BlogTagUtil;
/**
* @Util 博客操作
* 1 统一用单数
*/
class MBlog
{
/**
* @Util 获取分类树
* @return array
*/
public static function categoryTree()
{
return BlogCategoryUtil::categoryTree();
}
/**
* @Util 获取最新博客
* @param $limit int 限制条数
* @return array 数组
* @returnExample
* [
* {
* "id": 19, // 博客ID
* "created_at": "2017-12-20 16:35:24", // 创建时间
* "updated_at": "2022-09-21 14:59:09", // 更新时间
* "title": "博客标题",
* "tag": [
* "标签1",
* "标签2"
* ],
* "summary": "博客摘要",
* "images": [
* "https://example.com/xxx.jpg" // 博客图片多张
* ],
* "content": "内容HTML",
* "isPublished": 1, // 是否发布
* "postTime": "2017-12-20 16:34:44", // 发布时间
* "clickCount": 215048, // 点击量
* "seoKeywords": "SEO关键词",
* "seoDescription": "SEO描述内容",
* "isTop": null, // 是否置顶
* "commentCount": 16, // 评论数
* "categoryId": 1, // 分类ID
* "_category": {
* "id": 1, // 分类ID
* "created_at": "2022-05-27 14:51:09", // 分类创建时间
* "updated_at": "2022-09-21 14:55:33", // 分类更新时间
* "pid": 0, // 父分类ID
* "sort": 1, // 分类排序
* "title": "分类名称",
* "blogCount": 1, // 分类下博客数量
* "cover": "https://xx.com/xx.jpg",
* "keywords": "分类关键词",
* "description": "分类描述"
* },
* "_cover": "https://xx.com/xx.jpg" // 博客封面,如果博客图片为空,会尝试自动从内容中抽取
* },
* // ...
* ]
*/
public static function latestBlog($limit)
{
$paginateData = self::paginateBlog(0, 1, $limit);
return $paginateData['records'];
}
/**
* @Util 获取置顶博客
* @param $limit int 限制条数
* @return array 数组
* @returnExample
* [
* {
* "id": 19, // 博客ID
* "created_at": "2017-12-20 16:35:24", // 创建时间
* "updated_at": "2022-09-21 14:59:09", // 更新时间
* "title": "博客标题",
* "tag": [
* "标签1",
* "标签2"
* ],
* "summary": "博客摘要",
* "images": [
* "https://example.com/xxx.jpg" // 博客图片多张
* ],
* "content": "内容HTML",
* "isPublished": 1, // 是否发布
* "postTime": "2017-12-20 16:34:44", // 发布时间
* "clickCount": 215048, // 点击量
* "seoKeywords": "SEO关键词",
* "seoDescription": "SEO描述内容",
* "isTop": null, // 是否置顶
* "commentCount": 16, // 评论数
* "categoryId": 1, // 分类ID
* "_category": {
* "id": 1, // 分类ID
* "created_at": "2022-05-27 14:51:09", // 分类创建时间
* "updated_at": "2022-09-21 14:55:33", // 分类更新时间
* "pid": 0, // 父分类ID
* "sort": 1, // 分类排序
* "title": "分类名称",
* "blogCount": 1, // 分类下博客数量
* "cover": "https://xx.com/xx.jpg",
* "keywords": "分类关键词",
* "description": "分类描述"
* },
* "_cover": "https://xx.com/xx.jpg" // 博客封面,如果博客图片为空,会尝试自动从内容中抽取
* },
* // ...
* ]
*/
public static function topBlog($limit)
{
$paginateData = self::paginateBlog(0, 1, $limit, [
'isTop' => true,
]);
return $paginateData['records'];
}
/**
* @param $limit
* @return mixed
* @deprecated delete at 2024-05-09
*/
public static function topestBlog($limit)
{
$paginateData = self::paginateBlog(0, 1, $limit, [
'isTop' => true,
]);
return $paginateData['records'];
}
/**
* @Util 获取热门博客
* @param $limit int 限制条数
* @return array 数组
* @returnExample
* [
* {
* "id": 19, // 博客ID
* "created_at": "2017-12-20 16:35:24", // 创建时间
* "updated_at": "2022-09-21 14:59:09", // 更新时间
* "title": "博客标题",
* "tag": [
* "标签1",
* "标签2"
* ],
* "summary": "博客摘要",
* "images": [
* "https://example.com/xxx.jpg" // 博客图片多张
* ],
* "content": "内容HTML",
* "isPublished": 1, // 是否发布
* "postTime": "2017-12-20 16:34:44", // 发布时间
* "clickCount": 215048, // 点击量
* "seoKeywords": "SEO关键词",
* "seoDescription": "SEO描述内容",
* "isTop": null, // 是否置顶
* "commentCount": 16, // 评论数
* "categoryId": 1, // 分类ID
* "_category": {
* "id": 1, // 分类ID
* "created_at": "2022-05-27 14:51:09", // 分类创建时间
* "updated_at": "2022-09-21 14:55:33", // 分类更新时间
* "pid": 0, // 父分类ID
* "sort": 1, // 分类排序
* "title": "分类名称",
* "blogCount": 1, // 分类下博客数量
* "cover": "https://xx.com/xx.jpg",
* "keywords": "分类关键词",
* "description": "分类描述"
* },
* "_cover": "https://xx.com/xx.jpg" // 博客封面,如果博客图片为空,会尝试自动从内容中抽取
* },
* // ...
* ]
*/
public static function hotBlog($limit)
{
$paginateData = self::paginateBlog(0, 1, $limit, [
'isHot' => true,
]);
return $paginateData['records'];
}
/**
* @Util 获取推荐博客
* @param $limit int 限制条数
* @return array 数组
* @returnExample
* [
* {
* "id": 19, // 博客ID
* "created_at": "2017-12-20 16:35:24", // 创建时间
* "updated_at": "2022-09-21 14:59:09", // 更新时间
* "title": "博客标题",
* "tag": [
* "标签1",
* "标签2"
* ],
* "summary": "博客摘要",
* "images": [
* "https://example.com/xxx.jpg" // 博客图片多张
* ],
* "content": "内容HTML",
* "isPublished": 1, // 是否发布
* "postTime": "2017-12-20 16:34:44", // 发布时间
* "clickCount": 215048, // 点击量
* "seoKeywords": "SEO关键词",
* "seoDescription": "SEO描述内容",
* "isTop": null, // 是否置顶
* "commentCount": 16, // 评论数
* "categoryId": 1, // 分类ID
* "_category": {
* "id": 1, // 分类ID
* "created_at": "2022-05-27 14:51:09", // 分类创建时间
* "updated_at": "2022-09-21 14:55:33", // 分类更新时间
* "pid": 0, // 父分类ID
* "sort": 1, // 分类排序
* "title": "分类名称",
* "blogCount": 1, // 分类下博客数量
* "cover": "https://xx.com/xx.jpg",
* "keywords": "分类关键词",
* "description": "分类描述"
* },
* "_cover": "https://xx.com/xx.jpg" // 博客封面,如果博客图片为空,会尝试自动从内容中抽取
* },
* // ...
* ]
*/
public static function recommendBlog($limit)
{
$paginateData = self::paginateBlog(0, 1, $limit, [
'isRecommend' => true,
]);
return $paginateData['records'];
}
/**
* @Util 最新博客评论
* @param $limit int 限制条数
* @return array
* @returnExample
* [
* {
* "id": 1, // 评论ID
* "created_at": "2022-09-21 14:59:09", // 创建时间
* "blog": {
* "id": 19, // 博客ID
* "title": "博客标题",
* "_url": "/blog/15" // 博客URL
* }
* },
* // ...
* ]
*/
public static function latestComment($limit)
{
return \Module\Blog\Util\BlogCommentUtil::latest($limit);
}
/**
* @Util 最新留言内容
* @param $limit int 限制条数
* @return array
* @returnExample
* [
* {
* "id": 5650,
* "created_at": "2022-05-28 11:06:23",
* "updated_at": "2022-05-28 11:06:28",
* "reply": null,
* "username": "我心飞扬",
* "email": "",
* "url": "",
* "content": "<p>或许对于世界,你是一个人;但对于我,你就是全世界。你在时,你是一切,你不在时,一切是你。</p>",
* "upCount": null,
* "downCount": null,
* "memberUserId": 0,
* "status": 2
* },
* // ...
* }
*/
public static function latestMessage($limit)
{
return \Module\Blog\Util\BlogMessageUtil::latest($limit);
}
/**
* @Util 获取最热博客
* @param $limit int 限制条数
* @return array 数组
* @returnExample
* [
* {
* "id": 19, // 博客ID
* "created_at": "2017-12-20 16:35:24", // 创建时间
* "updated_at": "2022-09-21 14:59:09", // 更新时间
* "title": "博客标题",
* "tag": [
* "标签1",
* "标签2"
* ],
* "summary": "博客摘要",
* "images": [
* "https://example.com/xxx.jpg" // 博客图片多张
* ],
* "content": "内容HTML",
* "isPublished": 1, // 是否发布
* "postTime": "2017-12-20 16:34:44", // 发布时间
* "clickCount": 215048, // 点击量
* "seoKeywords": "SEO关键词",
* "seoDescription": "SEO描述内容",
* "isTop": null, // 是否置顶
* "commentCount": 16, // 评论数
* "categoryId": 1, // 分类ID
* "_category": {
* "id": 1, // 分类ID
* "created_at": "2022-05-27 14:51:09", // 分类创建时间
* "updated_at": "2022-09-21 14:55:33", // 分类更新时间
* "pid": 0, // 父分类ID
* "sort": 1, // 分类排序
* "title": "分类名称",
* "blogCount": 1, // 分类下博客数量
* "cover": "https://xx.com/xx.jpg",
* "keywords": "分类关键词",
* "description": "分类描述"
* },
* "_cover": "https://xx.com/xx.jpg" // 博客封面,如果博客图片为空,会尝试自动从内容中抽取
* },
* // ...
* ]
*/
public static function hottestBlog($limit)
{
$paginateData = self::paginateBlog(0, 1, $limit, [
'order' => ['clickCount', 'desc'],
]);
return $paginateData['records'];
}
/**
* @Util 随机获取博客
* @param $limit int 限制条数
* @return array 数组
* @returnExample
* [
* {
* "id": 19, // 博客ID
* "created_at": "2017-12-20 16:35:24", // 创建时间
* "updated_at": "2022-09-21 14:59:09", // 更新时间
* "title": "博客标题",
* "tag": [
* "标签1",
* "标签2"
* ],
* "summary": "博客摘要",
* "images": [
* "https://example.com/xxx.jpg" // 博客图片多张
* ],
* "content": "内容HTML",
* "isPublished": 1, // 是否发布
* "postTime": "2017-12-20 16:34:44", // 发布时间
* "clickCount": 215048, // 点击量
* "seoKeywords": "SEO关键词",
* "seoDescription": "SEO描述内容",
* "isTop": null, // 是否置顶
* "commentCount": 16, // 评论数
* "categoryId": 1, // 分类ID
* "_category": {
* "id": 1, // 分类ID
* "created_at": "2022-05-27 14:51:09", // 分类创建时间
* "updated_at": "2022-09-21 14:55:33", // 分类更新时间
* "pid": 0, // 父分类ID
* "sort": 1, // 分类排序
* "title": "分类名称",
* "blogCount": 1, // 分类下博客数量
* "cover": "https://xx.com/xx.jpg",
* "keywords": "分类关键词",
* "description": "分类描述"
* },
* "_cover": "https://xx.com/xx.jpg" // 博客封面,如果博客图片为空,会尝试自动从内容中抽取
* },
* // ...
* ]
*/
public static function randomBlog($limit)
{
$paginateData = self::paginateBlog(0, 1, $limit, [
'order' => [\Illuminate\Support\Facades\DB::raw('RAND()'), ''],
]);
return $paginateData['records'];
}
/**
* @Util 获取博客分页
* @param $categoryId int 分类ID
* @param $page int 分页,默认为1
* @param $pageSize int 分页大小,默认为10
* @param $option array 分页高级参数
* @return array 数组
* @returnExample
* {
* "records": [
* {
* "id": 19,
* "created_at": "2017-12-20 16:35:24",
* "updated_at": "2022-09-21 14:59:09",
* "title": "博客标题",
* "tag": [
* "标签1",
* "标签2"
* ],
* "summary": "博客摘要",
* "images": [
* "https://example.com/xxx.jpg"
* ],
* "content": "内容HTML",
* "isPublished": 1,
* "postTime": "2017-12-20 16:34:44",
* "clickCount": 215048,
* "seoKeywords": "博客SEO关键词",
* "seoDescription": "博客SEO描述",
* "isTop": null,
* "commentCount": 16,
* "categoryId": 1,
* "_category": {
* "id": 1,
* "created_at": "2022-05-27 14:51:09",
* "updated_at": "2022-09-21 14:55:33",
* "pid": 0,
* "sort": 1,
* "title": "分类名称",
* "blogCount": 1,
* "cover": "https://xx.com/xx.jpg",
* "keywords": "分类关键词",
* "description": "分类描述"
* },
* "_cover": "https://xx.com/xx.jpg"
* },
* // ...
* ],
* "total": 1
* }
* @example
* // $option 说明
* // 发布时间倒序
* $option = [ 'order'=>['id', 'desc'] ];
* // 发布时间顺序
* $option = [ 'order'=>['id', 'asc'] ];
* // 增加检索条件
* $option = [ 'where'=>['id'=>1] ];
*/
public static function paginateBlog($categoryId, $page = 1, $pageSize = 10, $option = [])
{
return \Module\Blog\Util\BlogUtil::paginateBlogsByCategoryId($categoryId, $page, $pageSize, $option);
}
public static function buildRecords($records)
{
return \Module\Blog\Util\BlogUtil::buildRecords($records);
}
/**
* @Util 根据年份列出所有博客
* @param array $option
* @return array
* {
* "records": [
* {
* "id": 19,
* "created_at": "2017-12-20 16:35:24",
* "updated_at": "2022-09-21 14:59:09",
* "title": "博客标题",
* "tag": [
* "标签1",
* "标签2"
* ],
* "summary": "博客摘要",
* "images": [
* "https://example.com/xxx.jpg"
* ],
* "content": "内容HTML",
* "isPublished": 1,
* "postTime": "2017-12-20 16:34:44",
* "clickCount": 215048,
* "seoKeywords": "博客SEO关键词",
* "seoDescription": "博客SEO描述",
* "isTop": null,
* "commentCount": 16,
* "categoryId": 1,
* "_category": {
* "id": 1,
* "created_at": "2022-05-27 14:51:09",
* "updated_at": "2022-09-21 14:55:33",
* "pid": 0,
* "sort": 1,
* "title": "分类名称",
* "blogCount": 1,
* "cover": "https://xx.com/xx.jpg",
* "keywords": "分类关键词",
* "description": "分类描述"
* },
* "_cover": "https://xx.com/xx.jpg"
* },
* // ...
* ],
* "total": 1
* }
*/
public static function listBlogByYear($option = [])
{
$records = Blog::query()->where(['isPublished' => true])
->orderBy('id', 'desc')
->get(['id', 'images', 'tag', 'title', 'categoryId', 'created_at'])
->toArray();
$records = self::buildRecords($records);
$yearRecords = [];
foreach ($records as $i => $v) {
$year = date('Y', strtotime($v['created_at']));
if (!isset($yearRecords[$year])) {
$yearRecords[$year] = [
'count' => 0,
'year' => $year,
'records' => [],
];
}
$yearRecords[$year]['records'][] = $v;
}
foreach ($yearRecords as $i => $v) {
$yearRecords[$i]['count'] = count($v['records']);
}
return [
'total' => count($records),
'records' => $yearRecords,
];
}
/**
* @Util 获取分类信息
* @param $categoryId int 分类ID
* @return array 数组
* @returnExample
* {
* "id": 1, // 分类ID
* "created_at": "2022-05-27 14:51:09",
* "updated_at": "2022-09-21 14:55:33",
* "pid": 0,
* "sort": 1,
* "title": "分类名称",
* "blogCount": 1,
* "cover": "https://example.com/xxx.jpg",
* "keywords": "分类关键词",
* "description": "分类描述"
* }
*/
public static function getCategory($categoryId)
{
return BlogCategoryUtil::get($categoryId);
}
/**
* @Util 获取子分类信息
* @param $parentCategoryId int 分类ID
* @return array 数组
* @returnExample
* [
* {
* "id": 1, // 分类ID
* "created_at": "2022-05-27 14:51:09",
* "updated_at": "2022-09-21 14:55:33",
* "pid": 0,
* "sort": 1,
* "title": "分类名称",
* "blogCount": 1,
* "cover": "https://example.com/xxx.jpg",
* "keywords": "分类关键词",
* "description": "分类描述"
* },
* // ...
* ]
*/
public static function listChildCategories($parentCategoryId)
{
return BlogCategoryUtil::listChildCategories($parentCategoryId);
}
/**
* @Util 获取所有博客标签信息
* @param $limit int 限制数量,0为不限制
* @return array 数组,标签→数量映射
* @returnExample
* {
* "标签1": 1,
* "标签2": 2
* }
*/
public static function tags($limit = 0)
{
$records = BlogTagUtil::all();
if ($limit > 0) {
$records = array_slice($records, 0, $limit);
}
return $records;
}
/**
* @Util 获取所有二级分类的默认标签
* @param $limit int 限制数量,0为不限制
* @return array 数组,标签→数量映射
* @returnExample
* {
* "标签1": 5,
* "标签2": 3
* }
*/
public static function subcategoryTags($limit = 0)
{
return BlogCategoryUtil::getSubcategoryTags($limit);
}
/**
* @Util 获取所有博客标签信息
* @return array 数组
* @returnExample
* [
* {
* "name": "标签1",
* "count": 1
* }
* ]
*/
public static function tagRecords()
{
return BlogTagUtil::records();
}
/**
* @Util 按月获取归档数量
* @param $year number|string 年
* @return array 数组
* @returnExample
* [
* {
* "month": "06",
* "total": 1
* },
* {
* "month": "12",
* "total": 6
* }
* ]
*/
public static function archiveMonthCounts($year)
{
$archiveCounts = Blog::query()
->where('created_at', '<', date('Y-m-d H:i:s'))
->where('created_at', '>=', $year . '-01-01 00:00:00')
->where('created_at', '<=', $year . '-12-31 23:59:59')
->select([DB::raw("DATE_FORMAT(`created_at`,'%m') AS `month`"), DB::raw("COUNT(*) AS total")])
->groupBy('month')
->orderBy('month', 'desc')
->get()->toArray();
return $archiveCounts;
}
/**
* @Util 按年获取归档数量
* @return array 数组
* @returnExample
* [
* {
* "year": "06",
* "total": 1
* },
* {
* "year": "12",
* "total": 6
* }
* ]
*/
public static function archiveYearCounts()
{
$archiveCounts = Blog::published()
->select([DB::raw("DATE_FORMAT(`created_at`,'%Y') AS `year`"), DB::raw("COUNT(*) AS total")])
->groupBy('year')
->orderBy('year', 'desc')
->get()->toArray();
return $archiveCounts;
}
}