const API = require('../../utils/api.js'); Page({ data: { hotTopics: [], posts: [], loading: false, hasMore: true, page: 1, pageSize: 10 }, onLoad() { this.loadHotTopics(); this.loadPosts(); }, onShow() { this.setData({ page: 1, hasMore: true, posts: [] }); this.loadPosts(); }, onReachBottom() { if (this.data.hasMore && !this.data.loading) { this.loadPosts(); } }, onPullDownRefresh() { this.setData({ page: 1, hasMore: true, posts: [] }); this.loadHotTopics(); this.loadPosts(); wx.stopPullDownRefresh(); }, loadHotTopics() { API.getHotTopics().then(res => { if (res.data && res.data.length > 0) { this.setData({ hotTopics: res.data }); } else { // 使用默认热门话题 this.setData({ hotTopics: [ { id: 1, name: '今天你吃蛋了么?', count: 123 }, { id: 2, name: '日常分享', count: 456 }, { id: 3, name: '鸡蛋行情', count: 789 }, { id: 4, name: '养殖经验', count: 321 }, { id: 5, name: '美食分享', count: 654 } ] }); } }).catch(err => { console.error('加载热门话题失败:', err); // 出错时使用默认热门话题 this.setData({ hotTopics: [ { id: 1, name: '今天你吃蛋了么?', count: 123 }, { id: 2, name: '日常分享', count: 456 }, { id: 3, name: '鸡蛋行情', count: 789 }, { id: 4, name: '养殖经验', count: 321 }, { id: 5, name: '美食分享', count: 654 } ] }); }); }, loadPosts() { if (this.data.loading) { return; } this.setData({ loading: true }); API.getPosts({ page: this.data.page, pageSize: this.data.pageSize }).then(res => { // 正确处理后端返回的响应格式 let newPosts = res.data && res.data.posts ? res.data.posts : []; // 如果是第一页且没有数据,使用默认动态数据 if (this.data.page === 1 && (!newPosts || newPosts.length === 0)) { newPosts = [ { id: 1, user_id: '1', content: '今天的鸡蛋质量真好,客户都很满意!', images: [], likes: 12, comments: 3, created_at: new Date().toISOString(), username: '鸡蛋养殖户', avatar: '', liked: false }, { id: 2, user_id: '2', content: '分享一下我的养殖经验,希望对大家有帮助', images: [], likes: 8, comments: 5, created_at: new Date().toISOString(), username: '养殖专家', avatar: '', liked: false }, { id: 3, user_id: '3', content: '鸡蛋行情不错,今天卖了个好价钱', images: [], likes: 15, comments: 2, created_at: new Date().toISOString(), username: '蛋商小王', avatar: '', liked: false } ]; } // 根据后端返回的分页信息判断是否还有更多数据 const shouldHasMore = res.data && res.data.pagination ? this.data.page < res.data.pagination.totalPages : this.data.page < 3; this.setData({ posts: this.data.page === 1 ? newPosts : [...this.data.posts, ...newPosts], loading: false, hasMore: shouldHasMore, page: this.data.page + 1 }); }).catch(err => { console.error('加载动态失败:', err); // 出错时使用默认动态数据 if (this.data.page === 1) { // 当页码大于3时,设置hasMore为false,显示"暂无更多动态"提示 const shouldHasMore = this.data.page < 3; this.setData({ posts: [ { id: 1, user_id: '1', content: '今天的鸡蛋质量真好,客户都很满意!', images: [], likes: 12, comments: 3, created_at: new Date().toISOString(), username: '鸡蛋养殖户', avatar: '', liked: false }, { id: 2, user_id: '2', content: '分享一下我的养殖经验,希望对大家有帮助', images: [], likes: 8, comments: 5, created_at: new Date().toISOString(), username: '养殖专家', avatar: '', liked: false }, { id: 3, user_id: '3', content: '鸡蛋行情不错,今天卖了个好价钱', images: [], likes: 15, comments: 2, created_at: new Date().toISOString(), username: '蛋商小王', avatar: '', liked: false } ], loading: false, hasMore: shouldHasMore }); } else { this.setData({ loading: false }); } }); }, viewTopic(e) { const topic = e.currentTarget.dataset.topic; wx.navigateTo({ url: `/pages/eggbar/topic-detail?id=${topic.id}` }); }, viewPost(e) { const post = e.currentTarget.dataset.post; wx.navigateTo({ url: `/pages/eggbar/post-detail?id=${post.id}` }); }, likePost(e) { const postId = e.currentTarget.dataset.id; const posts = this.data.posts.map(post => { if (post.id === postId) { return { ...post, liked: !post.liked, likes: post.liked ? post.likes - 1 : post.likes + 1 }; } return post; }); this.setData({ posts }); API.likePost(postId).then(res => { console.log('点赞成功'); }).catch(err => { console.error('点赞失败:', err); }); }, commentPost(e) { const postId = e.currentTarget.dataset.id; wx.navigateTo({ url: `/pages/eggbar/post-detail?id=${postId}&focusComment=true` }); }, sharePost(e) { const postId = e.currentTarget.dataset.id; wx.showShareMenu({ withShareTicket: true, success: () => { console.log('分享成功'); } }); }, createPost() { wx.navigateTo({ url: '/pages/eggbar/create-post' }); }, onShareAppMessage() { return { title: '蛋吧 - 鸡蛋行业交流社区', path: '/pages/eggbar/eggbar', imageUrl: '/images/eggbar-share.jpg' }; } });