更新后端接口

This commit is contained in:
2025-12-25 11:41:56 +08:00
parent 2161113eae
commit 2e89705815
2 changed files with 550 additions and 1 deletions
+138
View File
@@ -170,6 +170,144 @@ class ArticlesController extends BaseController
}
}
// 获取technicalArticles分类
public function getTechnicalArticlesCategory()
{
// 获取名为 '技术文章' 的分类
$siteInfoCategory = ArticlesCategory::where('name', '技术文章')
->where('delete_time', null)
->where('status', 1)
->field('id,cid,name,image,sort')
->find();
if (!$siteInfoCategory) {
return json([
'code' => 1,
'msg' => '未找到技术文章分类'
]);
}
// 只返回其子分类,cid等于'技术文章'的id
$children = ArticlesCategory::where('cid', $siteInfoCategory['id'])
->where('delete_time', null)
->where('status', 1)
->field('id,cid,name,image,sort')
->select()
->toArray();
return json([
'code' => 0,
'msg' => '获取技术文章子分类成功',
'data' => $children
]);
}
// 获取technicalArticles内容,传参
public function getTechnicalArticlesLists()
{
try {
// 获取前端传递的分类ID
$cateid = input('cateid/d', 0);
// 验证分类ID
if ($cateid <= 0) {
return json([
'code' => 1,
'msg' => '分类ID不能为空'
]);
}
// 检查分类是否存在且有效
$category = ArticlesCategory::where('id', $cateid)
->where('delete_time', null)
->where('status', 1)
->find();
if (!$category) {
return json([
'code' => 1,
'msg' => '分类不存在或已禁用'
]);
}
// 获取分页参数
$page = input('page/d', 1);
$limit = input('limit/d', 10);
// 获取该分类下的所有子分类ID(包括自身)
$subCategoryIds = [$cateid];
// 查找所有子分类
$subCategories = ArticlesCategory::where('cid', $cateid)
->where('delete_time', null)
->where('status', 1)
->column('id');
if (!empty($subCategories)) {
$subCategoryIds = array_merge($subCategoryIds, $subCategories);
}
// 构建查询条件
$where = [
['delete_time', '=', null],
['status', '=', 2], // 已发布的文章
['cate', 'in', $subCategoryIds]
];
// 查询文章总数
$total = Articles::where($where)->count();
// 查询文章列表
$articles = Articles::where($where)
->field('id,title,cate,image,desc,author,content,publishdate,views,likes,is_trans,transurl,push,create_time')
->order('sort DESC, id DESC')
->page($page, $limit)
->select()
->toArray();
// 处理文章数据
foreach ($articles as &$article) {
// 如果文章没有封面图,使用分类封面图
if (empty($article['image'])) {
$article['image'] = $category['image'] ?? '';
}
// 格式化时间
$article['publishdate'] = date('Y-m-d H:i:s', strtotime($article['publishdate']));
$article['create_time'] = date('Y-m-d H:i:s', strtotime($article['create_time']));
// 获取分类名称
$articleCategory = ArticlesCategory::where('id', $article['cate'])->find();
$article['category_name'] = $articleCategory ? $articleCategory['name'] : '';
}
// 返回数据
return json([
'code' => 0,
'msg' => '获取成功',
'data' => [
'category' => [
'id' => $category['id'],
'name' => $category['name'],
'desc' => $category['desc'],
'image' => $category['image']
],
'articles' => $articles,
'total' => $total,
'page' => $page,
'limit' => $limit,
'total_pages' => ceil($total / $limit)
]
]);
} catch (\Exception $e) {
return json([
'code' => 1,
'msg' => '获取失败:' . $e->getMessage()
]);
}
}
//文章中心
public function index()
{