更新文章接口

This commit is contained in:
李志强 2025-12-26 15:30:15 +08:00
parent 3e9a3731e5
commit 9edafa66e7
5 changed files with 199 additions and 30 deletions

4
.env
View File

@ -5,11 +5,11 @@ DEFAULT_TIMEZONE = Asia/Shanghai
[DATABASE] [DATABASE]
TYPE = mysql TYPE = mysql
HOSTNAME = 192.168.31.10 HOSTNAME = 212.64.112.158
DATABASE = yunzertest DATABASE = yunzertest
USERNAME = yunzertest USERNAME = yunzertest
PASSWORD = zKMDMEs7YP3SLDEF PASSWORD = zKMDMEs7YP3SLDEF
HOSTPORT = 3306 HOSTPORT = 3388
CHARSET = utf8 CHARSET = utf8
DEBUG = true DEBUG = true

View File

@ -325,38 +325,123 @@ class ArticlesController extends BaseController
// 构建查询条件 // 构建查询条件
$where = [ $where = [
['r.id', '=', $id], ['a.id', '=', $id],
['r.delete_time', '=', null], ['a.delete_time', '=', null],
['r.status', '=', 1], // 已审核的资源 ['a.status', '=', 2], // 已发布的文章
['r.push', '=', 1], // 已推送的资源
]; ];
// 查询资源详情,联查分类名称 // 查询文章详情,联查分类信息
$resource = Resources::where($where) $article = Articles::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') ->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('r') ->alias('a')
->join('resources_category c', 'r.cate = c.id') ->join('articles_category ac', 'a.cate = ac.id', 'LEFT')
->find(); ->find();
if (!$resource) { if (!$article) {
return json([ return json([
'code' => 1, 'code' => 1,
'msg' => '资源不存在或已删除' 'msg' => '文章不存在或已删除'
]); ]);
} }
// 转换为数组并处理数据 // 转换为数组并处理数据
$resource = $resource->toArray(); $article = $article->toArray();
// 将cate字段替换为分类名称 // 获取当前文章的分类ID和作者用于查询相关内容
$resource['cate'] = $resource['cate_name']; $currentCateId = $article['cate'];
unset($resource['cate_name']); $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([ return json([
'code' => 0, 'code' => 0,
'msg' => '获取成功', '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) { } catch (\Exception $e) {

View File

@ -73,11 +73,74 @@ class ResourcesController extends BaseController
// 转换为数组并处理数据 // 转换为数组并处理数据
$resource = $resource->toArray(); $resource = $resource->toArray();
// 获取当前资源的分类ID用于查询相关内容
$currentCateId = $resource['cate'];
// 查询上一篇同分类ID更小的按ID倒序取第一个
$prevResource = Resources::where([
['id', '<', $id],
['cate', '=', $currentCateId],
['delete_time', '=', null],
['status', '=', 1],
['push', '=', 1]
])
->field('id,title,icon')
->order('id', 'desc')
->find();
// 查询下一篇同分类ID更大的按ID正序取第一个
$nextResource = Resources::where([
['id', '>', $id],
['cate', '=', $currentCateId],
['delete_time', '=', null],
['status', '=', 1],
['push', '=', 1]
])
->field('id,title,icon')
->order('id', 'asc')
->find();
// 查询相关文章同分类排除当前文章按时间倒序取5个
$relatedResources = Resources::where([
['id', '<>', $id],
['cate', '=', $currentCateId],
['delete_time', '=', null],
['status', '=', 1],
['push', '=', 1]
])
->field('id,title,icon,views,downloads,create_time')
->order('create_time', 'desc')
->limit(5)
->select();
// 将cate字段替换为分类名称 // 将cate字段替换为分类名称
$resource['cate'] = $resource['cate_name']; $resource['cate'] = $resource['cate_name'];
unset($resource['cate_name']); unset($resource['cate_name']);
// 返回数据 // 处理相关文章数据
$relatedData = [];
if ($relatedResources) {
foreach ($relatedResources as $related) {
$relatedData[] = [
'id' => $related['id'],
'title' => $related['title'],
'icon' => $related['icon']
];
}
}
// 将相关数据添加到资源对象中,保持原有数据结构
$resource['prev_resource'] = $prevResource ? [
'id' => $prevResource['id'],
'title' => $prevResource['title']
] : null;
$resource['next_resource'] = $nextResource ? [
'id' => $nextResource['id'],
'title' => $nextResource['title']
] : null;
$resource['related_resources'] = $relatedData;
// 返回数据,保持原有结构
return json([ return json([
'code' => 0, 'code' => 0,
'msg' => '获取成功', 'msg' => '获取成功',

View File

@ -44,17 +44,36 @@
<div class="game-info"> <div class="game-info">
<div class="title">Free</div> <div class="title">Free</div>
<div class="infos"> <div class="infos">
<div class="infoitem"><span>更新时间:</span><span <div class="infoitem">
class="infoitem-value"><?php echo date('Y-m-d', $game['create_time']); ?></span> <span>更新时间:</span>
<span class="infoitem-value">
<?php echo date('Y-m-d', is_numeric($game['create_time']) ? $game['create_time'] : strtotime($game['create_time'])); ?>
</span>
</div>
<div class="infoitem">
<span>所属分类:</span>
<span class="infoitem-value">
<?php echo $cateName; ?>
</span>
</div>
<div class="infoitem">
<span>程序编号:</span>
<span class="infoitem-value">
<?php echo $game['number']; ?>
</span>
</div>
<div class="infoitem">
<span>查看:</span>
<span class="infoitem-value">
<?php echo $game['views']; ?>
</span>
</div>
<div class="infoitem">
<span>下载:</span>
<span class="infoitem-value">
<?php echo $game['downloads']; ?>
</span>
</div> </div>
<div class="infoitem"><span>所属分类:</span><span
class="infoitem-value"><?php echo $cateName; ?></span></div>
<div class="infoitem"><span>程序编号:</span><span
class="infoitem-value"><?php echo $game['number']; ?></span></div>
<div class="infoitem"><span>查看:</span><span
class="infoitem-value"><?php echo $game['views']; ?></span></div>
<div class="infoitem"><span>下载:</span><span
class="infoitem-value"><?php echo $game['downloads']; ?></span></div>
</div> </div>
</div> </div>
</div> </div>

View File

@ -1,4 +1,4 @@
<?php /*a:5:{s:63:"E:\Demos\DemoOwns\PHP\yunzer\app\index\view\articles\detail.php";i:1748423296;s:62:"E:\Demos\DemoOwns\PHP\yunzer\app\index\view\component\head.php";i:1747617129;s:71:"E:\Demos\DemoOwns\PHP\yunzer\app\index\view\component\header-simple.php";i:1749258723;s:64:"E:\Demos\DemoOwns\PHP\yunzer\app\index\view\component\footer.php";i:1749170849;s:62:"E:\Demos\DemoOwns\PHP\yunzer\app\index\view\component\foot.php";i:1747616844;}*/ ?> <?php /*a:5:{s:63:"E:\Demos\DemoOwns\PHP\yunzer\app\index\view\articles\detail.php";i:1750323451;s:62:"E:\Demos\DemoOwns\PHP\yunzer\app\index\view\component\head.php";i:1754756468;s:71:"E:\Demos\DemoOwns\PHP\yunzer\app\index\view\component\header-simple.php";i:1766456641;s:64:"E:\Demos\DemoOwns\PHP\yunzer\app\index\view\component\footer.php";i:1750323451;s:62:"E:\Demos\DemoOwns\PHP\yunzer\app\index\view\component\foot.php";i:1750323451;}*/ ?>
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
@ -10,8 +10,10 @@
<link rel="stylesheet" href="/static/css/style.css"> <link rel="stylesheet" href="/static/css/style.css">
<link rel="stylesheet" href="/static/css/bootstrap.min.css"> <link rel="stylesheet" href="/static/css/bootstrap.min.css">
<link rel="stylesheet" href="/static/css/fontawesome.css"> <link rel="stylesheet" href="/static/css/fontawesome.css">
<link rel="stylesheet" href="/static/css/all.min.css">
<script src="/static/layui/layui.js" charset="utf-8"></script> <script src="/static/layui/layui.js" charset="utf-8"></script>
<script src="/static/js/all.min.js"></script>
<script src="/static/js/bootstrap.bundle.js"></script> <script src="/static/js/bootstrap.bundle.js"></script>
<script charset="UTF-8" id="LA_COLLECT" src="//www.yunzer.cn/plugins/js-sdk-pro.min.js"></script> <script charset="UTF-8" id="LA_COLLECT" src="//www.yunzer.cn/plugins/js-sdk-pro.min.js"></script>
<script>LA.init({ id: "KoyzaWWEcLvPzkQn", ck: "KoyzaWWEcLvPzkQn", autoTrack: true, prefix: 'event' })</script> <script>LA.init({ id: "KoyzaWWEcLvPzkQn", ck: "KoyzaWWEcLvPzkQn", autoTrack: true, prefix: 'event' })</script>