更新数据

This commit is contained in:
云泽网
2025-05-12 02:09:58 +08:00
parent f55b0068a2
commit 754db2cfee
9 changed files with 109 additions and 36 deletions
+23 -17
View File
@@ -41,13 +41,14 @@ class Index extends Base
'name' => $category['name']
];
// 获取该分类下的文章
// 获取该分类下的文章,限制4条
$articles = Db::name('yz_article')
->where('cate', $category['id'])
->where('delete_time', null)
->where('status', 2)
->order('id', 'desc')
->field('id, cate, title, image, author, publishdate, views')
->limit(4)
->select()
->toArray();
@@ -76,36 +77,41 @@ class Index extends Base
// 组装分类数据,方便后续查找
$categoryData = [];
$categoryImageMap = [];
$articlesByCategory = [];
foreach ($categories as $category) {
$categoryData[] = [
'id' => $category['id'],
'name' => $category['name']
];
$categoryImageMap[$category['id']] = $category['image'] ?? '';
}
// 获取所有技术分类下的文章
$technicalarticles = Db::name('yz_article')
->whereIn('cate', array_column($categories, 'id'))
->where('delete_time', null)
->where('status', 2)
->order('id', 'desc')
->field('id, cate, title, image, author, publishdate, views')
->select()
->toArray();
// 获取每个分类下的文章,限制12条
$articles = Db::name('yz_article')
->where('cate', $category['id'])
->where('delete_time', null)
->where('status', 2)
->order('id', 'desc')
->field('id, cate, title, image, author, publishdate, views')
->limit(12)
->select()
->toArray();
// 替换image为空的文章
foreach ($technicalarticles as &$article) {
if (empty($article['image']) && !empty($categoryImageMap[$article['cate']])) {
$article['image'] = $categoryImageMap[$article['cate']];
// 替换image为空的文章
foreach ($articles as &$article) {
if (empty($article['image']) && !empty($categoryImageMap[$article['cate']])) {
$article['image'] = $categoryImageMap[$article['cate']];
}
}
unset($article);
$articlesByCategory[$category['id']] = $articles;
}
unset($article);
return json([
'code' => 1,
'msg' => '获取成功',
'articles' => $technicalarticles,
'articles' => $articlesByCategory,
'categories' => $categoryData
]);
}