From 4509279ac71fbe417d4b0e61cba9141886d39f65 Mon Sep 17 00:00:00 2001 From: hajimi Date: Fri, 12 Jun 2026 17:51:58 +0800 Subject: [PATCH] deploy: auto commit server changes 2026-06-12 17:51:57 --- .../community/CommentAccountController.php | 14 +++ .../lists/community/CommentAccountLists.php | 96 +++++++++++++++++++ .../logic/community/CommunityCommentLogic.php | 55 +++++++++-- .../community/CommunityCommentValidate.php | 16 +++- 4 files changed, 168 insertions(+), 13 deletions(-) create mode 100644 server/app/adminapi/controller/community/CommentAccountController.php create mode 100644 server/app/adminapi/lists/community/CommentAccountLists.php diff --git a/server/app/adminapi/controller/community/CommentAccountController.php b/server/app/adminapi/controller/community/CommentAccountController.php new file mode 100644 index 0000000..4d7377a --- /dev/null +++ b/server/app/adminapi/controller/community/CommentAccountController.php @@ -0,0 +1,14 @@ +dataLists(new CommentAccountLists()); + } +} diff --git a/server/app/adminapi/lists/community/CommentAccountLists.php b/server/app/adminapi/lists/community/CommentAccountLists.php new file mode 100644 index 0000000..1e01848 --- /dev/null +++ b/server/app/adminapi/lists/community/CommentAccountLists.php @@ -0,0 +1,96 @@ +params['keyword'] ?? '')); + $isDisable = $this->params['is_disable'] ?? ''; + if ($keyword !== '') { + $query->where('id|sn|nickname|account', 'like', '%' . $keyword . '%'); + } + if ($isDisable !== '') { + $query->where('is_disable', (int) $isDisable); + } + + $lists = $query + ->field('id,sn,nickname,avatar,account,is_disable,create_time') + ->order('id', 'desc') + ->limit($this->limitOffset, $this->limitLength) + ->select() + ->toArray(); + + $userIds = array_map('intval', array_column($lists, 'id')); + $articleCounts = []; + $communityCounts = []; + if (!empty($userIds)) { + $articleRows = Db::name('article_comment') + ->field('user_id, COUNT(*) AS count') + ->whereIn('user_id', $userIds) + ->whereNull('delete_time') + ->group('user_id') + ->select() + ->toArray(); + foreach ($articleRows as $row) { + $articleCounts[(int) $row['user_id']] = (int) $row['count']; + } + + $communityRows = Db::name('community_comment') + ->field('user_id, COUNT(*) AS count') + ->whereIn('user_id', $userIds) + ->whereNull('delete_time') + ->group('user_id') + ->select() + ->toArray(); + foreach ($communityRows as $row) { + $communityCounts[(int) $row['user_id']] = (int) $row['count']; + } + } + + foreach ($lists as &$item) { + $item['persona_label'] = $this->resolvePersonaLabel((string) $item['nickname']); + $item['article_comment_count'] = $articleCounts[(int) $item['id']] ?? 0; + $item['community_comment_count'] = $communityCounts[(int) $item['id']] ?? 0; + $item['total_comment_count'] = $item['article_comment_count'] + $item['community_comment_count']; + $item['create_time'] = is_numeric($item['create_time']) ? date('Y-m-d H:i:s', (int) $item['create_time']) : $item['create_time']; + } + + return $lists; + } + + public function count(): int + { + $query = User::where('account', 'like', 'ai_comment_%'); + $keyword = trim((string) ($this->params['keyword'] ?? '')); + $isDisable = $this->params['is_disable'] ?? ''; + if ($keyword !== '') { + $query->where('id|sn|nickname|account', 'like', '%' . $keyword . '%'); + } + if ($isDisable !== '') { + $query->where('is_disable', (int) $isDisable); + } + return $query->count(); + } + + protected function resolvePersonaLabel(string $nickname): string + { + if (str_contains($nickname, '·')) { + $parts = explode('·', $nickname, 2); + return $parts[1] ?: '-'; + } + return '-'; + } +} diff --git a/server/app/adminapi/logic/community/CommunityCommentLogic.php b/server/app/adminapi/logic/community/CommunityCommentLogic.php index 048603c..9715bb6 100644 --- a/server/app/adminapi/logic/community/CommunityCommentLogic.php +++ b/server/app/adminapi/logic/community/CommunityCommentLogic.php @@ -3,18 +3,36 @@ namespace app\adminapi\logic\community; use app\common\logic\BaseLogic; +use app\common\model\article\Article; +use app\common\model\article\ArticleComment; use app\common\model\community\CommunityComment; use app\common\model\community\CommunityPost; +use think\facade\Db; class CommunityCommentLogic extends BaseLogic { public static function updateStatus(array $params): bool { try { - CommunityComment::update([ - 'id' => $params['id'], - 'status' => $params['status'], - ]); + if (($params['comment_type'] ?? '') === 'article') { + $comment = ArticleComment::findOrEmpty($params['id']); + if ($comment->isEmpty()) { + self::setError('评论不存在'); + return false; + } + $comment->save([ + 'is_show' => $params['status'], + ]); + } else { + $comment = CommunityComment::findOrEmpty($params['id']); + if ($comment->isEmpty()) { + self::setError('评论不存在'); + return false; + } + $comment->save([ + 'status' => $params['status'], + ]); + } return true; } catch (\Exception $e) { self::setError($e->getMessage()); @@ -24,10 +42,31 @@ class CommunityCommentLogic extends BaseLogic public static function delete(array $params) { - $comment = CommunityComment::findOrEmpty($params['id']); - if (!$comment->isEmpty()) { - CommunityPost::where('id', $comment->post_id)->dec('comment_count')->update(); - $comment->delete(); + Db::startTrans(); + try { + if (($params['comment_type'] ?? '') === 'article') { + $comment = ArticleComment::findOrEmpty($params['id']); + if (!$comment->isEmpty()) { + Article::where('id', $comment->article_id) + ->where('comment_count', '>', 0) + ->dec('comment_count') + ->update(); + $comment->delete(); + } + } else { + $comment = CommunityComment::findOrEmpty($params['id']); + if (!$comment->isEmpty()) { + CommunityPost::where('id', $comment->post_id) + ->where('comment_count', '>', 0) + ->dec('comment_count') + ->update(); + $comment->delete(); + } + } + Db::commit(); + } catch (\Throwable $e) { + Db::rollback(); + self::setError($e->getMessage()); } } } diff --git a/server/app/adminapi/validate/community/CommunityCommentValidate.php b/server/app/adminapi/validate/community/CommunityCommentValidate.php index 2d32277..1b79309 100644 --- a/server/app/adminapi/validate/community/CommunityCommentValidate.php +++ b/server/app/adminapi/validate/community/CommunityCommentValidate.php @@ -8,28 +8,34 @@ use app\common\model\community\CommunityComment; class CommunityCommentValidate extends BaseValidate { protected $rule = [ - 'id' => 'require|checkComment', + 'comment_type' => 'require|in:article,post', + 'id' => 'require|number|checkComment', 'status' => 'require|in:0,1', ]; protected $message = [ + 'comment_type.require' => '评论类型不能为空', + 'comment_type.in' => '评论类型错误', 'id.require' => '评论id不能为空', 'status.require' => '状态不能为空', ]; public function sceneDelete() { - return $this->only(['id']); + return $this->only(['comment_type', 'id']); } public function sceneStatus() { - return $this->only(['id', 'status']); + return $this->only(['comment_type', 'id', 'status']); } - public function checkComment($value) + public function checkComment($value, $rule, $data) { - $comment = CommunityComment::findOrEmpty($value); + $commentType = $data['comment_type'] ?? ''; + $comment = $commentType === 'article' + ? \app\common\model\article\ArticleComment::findOrEmpty($value) + : CommunityComment::findOrEmpty($value); if ($comment->isEmpty()) { return '评论不存在'; }