czq
2991692032 4 days ago
parent 06532d7b5c
commit 0aa083caa5

@ -94,4 +94,14 @@ export const getUserRecentPosts = (limit?: number) => {
// 获取邮箱验证码 // 获取邮箱验证码
export const sendEmailCode = (email: string) => { export const sendEmailCode = (email: string) => {
return api.post<ApiResponse<null>>('/users/code', { email }) 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' } from '@element-plus/icons-vue'
import { useUserStore } from '@/stores/user' import { useUserStore } from '@/stores/user'
import { getPostDetail, likePost as likePostAPI, getComments, createComment as createCommentAPI, likeComment as likeCommentAPI } from '@/api/forum' 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 type { Post, ApiResponse } from '@/types'
import { MdPreview } from 'md-editor-v3' import { MdPreview } from 'md-editor-v3'
import 'md-editor-v3/lib/style.css' import 'md-editor-v3/lib/style.css'
@ -237,8 +238,8 @@ const comments = ref<any[]>([])
// //
const authorStats = ref({ const authorStats = ref({
postCount: 12, postCount: 0,
likeCount: 234 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 loadPost = async () => {
const postId = Number(route.params.id) const postId = Number(route.params.id)
@ -332,6 +353,9 @@ const loadPost = async () => {
isLiked: post.value.isLiked, isLiked: post.value.isLiked,
likeCount: post.value.likeCount likeCount: post.value.likeCount
}) })
//
await loadAuthorStats(post.value.userId)
} else { } else {
ElMessage.error(response.message || '加载帖子失败') ElMessage.error(response.message || '加载帖子失败')
router.push('/forum') router.push('/forum')

@ -161,4 +161,10 @@ public class UserController {
} }
return userService.getUserRecentPosts(userId, limit); return userService.getUserRecentPosts(userId, limit);
} }
@Operation(summary = "获取指定用户的统计数据")
@GetMapping("{userId}/stats")
public Result<?> getUserStatsByUserId(@PathVariable Long userId) {
return userService.getUserStats(userId);
}
} }

Loading…
Cancel
Save