// pages/forum/forum.js const {forumData} = require('../../utils/data.js') const learningTracker = require('../../utils/learningTracker.js') Page({ data: { posts: [], categories: ['全部', '数学', '物理', '计算机', '英语', '其他'], selectedCategory: '全部' }, onLoad() { this.loadPosts() }, onShow() { // 开始跟踪学习时间 learningTracker.onPageShow('forum') // 每次显示时重新加载,以获取最新发布的帖子 this.loadPosts() // 更新自定义TabBar选中状态 if (typeof this.getTabBar === 'function' && this.getTabBar()) { this.getTabBar().setData({ selected: 2 }) } }, onHide() { // 停止跟踪学习时间 learningTracker.onPageHide() }, onUnload() { // 记录学习时长 learningTracker.onPageUnload() }, // 加载帖子 loadPosts() { try { let posts = wx.getStorageSync('forumPosts') || forumData // 同步评论数量 const allComments = wx.getStorageSync('forumComments') || {} // 获取收藏的帖子列表 const favoritePosts = wx.getStorageSync('favoritePosts') || [] posts = posts.map(post => { const comments = allComments[post.id] || [] post.comments = comments.length // 检查是否已收藏 post.isFavorite = favoritePosts.some(fav => fav.id === post.id) return post }) // 保存更新后的帖子数据 wx.setStorageSync('forumPosts', posts) // 按时间排序(最新的在前) posts.sort((a, b) => b.id - a.id) this.setData({ posts }) this.filterPosts() } catch (error) { console.error('加载帖子失败:', error) wx.showToast({ title: '数据加载失败', icon: 'none' }) } }, // 分类筛选 onCategoryChange(e) { const category = e.currentTarget.dataset.category this.setData({ selectedCategory: category }) this.filterPosts() }, // 筛选帖子 filterPosts() { const { selectedCategory } = this.data let allPosts = wx.getStorageSync('forumPosts') || forumData // 同步评论数量和收藏状态 const allComments = wx.getStorageSync('forumComments') || {} const favoritePosts = wx.getStorageSync('favoritePosts') || [] allPosts = allPosts.map(post => { const comments = allComments[post.id] || [] post.comments = comments.length // 检查是否已收藏 post.isFavorite = favoritePosts.some(fav => fav.id === post.id) return post }) if (selectedCategory === '全部') { this.setData({ posts: allPosts }) } else { const filtered = allPosts.filter(post => post.category === selectedCategory) this.setData({ posts: filtered }) } }, // 查看帖子详情 onPostDetail(e) { const { id } = e.currentTarget.dataset wx.navigateTo({ url: `/pages/forum-detail/forum-detail?id=${id}` }) }, // 发布新帖子 onNewPost() { wx.navigateTo({ url: '/pages/post/post' }) }, // 点赞 onLike(e) { const { id } = e.currentTarget.dataset let posts = wx.getStorageSync('forumPosts') || forumData posts = posts.map(post => { if (post.id === id) { post.isLiked = !post.isLiked post.likes = post.isLiked ? post.likes + 1 : post.likes - 1 } return post }) wx.setStorageSync('forumPosts', posts) this.loadPosts() }, // 预览图片 onPreviewImage(e) { const { url, urls } = e.currentTarget.dataset wx.previewImage({ current: url, // 当前显示图片的链接 urls: urls // 需要预览的图片链接列表 }) }, // 收藏/取消收藏 onFavorite(e) { const { id } = e.currentTarget.dataset let favoritePosts = wx.getStorageSync('favoritePosts') || [] let posts = wx.getStorageSync('forumPosts') || forumData // 找到当前帖子 const currentPost = posts.find(post => post.id === id) if (!currentPost) return // 检查是否已收藏 const index = favoritePosts.findIndex(fav => fav.id === id) if (index > -1) { // 已收藏,取消收藏 favoritePosts.splice(index, 1) wx.showToast({ title: '取消收藏', icon: 'success', duration: 1500 }) } else { // 未收藏,添加收藏 favoritePosts.push({ id: currentPost.id, title: currentPost.title, category: currentPost.category, time: new Date().toLocaleString() }) wx.showToast({ title: '收藏成功', icon: 'success', duration: 1500 }) } // 保存收藏列表 wx.setStorageSync('favoritePosts', favoritePosts) // 重新加载帖子列表 this.loadPosts() } })