You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

149 lines
2.9 KiB

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 => {
this.setData({
hotTopics: res.data || []
});
}).catch(err => {
console.error('加载热门话题失败:', err);
});
},
loadPosts() {
if (this.data.loading) {
return;
}
this.setData({
loading: true
});
API.getPosts({
page: this.data.page,
pageSize: this.data.pageSize
}).then(res => {
const newPosts = res.data || [];
this.setData({
posts: this.data.page === 1 ? newPosts : [...this.data.posts, ...newPosts],
loading: false,
hasMore: newPosts.length >= this.data.pageSize,
page: this.data.page + 1
});
}).catch(err => {
console.error('加载动态失败:', err);
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'
};
}
});