diff --git a/src/views/community/PostCard.vue b/src/views/community/PostCard.vue index f9fecb2d..7846c84b 100644 --- a/src/views/community/PostCard.vue +++ b/src/views/community/PostCard.vue @@ -51,7 +51,7 @@
-

{{ comment.userid}}: {{ comment.content }}

+

{{ comment.username}}: {{ comment.content }}

@@ -128,14 +128,14 @@ if (this.followsbuttonActive == false) { try { - const response = await axios.post(`http://localhost:8082/loveforest/Myfollows/userid=${useride}/myfollowid=${this.postuserid}`); - if (response.data === "关注成功!") { + const response = await axios.post(`/Myfollows/userid=${useride}/myfollowid=${this.postuserid}`); + if (response === "关注成功!") { - this.$message.success(response.data); + this.$message.success(response); this.followsbuttonActive = true; } else { - this.$message.error(response.data); + this.$message.error(response); } } catch (error) { @@ -149,14 +149,14 @@ else { try { - const response = await axios.delete(`http://localhost:8082/loveforest/Myfollows/userid=${useride}/myfollowid=${this.postuserid}`); - if (response.data === "取消关注成功!") { - this.$message.success(response.data); + const response = await axios.delete(`/Myfollows/userid=${useride}/myfollowid=${this.postuserid}`); + if (response === "取消关注成功!") { + this.$message.success(response); this.followsbuttonActive = false; } else { - this.$message.error(response.data); + this.$message.error(response); } } catch (error) { @@ -180,15 +180,15 @@ async increaseLikeCount() {//点赞按钮 if (!this.isLiked) {//假如没点赞 try { - const response = await axios.post(`http://localhost:8082/loveforest/likes/userid=${this.userid}/postid=${this.postid}`); - if (response.data === "点赞成功!") { - this.$message.success(response.data); + const response = await axios.post(`/likes/userid=${this.userid}/postid=${this.postid}`); + if (response === "点赞成功!") { + this.$message.success(response); this.$emit('updateLikeCount', this.likeCount + 1); this.isLiked = !this.isLiked; } else { - this.$message.error(response.data); + this.$message.error(response); } } catch (error) { @@ -198,8 +198,8 @@ } else {//假如已经点赞 try { - const response = await axios.delete(`http://localhost:8082/loveforest/likes/userid=${this.userid}/postid=${this.postid}`); - if (response.data === "取消点赞成功!") { + const response = await axios.delete(`/likes/userid=${this.userid}/postid=${this.postid}`); + if (response === "取消点赞成功!") { this.$message.error("取消点赞成功"); this.$emit('updateLikeCount', this.likeCount - 1); @@ -207,7 +207,7 @@ } else { - this.$message.error(response.data); + this.$message.error(response); } } catch (error) { diff --git a/src/views/community/trends.vue b/src/views/community/trends.vue index f01d00d1..962fea30 100644 --- a/src/views/community/trends.vue +++ b/src/views/community/trends.vue @@ -28,8 +28,8 @@ style="display: flex; flex-direction: row; justify-content: center; align-items: center; position: relative;height: 120%;gap:3%;width: 100%;" @mouseover="hoverIndex = index" @mouseleave="hoverIndex = -1" @click="fllowsClick(follow.myfollowid,index)" :class="{'selected': selectedIndex === index}"> - -
{{ follow.myfollowid }}
+ +
{{ follow.username }}
{{follow.newpostnum}}条新动态 @@ -143,7 +143,7 @@ import PostCard from './PostCard.vue'; import PostForm from './Postform.vue'; import axios from '@/utils/axiosConfig'; - import {getUserId} from '@/token/auth' + import { getUserId } from '@/token/auth' export default { name: 'CommunityIndex', // 添加组件名称 components: { PostCard, PostForm }, @@ -192,8 +192,8 @@ async loadData() { try { - this.userid= getUserId(); - + this.userid = getUserId(); + // 确保异步操作按顺序执行 await this.fllowlist(); await this.loadhotpost(); @@ -209,37 +209,60 @@ }, async fllowlist() { - const userid = this.userid;//获取用户id - axios.get(`http://localhost:8082/loveforest/Myfollows/userid=${userid}`) - .then(response => { - - - this.myfollows.splice(0, this.myfollows.length); - this.myfollows.push(...response.data); - - return response.data; - }) - .catch(error => { - console.error('Error:', error); - }); + const userid = this.userid; // 获取当前用户的 ID + try { + // 获取当前用户的 follow 列表 + const response = await axios.get(`/Myfollows/userid=${userid}`); + + // 清空原来的 follow 列表 + this.myfollows.splice(0, this.myfollows.length); + + // 并发请求每个 follow 的用户信息(用户名和头像) + const followsWithUserInfo = await Promise.all( + response.map(async (follow) => { + const { userid: followUserid } = follow; + try { + // 获取每个 follow 的用户信息(包括用户名和头像) + const userResponse = await axios.get(`http://localhost:8082/user/info/${followUserid}/editInfo`); + const { username, photo } = userResponse; + + // 返回包含用户名和头像的 follow 对象 + return { + ...follow, + username, + photo + }; + } catch (userError) { + console.error(`Error fetching user info for follow with userid ${followUserid}:`, userError); + return { + ...follow, + username: 'Unknown', // 如果失败,设置默认的用户名 + photo: '' // 如果失败,设置默认的头像为空 + }; + } + }) + ); + + // 将带有用户名和头像的 follow 列表赋值给 myfollows + this.myfollows.push(...followsWithUserInfo); - }, + } catch (error) { + console.error('Error fetching follow list:', error); + } - submitPost(newpost) {//发布帖子后,本地显示帖子 - this.checkPosts = [newpost, ...this.checkPosts]; }, async load() {//加载帖子函数 if (this.isloading) { try { - const response = await axios.get(`http://localhost:8082/loveforest/posts`, { + const response = await axios.get(`/posts`, { params: { page: this.page, size: 5, }, }); - const filteredPostsList = response.data.records; + const filteredPostsList = response.records; this.$message("动态加载中page" + this.page); @@ -256,10 +279,10 @@ try { - const response = await axios.get(`http://localhost:8082/loveforest/posts/postid=${postid}`); + const response = await axios.get(`/posts/postid=${postid}`); this.isloading = false; this.checkPosts.splice(0, this.checkPosts.length); - this.checkPosts.push(response.data); + this.checkPosts.push(response); } catch (error) { @@ -271,13 +294,13 @@ async loaduserlikes() {//加载user点赞列表 并会传入每个帖子做是否已经点赞的判断 const userid = this.userid;//获取用户id - axios.get(`http://localhost:8082/loveforest/likes/userid=${userid}`) + axios.get(`/likes/userid=${userid}`) .then(response => { this.userlikelist.splice(0, this.myfollows.length); - this.userlikelist.push(...response.data); + this.userlikelist.push(...response); - return response.data; + return response; }) .catch(error => { console.error('Error:', error); @@ -294,11 +317,12 @@ this.selectedIndex = -1; }, + async loadhotpost() {//加载热门动态 try { - const response = await axios.get(`http://localhost:8082/loveforest/posts/topposts`); - this.HotsearchList.push(...response.data); - console.log(response.data); + const response = await axios.get(`/posts/topposts`); + this.HotsearchList.push(...response); + console.log(response); } catch (error) { this.$message.error("热门动态获取失败"); @@ -309,8 +333,8 @@ async loadhotfamilystory() {//加载热门家族故事 try { - const response = await axios.get(`http://localhost:8082/loveforest/posts/topfamilystory`); - this.familystories.push(...response.data); + const response = await axios.get(`/posts/topfamilystory`); + this.familystories.push(...response); } catch (error) { @@ -326,12 +350,12 @@ this.selectedIndex = index; // 点击后设置选中的项 try { - const response = await axios.get(`http://localhost:8082/loveforest/posts/userid=${userid}`); + const response = await axios.get(`/posts/userid=${userid}`); this.isloading = false; this.checkPosts.splice(0, this.checkPosts.length); - this.checkPosts.push(...response.data); - if (response.data.length === 0) { + this.checkPosts.push(...response); + if (response.length === 0) { this.$message("对方还没有发过动态哦"); } @@ -350,12 +374,37 @@ updateCommentCount(index, newCommentCount) {//用于本地显示的点赞数量变化 this.checkPosts[index].commentcount = newCommentCount; }, + async toggleCommentsVisibility(index, postid) {//展开评论以及获取评论列表 this.$message("评论区展开" + index); try { - const response = await axios.get(`/comments/postid=${postid}`); - this.checkPosts[index].comments = response; - + const comments = await axios.get(`/comments/postid=${postid}`); + + const commentsWithUsernames = await Promise.all( + comments.map(async (comment) => { + const { userid } = comment; + try { + // 获取用户信息 + const userResponse = await axios.get(`/user/info/${userid}/editInfo`); + const username = userResponse.username; // 假设用户名在 response.data.username 中 + + // 返回包含用户名的评论对象 + return { + ...comment, + username + }; + } catch (userError) { + console.error(`Error fetching username for userid ${userid}:`, userError); + return { + ...comment, + username: 'Unknown' // 如果用户信息请求失败,设置默认用户名 + }; + } + }) + ); + + this.checkPosts[index].comments = commentsWithUsernames; + this.checkPosts[index].isCommentsVisible = !this.checkPosts[index].isCommentsVisible; } catch (error) { @@ -368,7 +417,7 @@ async addComment(index, commentText, postid) {//发送评论 try { - const response = await axios.post('http://localhost:8082/loveforest/comments/addcomments', { + const response = await axios.post('/comments/addcomments', { userid: this.userid, postid: postid, content: commentText @@ -378,20 +427,20 @@ 'Accept': 'application/json' } }); - if (response.startsWith ("评论成功!")) { + if (response.startsWith("评论成功!")) { this.$message.success("评论成功!"); const id = response.replace("评论成功!", '').trim(); this.checkPosts[index].comments.push({ username: '用户名',//等待user接口 postid: postid, content: commentText, - commentid:id, - userid:this.userid + commentid: id, + userid: this.userid }); this.checkPosts[index].commentCount += 1; } else { - this.$message.error(response.data); + this.$message.error(response); } } catch (error) {