From 0aa083caa5a31460d7c73cfa597d8f6b9e1c1954 Mon Sep 17 00:00:00 2001 From: 2991692032 Date: Tue, 3 Jun 2025 18:08:42 +0800 Subject: [PATCH] bug-fix --- unilife-frontend/src/api/user.ts | 10 +++++++ .../src/views/forum/PostDetailView.vue | 28 +++++++++++++++++-- .../unilife/controller/UserController.java | 6 ++++ 3 files changed, 42 insertions(+), 2 deletions(-) diff --git a/unilife-frontend/src/api/user.ts b/unilife-frontend/src/api/user.ts index cd0209d..878854e 100644 --- a/unilife-frontend/src/api/user.ts +++ b/unilife-frontend/src/api/user.ts @@ -94,4 +94,14 @@ export const getUserRecentPosts = (limit?: number) => { // 获取邮箱验证码 export const sendEmailCode = (email: string) => { return api.post>('/users/code', { email }) +} + +// 获取指定用户的统计数据 +export const getUserStatsByUserId = (userId: number) => { + return api.get>(`/users/${userId}/stats`) } \ No newline at end of file diff --git a/unilife-frontend/src/views/forum/PostDetailView.vue b/unilife-frontend/src/views/forum/PostDetailView.vue index 910a012..ba8f87d 100644 --- a/unilife-frontend/src/views/forum/PostDetailView.vue +++ b/unilife-frontend/src/views/forum/PostDetailView.vue @@ -200,6 +200,7 @@ import { } from '@element-plus/icons-vue' import { useUserStore } from '@/stores/user' import { getPostDetail, likePost as likePostAPI, getComments, createComment as createCommentAPI, likeComment as likeCommentAPI } from '@/api/forum' +import { getUserStatsByUserId } from '@/api/user' import type { Post, ApiResponse } from '@/types' import { MdPreview } from 'md-editor-v3' import 'md-editor-v3/lib/style.css' @@ -237,8 +238,8 @@ const comments = ref([]) // 作者统计信息 const authorStats = ref({ - postCount: 12, - likeCount: 234 + postCount: 0, + likeCount: 0 }) // 计算属性 @@ -310,6 +311,26 @@ const likeComment = async (comment: any) => { } } +// 加载作者统计信息 +const loadAuthorStats = async (userId: number) => { + try { + const response = await getUserStatsByUserId(userId) as any as ApiResponse<{ + totalPosts: number + totalLikes: number + totalComments: number + totalViews: number + }> + + if (response.code === 200) { + authorStats.value.postCount = response.data.totalPosts + authorStats.value.likeCount = response.data.totalLikes + } + } catch (error: any) { + console.error('加载作者统计信息失败:', error) + // 失败时保持默认值,不显示错误消息,避免影响用户体验 + } +} + // 加载帖子详情 const loadPost = async () => { const postId = Number(route.params.id) @@ -332,6 +353,9 @@ const loadPost = async () => { isLiked: post.value.isLiked, likeCount: post.value.likeCount }) + + // 加载作者统计信息 + await loadAuthorStats(post.value.userId) } else { ElMessage.error(response.message || '加载帖子失败') router.push('/forum') diff --git a/unilife-server/src/main/java/com/unilife/controller/UserController.java b/unilife-server/src/main/java/com/unilife/controller/UserController.java index e05de7f..47d6957 100644 --- a/unilife-server/src/main/java/com/unilife/controller/UserController.java +++ b/unilife-server/src/main/java/com/unilife/controller/UserController.java @@ -161,4 +161,10 @@ public class UserController { } return userService.getUserRecentPosts(userId, limit); } + + @Operation(summary = "获取指定用户的统计数据") + @GetMapping("{userId}/stats") + public Result getUserStatsByUserId(@PathVariable Long userId) { + return userService.getUserStats(userId); + } }