8 Star 112 Fork 35

modstart/ModStartBlog

Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
Clone or Download
MBlog.php 20.86 KB
Copy Edit Raw Blame History
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
<?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 获取所有博客标签信息
* @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;
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/modstart/ModStartBlog.git
git@gitee.com:modstart/ModStartBlog.git
modstart
ModStartBlog
ModStartBlog
master

Search

D67c1975 1850385 1daf7b77 1850385