parent
							
								
									a637c4eaec
								
							
						
					
					
						commit
						d856624130
					
				| @ -0,0 +1,93 @@ | ||||
| import { defineStore } from "pinia"; | ||||
| import request from "@/utils/request"; | ||||
| 
 | ||||
| export const usePostDetailStore = defineStore("postDetail", { | ||||
|   state: () => ({ | ||||
|     post: null,           // 帖子主要信息
 | ||||
|     comments: [],         // 评论列表
 | ||||
|     userInfo: null,       // 用户信息
 | ||||
|     totalComments: 0,     // 评论总数
 | ||||
|     likeCount: 0,         // 点赞数
 | ||||
|     commentCount: 0,      // 评论数
 | ||||
|     favoriteCount: 0,     // 收藏数
 | ||||
|     viewCount: 0,         // 浏览数
 | ||||
|     isLike: false,        // 是否点赞
 | ||||
|     loading: false,       // 加载状态
 | ||||
|   }), | ||||
|   actions: { | ||||
|     setPost(post) { | ||||
|       this.post = post; | ||||
|     }, | ||||
|     setComments(comments) { | ||||
|       this.comments = comments; | ||||
|       this.totalComments = comments.length; | ||||
|     }, | ||||
|     setUserInfo(userInfo) { | ||||
|       this.userInfo = userInfo; | ||||
|     }, | ||||
|     addComment(comment) { | ||||
|       this.comments.push(comment); | ||||
|       this.totalComments += 1; | ||||
|     }, | ||||
|     async fetchPostDetail(postId, { lastVal = Date.now(), offset = 0, size = 10 } = {}) { | ||||
|       this.loading = true; | ||||
|       try { | ||||
|         // 获取帖子详情
 | ||||
|         const postRes = await request.get(`/post/detail/${postId}`); | ||||
|         if (postRes.code === 200 && postRes.data) { | ||||
|           const { | ||||
|             id, | ||||
|             image, | ||||
|             title, | ||||
|             content, | ||||
|             likeCount, | ||||
|             commentCount, | ||||
|             favoriteCount, | ||||
|             viewCount, | ||||
|             isLike, | ||||
|             userId, | ||||
|             userName, | ||||
|             userAvatar, | ||||
|             createTime | ||||
|           } = postRes.data; | ||||
| 
 | ||||
|           // 主要帖子信息
 | ||||
|           this.post = { | ||||
|             postId: id, | ||||
|             title, | ||||
|             content, | ||||
|             createTime, | ||||
|           }; | ||||
| 
 | ||||
|           // 用户信息
 | ||||
|           this.userInfo = { | ||||
|             userId, | ||||
|             userName, | ||||
|             userAvatar: userAvatar || image || null, | ||||
|             followers:1234,//先预设粉丝占位
 | ||||
|           }; | ||||
| 
 | ||||
|           // 其余字段
 | ||||
|           this.likeCount = likeCount; | ||||
|           this.commentCount = commentCount; | ||||
|           this.favoriteCount = favoriteCount; | ||||
|           this.viewCount = viewCount; | ||||
|           this.isLike = isLike; | ||||
|         } | ||||
| 
 | ||||
|         // 获取评论列表
 | ||||
|         const commentRes = await request.post('/comment/list', { | ||||
|           lastVal, | ||||
|           offset, | ||||
|           size, | ||||
|           postId | ||||
|         }); | ||||
|         if (commentRes.code === 200) { | ||||
|           this.setComments(commentRes.data.records || []); | ||||
|         } | ||||
|       } finally { | ||||
|         this.loading = false; | ||||
|       } | ||||
|     }, | ||||
|   }, | ||||
| }); | ||||
| @ -0,0 +1,78 @@ | ||||
| import {defineStore} from 'pinia'; | ||||
| import axios from 'axios';  | ||||
| import request from '@/utils/request'; | ||||
| 
 | ||||
| export const usePostListStore = defineStore('postList', { | ||||
|   state: () => ({ | ||||
|     posts: [], // 帖子列表
 | ||||
|     total: 0, // 帖子总数
 | ||||
|     page: 1, // 当前页码
 | ||||
|     pageSize: 10, // 每页帖子数
 | ||||
|     lastVal: Date.now(), // 用于滚动分页的时间戳
 | ||||
|     offset: 0, // 偏移量
 | ||||
|     loading: false, // 加载状态
 | ||||
|     finished: false, // 是否加载完全部
 | ||||
|   }), | ||||
|   actions: { | ||||
|     setPosts(posts) { | ||||
|       this.posts = posts; | ||||
|     }, | ||||
|     setTotal(total) { | ||||
|       this.total = total; | ||||
|     }, | ||||
|     setPage(page) { | ||||
|       this.page = page; | ||||
|     }, | ||||
|     setPageSize(pageSize) { | ||||
|       this.pageSize = pageSize; | ||||
|     }, | ||||
|     addPost(post) { | ||||
|       this.posts.push(post); | ||||
|       this.total += 1; // 更新总数
 | ||||
|     }, | ||||
|     removePost(postId) { | ||||
|       this.posts = this.posts.filter(post => post.id !== postId); | ||||
|       this.total -= 1; // 更新总数
 | ||||
|     }, | ||||
|     async getList({ lastVal = this.lastVal, offset = this.offset, size = this.pageSize } = {}) { | ||||
|       if (this.loading || this.finished) return; | ||||
|       this.loading = true; | ||||
|       try { | ||||
|         const res = await request.post('/post/list', { lastVal, offset, size }); | ||||
|         if (res.code === 200) { | ||||
|           const { records, lastVal: newLastVal, offset: newOffset, size: newSize } = res.data; | ||||
|           if (records.length > 0) { | ||||
|             // 字段映射
 | ||||
|             const mappedRecords = records.map(post => ({ | ||||
|               id: post.id, | ||||
|               avatar: post.userAvatar || post.image || require('@/assets/default-avatar/boy_1.png'), | ||||
|               title: post.title, | ||||
|               summary: post.content ? post.content.slice(0, 40) + (post.content.length > 40 ? '...' : '') : '', | ||||
|               likes: post.likeCount, | ||||
|               comments: post.commentCount, | ||||
|               favorites: post.favoriteCount, | ||||
|               category: post.category || '全部', | ||||
|               createTime: post.createTime, | ||||
|               userName: post.userName, | ||||
|             })); | ||||
|             this.posts = [...this.posts, ...mappedRecords]; | ||||
|             this.lastVal = newLastVal; | ||||
|             this.offset = newOffset; | ||||
|             this.pageSize = newSize; | ||||
|           } | ||||
|           if (records.length < size) { | ||||
|             this.finished = true; // 没有更多数据
 | ||||
|           } | ||||
|         } | ||||
|       } finally { | ||||
|         this.loading = false; | ||||
|       } | ||||
|     }, | ||||
|     resetList() { | ||||
|       this.posts = []; | ||||
|       this.lastVal = Date.now(); | ||||
|       this.offset = 0; | ||||
|       this.finished = false; | ||||
|     } | ||||
|   }, | ||||
| }); | ||||
					Loading…
					
					
				
		Reference in new issue