更新界面详情

This commit is contained in:
2025-05-28 17:25:16 +08:00
parent 5d791d84ed
commit 352540b500
13 changed files with 1742 additions and 1312 deletions
+88 -4
View File
@@ -4,11 +4,14 @@
*/
namespace app\index\controller;
use app\index\controller\BaseController;
use app\index\model\Users;
use think\facade\Db;
use think\facade\View;
use think\facade\Request;
use app\index\model\Articles\Articles;
use app\index\model\Articles\ArticlesCategory;
use app\index\model\Authors\Authors;
use app\index\model\Resources\Resources;
class ArticlesController extends BaseController
{
@@ -210,16 +213,43 @@ class ArticlesController extends BaseController
$cateName = ArticlesCategory::where('id', $article['cate'])
->value('name');
// 获取作者信息
$authorInfo = Users::where('name', $article['author'])->find();
if ($authorInfo) {
// 统计作者的文章数
$articleCount = Articles::where('author', $article['author'])->count();
// 统计作者的资源数
$resourceCount = Resources::where('uploader', $article['author'])->count();
$authorData = [
'avatar' => $authorInfo['avatar'] ?: '/static/images/avatar.png',
'name' => $authorInfo['name'],
'resource_count' => $resourceCount,
'article_count' => $articleCount
];
} else {
$authorData = [
'avatar' => '/static/images/avatar.png',
'name' => $article['author'],
'resource_count' => 0,
'article_count' => 0
];
}
// 获取上一篇和下一篇文章
$prevArticle = Articles::where('id', '<', $id)
->where('delete_time', null)
->where('status', '<>', 3)
->where('cate', $article['cate'])
->field(['id', 'title'])
->order('id DESC')
->find();
$nextArticle = Articles::where('id', '>', $id)
->where('delete_time', null)
->where('status', '<>', 3)
->where('cate', $article['cate'])
->field(['id', 'title'])
->order('id ASC')
->find();
@@ -233,7 +263,6 @@ class ArticlesController extends BaseController
->field([
'a.id',
'a.title',
'a.desc',
'IF(a.image IS NULL OR a.image = "", c.image, a.image) as image'
])
->order('a.id DESC')
@@ -241,12 +270,13 @@ class ArticlesController extends BaseController
->select()
->toArray();
// 如果是 AJAX 请求,返回 JSON 数据
if (Request::isAjax()) {
// 如果是 POST 请求,返回 JSON 数据
if (Request::isPost()) {
return json([
'code' => 1,
'msg' => '获取成功',
'data' => [
'authorInfo' => $authorData,
'article' => $article,
'cateName' => $cateName,
'prevArticle' => $prevArticle,
@@ -256,8 +286,9 @@ class ArticlesController extends BaseController
]);
}
// 非 AJAX 请求返回视图
// GET 请求返回视图
View::assign([
'authorInfo' => $authorData,
'article' => $article,
'cateName' => $cateName,
'prevArticle' => $prevArticle,
@@ -417,4 +448,57 @@ class ArticlesController extends BaseController
return json(['code' => 0, 'msg' => '更新失败:' . $e->getMessage()]);
}
}
//获取作者信息
public function getAuthorInfo()
{
if (!Request::isPost()) {
return json(['code' => 0, 'msg' => '非法请求']);
}
$authorName = Request::post('name');
if (empty($authorName)) {
return json(['code' => 0, 'msg' => '作者名称不能为空']);
}
try {
// 获取作者基本信息
$author = Db::name('users')
->where('name', $authorName)
->field('id, name, avatar')
->find();
if (!$author) {
return json(['code' => 0, 'msg' => '作者不存在']);
}
// 获取作者发布的资源数量
$resourceCount = Db::name('resources')
->where('user_id', $author['id'])
->where('delete_time', null)
->where('status', 2) // 假设2是已发布状态
->count();
// 获取作者发布的文章数量
$articleCount = Db::name('articles')
->where('author', $authorName)
->where('delete_time', null)
->where('status', 2) // 假设2是已发布状态
->count();
return json([
'code' => 1,
'msg' => '获取成功',
'data' => [
'avatar' => $author['avatar'],
'name' => $author['name'],
'resource_count' => $resourceCount,
'article_count' => $articleCount
]
]);
} catch (\Exception $e) {
return json(['code' => 0, 'msg' => '获取作者信息失败:' . $e->getMessage()]);
}
}
}