diff --git a/app/index/controller/ArticlesController.php b/app/index/controller/ArticlesController.php index 969da2a..85bcdef 100644 --- a/app/index/controller/ArticlesController.php +++ b/app/index/controller/ArticlesController.php @@ -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()]); + } + } } diff --git a/app/index/controller/BaseController.php b/app/index/controller/BaseController.php index 77951c3..ef15462 100644 --- a/app/index/controller/BaseController.php +++ b/app/index/controller/BaseController.php @@ -76,7 +76,7 @@ abstract class BaseController 'id' => $user->id, 'name' => $user->name, 'account' => $user->account, - 'avatar' => $user->avatar ?? '/static/images/default-avatar.png', + 'avatar' => $user->avatar ?? '/static/images/avatar.png', 'is_login' => true, 'last_login_time' => $user->last_login_time ]; diff --git a/app/index/controller/GameController.php b/app/index/controller/GameController.php index 6fdabe7..8be47f7 100644 --- a/app/index/controller/GameController.php +++ b/app/index/controller/GameController.php @@ -227,12 +227,16 @@ class GameController extends BaseController $prevGame = Resources::where('id', '<', $id) ->where('delete_time', null) ->where('status', 1) + ->where('cate', $game['cate']) + ->field(['id', 'title']) ->order('id DESC') ->find(); $nextGame = Resources::where('id', '>', $id) ->where('delete_time', null) ->where('status', 1) + ->where('cate', $game['cate']) + ->field(['id', 'title']) ->order('id ASC') ->find(); @@ -247,7 +251,6 @@ class GameController extends BaseController ->field([ 'g.id', 'g.title', - 'g.desc', 'IF(g.icon IS NULL OR g.icon = "", c.icon, g.icon) as icon' ]) ->order('g.id DESC') diff --git a/app/index/controller/ProgramController.php b/app/index/controller/ProgramController.php index e1f9697..3194396 100644 --- a/app/index/controller/ProgramController.php +++ b/app/index/controller/ProgramController.php @@ -11,6 +11,8 @@ use think\facade\Request; use app\index\model\Resources\Resources; use app\index\model\Resources\ResourcesCategory; use app\index\model\Attachments; +use app\index\model\Users; +use app\index\model\Articles\Articles; class ProgramController extends BaseController { @@ -236,16 +238,43 @@ class ProgramController extends BaseController $cateName = ResourcesCategory::where('id', $program['cate']) ->value('name'); + // 获取作者信息 + $authorInfo = Users::where('name', $program['uploader'])->find(); + if ($authorInfo) { + // 统计作者的文章数 + $articleCount = Articles::where('author', $program['uploader'])->count(); + // 统计作者的资源数 + $resourceCount = Resources::where('uploader', $program['uploader'])->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' => $program['uploader'], + 'resource_count' => 0, + 'article_count' => 0 + ]; + } + // 获取上一个和下一个程序 $prevProgram = Resources::where('id', '<', $id) ->where('delete_time', null) ->where('status', 1) + ->where('cate', $program['cate']) + ->field(['id', 'title']) ->order('id DESC') ->find(); $nextProgram = Resources::where('id', '>', $id) ->where('delete_time', null) ->where('status', 1) + ->where('cate', $program['cate']) + ->field(['id', 'title']) ->order('id ASC') ->find(); @@ -259,7 +288,6 @@ class ProgramController extends BaseController ->field([ 'p.id', 'p.title', - 'p.desc', 'COALESCE(p.icon, c.icon) as icon' ]) ->order('p.id DESC') @@ -273,6 +301,7 @@ class ProgramController extends BaseController 'code' => 1, 'msg' => '获取成功', 'data' => [ + 'authorInfo' => $authorData, 'program' => $program, 'cateName' => $cateName, 'prevProgram' => $prevProgram, @@ -284,6 +313,7 @@ class ProgramController extends BaseController // 非 AJAX 请求返回视图 View::assign([ + 'authorInfo' => $authorData, 'program' => $program, 'cateName' => $cateName, 'prevProgram' => $prevProgram, diff --git a/app/index/view/articles/detail.php b/app/index/view/articles/detail.php index e58fb17..f1d0875 100644 --- a/app/index/view/articles/detail.php +++ b/app/index/view/articles/detail.php @@ -6,87 +6,346 @@
- -