czq
2991692032 3 days ago
parent 06532d7b5c
commit 0aa083caa5

@ -94,4 +94,14 @@ export const getUserRecentPosts = (limit?: number) => {
// 获取邮箱验证码
export const sendEmailCode = (email: string) => {
return api.post<ApiResponse<null>>('/users/code', { email })
}
// 获取指定用户的统计数据
export const getUserStatsByUserId = (userId: number) => {
return api.get<ApiResponse<{
totalPosts: number
totalLikes: number
totalComments: number
totalViews: number
}>>(`/users/${userId}/stats`)
}

@ -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<any[]>([])
//
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')

@ -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);
}
}

Loading…
Cancel
Save