更新文章接口
This commit is contained in:
@@ -325,38 +325,123 @@ class ArticlesController extends BaseController
|
||||
|
||||
// 构建查询条件
|
||||
$where = [
|
||||
['r.id', '=', $id],
|
||||
['r.delete_time', '=', null],
|
||||
['r.status', '=', 1], // 已审核的资源
|
||||
['r.push', '=', 1], // 已推送的资源
|
||||
['a.id', '=', $id],
|
||||
['a.delete_time', '=', null],
|
||||
['a.status', '=', 2], // 已发布的文章
|
||||
];
|
||||
|
||||
// 查询资源详情,联查分类名称
|
||||
$resource = Resources::where($where)
|
||||
->field('r.id,r.title,r.cate,r.icon,r.price,r.code,r.desc,r.number,r.content,r.views,r.downloads,r.create_time,r.update_time,r.url,r.fileurl,c.name as cate_name')
|
||||
->alias('r')
|
||||
->join('resources_category c', 'r.cate = c.id')
|
||||
// 查询文章详情,联查分类信息
|
||||
$article = Articles::where($where)
|
||||
->field('a.id,a.title,a.cate,a.image,a.desc,a.author,a.content,a.publishdate,a.views,a.likes,a.is_trans,a.transurl,a.push,a.create_time,ac.name as cate_name')
|
||||
->alias('a')
|
||||
->join('articles_category ac', 'a.cate = ac.id', 'LEFT')
|
||||
->find();
|
||||
|
||||
if (!$resource) {
|
||||
if (!$article) {
|
||||
return json([
|
||||
'code' => 1,
|
||||
'msg' => '资源不存在或已删除'
|
||||
'msg' => '文章不存在或已删除'
|
||||
]);
|
||||
}
|
||||
|
||||
// 转换为数组并处理数据
|
||||
$resource = $resource->toArray();
|
||||
$article = $article->toArray();
|
||||
|
||||
// 将cate字段替换为分类名称
|
||||
$resource['cate'] = $resource['cate_name'];
|
||||
unset($resource['cate_name']);
|
||||
// 获取当前文章的分类ID和作者(用于查询相关内容)
|
||||
$currentCateId = $article['cate'];
|
||||
$currentAuthor = $article['author'];
|
||||
|
||||
// 查询上一篇(同分类,ID更小的,按ID倒序取第一个)
|
||||
$prevArticle = Articles::where([
|
||||
['id', '<', $id],
|
||||
['cate', '=', $currentCateId],
|
||||
['delete_time', '=', null],
|
||||
['status', '=', 2]
|
||||
])
|
||||
->field('id,title,image')
|
||||
->order('id', 'desc')
|
||||
->find();
|
||||
|
||||
// 查询下一篇(同分类,ID更大的,按ID正序取第一个)
|
||||
$nextArticle = Articles::where([
|
||||
['id', '>', $id],
|
||||
['cate', '=', $currentCateId],
|
||||
['delete_time', '=', null],
|
||||
['status', '=', 2]
|
||||
])
|
||||
->field('id,title,image')
|
||||
->order('id', 'asc')
|
||||
->find();
|
||||
|
||||
// 查询相关文章(同分类,排除当前文章,按发布时间倒序取5个)
|
||||
$relatedArticles = Articles::where([
|
||||
['id', '<>', $id],
|
||||
['cate', '=', $currentCateId],
|
||||
['delete_time', '=', null],
|
||||
['status', '=', 2]
|
||||
])
|
||||
->field('id,title,image,views,likes,publishdate')
|
||||
->order('publishdate', 'desc')
|
||||
->limit(5)
|
||||
->select();
|
||||
|
||||
// 查询当前作者的文章发布数量
|
||||
$articleCount = Articles::where([
|
||||
['author', '=', $currentAuthor],
|
||||
['delete_time', '=', null],
|
||||
['status', '=', 2]
|
||||
])->count();
|
||||
|
||||
// 查询当前作者的资源发布数量
|
||||
$resourceCount = Resources::where([
|
||||
['delete_time', '=', null],
|
||||
['status', '=', 1],
|
||||
['push', '=', 1]
|
||||
])->count();
|
||||
|
||||
// 格式化时间
|
||||
$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']));
|
||||
|
||||
// 分类名称
|
||||
$article['category_name'] = $article['cate_name'] ?? '';
|
||||
unset($article['cate_name']);
|
||||
|
||||
// 添加作者统计信息
|
||||
$article['articlecount'] = $articleCount;
|
||||
$article['resourcecount'] = $resourceCount;
|
||||
|
||||
// 处理相关文章数据
|
||||
$relatedData = [];
|
||||
if ($relatedArticles) {
|
||||
foreach ($relatedArticles as $related) {
|
||||
$relatedData[] = [
|
||||
'id' => $related['id'],
|
||||
'title' => $related['title'],
|
||||
'image' => $related['image'],
|
||||
'views' => $related['views'],
|
||||
'likes' => $related['likes'],
|
||||
'publishdate' => date('Y-m-d H:i:s', strtotime($related['publishdate']))
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
// 返回数据
|
||||
return json([
|
||||
'code' => 0,
|
||||
'msg' => '获取成功',
|
||||
'data' => $resource
|
||||
'data' => [
|
||||
'article' => $article,
|
||||
'prev_article' => $prevArticle ? [
|
||||
'id' => $prevArticle['id'],
|
||||
'title' => $prevArticle['title']
|
||||
] : null,
|
||||
'next_article' => $nextArticle ? [
|
||||
'id' => $nextArticle['id'],
|
||||
'title' => $nextArticle['title']
|
||||
] : null,
|
||||
'related_articles' => $relatedData
|
||||
]
|
||||
]);
|
||||
|
||||
} catch (\Exception $e) {
|
||||
|
||||
Reference in New Issue
Block a user