273 lines
6.3 KiB
JavaScript
273 lines
6.3 KiB
JavaScript
// pages/forum-detail/forum-detail.js
|
|
const {forumData} = require('../../utils/data.js')
|
|
const learningTracker = require('../../utils/learningTracker.js')
|
|
const { showSuccess } = require('../../utils/util.js')
|
|
|
|
// pages/forum-detail/forum-detail.js
|
|
const userManager = require('../../utils/userManager.js')
|
|
|
|
Page({
|
|
data: {
|
|
post: null,
|
|
commentText: '',
|
|
comments: [],
|
|
postId: null
|
|
},
|
|
|
|
onLoad(options) {
|
|
const { id } = options
|
|
this.setData({ postId: parseInt(id) })
|
|
this.loadPostDetail(id)
|
|
},
|
|
|
|
onShow() {
|
|
// 开始跟踪学习时间
|
|
learningTracker.onPageShow('forum')
|
|
},
|
|
|
|
onHide() {
|
|
// 停止跟踪学习时间
|
|
learningTracker.onPageHide()
|
|
},
|
|
|
|
onUnload() {
|
|
// 记录学习时长
|
|
learningTracker.onPageUnload()
|
|
},
|
|
|
|
// 加载帖子详情
|
|
loadPostDetail(id) {
|
|
let posts = wx.getStorageSync('forumPosts') || forumData
|
|
const post = posts.find(p => p.id === parseInt(id))
|
|
|
|
if (post) {
|
|
// 增加浏览量
|
|
post.views += 1
|
|
|
|
// 检查是否已收藏
|
|
const favoritePosts = wx.getStorageSync('favoritePosts') || []
|
|
post.isFavorite = favoritePosts.some(fav => fav.id === post.id)
|
|
|
|
posts = posts.map(p => p.id === post.id ? post : p)
|
|
wx.setStorageSync('forumPosts', posts)
|
|
|
|
// 加载评论
|
|
const comments = this.loadComments(parseInt(id))
|
|
|
|
this.setData({
|
|
post,
|
|
comments
|
|
})
|
|
}
|
|
},
|
|
|
|
// 加载评论
|
|
loadComments(postId) {
|
|
const allComments = wx.getStorageSync('forumComments') || {}
|
|
return allComments[postId] || []
|
|
},
|
|
|
|
// 保存评论
|
|
saveComments(postId, comments) {
|
|
const allComments = wx.getStorageSync('forumComments') || {}
|
|
allComments[postId] = comments
|
|
wx.setStorageSync('forumComments', allComments)
|
|
|
|
// 更新帖子的评论数
|
|
let posts = wx.getStorageSync('forumPosts') || forumData
|
|
posts = posts.map(p => {
|
|
if (p.id === postId) {
|
|
p.comments = comments.length
|
|
}
|
|
return p
|
|
})
|
|
wx.setStorageSync('forumPosts', posts)
|
|
},
|
|
|
|
// 点赞
|
|
onLike() {
|
|
const { post } = this.data
|
|
let posts = wx.getStorageSync('forumPosts') || forumData
|
|
|
|
posts = posts.map(p => {
|
|
if (p.id === post.id) {
|
|
p.isLiked = !p.isLiked
|
|
p.likes = p.isLiked ? p.likes + 1 : p.likes - 1
|
|
}
|
|
return p
|
|
})
|
|
|
|
wx.setStorageSync('forumPosts', posts)
|
|
|
|
this.setData({
|
|
post: {
|
|
...post,
|
|
isLiked: !post.isLiked,
|
|
likes: post.isLiked ? post.likes - 1 : post.likes + 1
|
|
}
|
|
})
|
|
},
|
|
|
|
// 收藏帖子
|
|
onFavorite() {
|
|
const { post } = this.data
|
|
|
|
if (!post || !post.id || !post.title) {
|
|
wx.showToast({
|
|
title: '帖子数据异常',
|
|
icon: 'none'
|
|
})
|
|
console.error('帖子数据不完整:', post)
|
|
return
|
|
}
|
|
|
|
let favoritePosts = wx.getStorageSync('favoritePosts') || []
|
|
const index = favoritePosts.findIndex(p => p.id === post.id)
|
|
|
|
if (index > -1) {
|
|
// 已收藏,取消收藏
|
|
favoritePosts.splice(index, 1)
|
|
wx.setStorageSync('favoritePosts', favoritePosts)
|
|
|
|
console.log('取消收藏后的列表:', favoritePosts)
|
|
|
|
// 更新帖子状态
|
|
let posts = wx.getStorageSync('forumPosts') || []
|
|
posts = posts.map(p => {
|
|
if (p.id === post.id) {
|
|
p.isFavorite = false
|
|
}
|
|
return p
|
|
})
|
|
wx.setStorageSync('forumPosts', posts)
|
|
|
|
this.setData({
|
|
post: {
|
|
...post,
|
|
isFavorite: false
|
|
}
|
|
})
|
|
|
|
wx.showToast({
|
|
title: '已取消收藏',
|
|
icon: 'success'
|
|
})
|
|
} else {
|
|
// 未收藏,添加收藏
|
|
const favoritePost = {
|
|
id: post.id,
|
|
title: post.title,
|
|
category: post.category,
|
|
time: new Date().toLocaleString()
|
|
}
|
|
|
|
favoritePosts.push(favoritePost)
|
|
wx.setStorageSync('favoritePosts', favoritePosts)
|
|
|
|
console.log('收藏成功,当前收藏列表:', favoritePosts)
|
|
|
|
// 更新帖子状态
|
|
let posts = wx.getStorageSync('forumPosts') || []
|
|
posts = posts.map(p => {
|
|
if (p.id === post.id) {
|
|
p.isFavorite = true
|
|
}
|
|
return p
|
|
})
|
|
wx.setStorageSync('forumPosts', posts)
|
|
|
|
this.setData({
|
|
post: {
|
|
...post,
|
|
isFavorite: true
|
|
}
|
|
})
|
|
|
|
wx.showToast({
|
|
title: '收藏成功',
|
|
icon: 'success'
|
|
})
|
|
}
|
|
},
|
|
|
|
// 评论输入
|
|
onCommentInput(e) {
|
|
this.setData({ commentText: e.detail.value })
|
|
},
|
|
|
|
// 发表评论
|
|
onSubmitComment() {
|
|
const { commentText, comments, postId } = this.data
|
|
|
|
if (!commentText.trim()) {
|
|
wx.showToast({
|
|
title: '请输入评论内容',
|
|
icon: 'none'
|
|
})
|
|
return
|
|
}
|
|
|
|
// 获取用户信息
|
|
const userInfo = userManager.getUserInfo()
|
|
const userName = userInfo.nickname || '我'
|
|
const userAvatar = userInfo.avatar || '/images/avatar-default.png'
|
|
|
|
const newComment = {
|
|
id: Date.now(),
|
|
author: userName,
|
|
avatar: userAvatar,
|
|
content: commentText,
|
|
time: '刚刚'
|
|
}
|
|
|
|
const newComments = [newComment, ...comments]
|
|
|
|
// 保存评论
|
|
this.saveComments(postId, newComments)
|
|
|
|
// 同时将评论保存到帖子的commentList中
|
|
let posts = wx.getStorageSync('forumPosts') || []
|
|
posts = posts.map(p => {
|
|
if (p.id === postId) {
|
|
if (!p.commentList) {
|
|
p.commentList = []
|
|
}
|
|
p.commentList.unshift(newComment)
|
|
}
|
|
return p
|
|
})
|
|
wx.setStorageSync('forumPosts', posts)
|
|
|
|
this.setData({
|
|
comments: newComments,
|
|
commentText: ''
|
|
})
|
|
|
|
showSuccess('评论成功')
|
|
|
|
console.log('评论已发布', {
|
|
作者: userName,
|
|
内容: commentText,
|
|
帖子ID: postId
|
|
})
|
|
},
|
|
|
|
// 预览图片
|
|
onPreviewImage(e) {
|
|
const { url, urls } = e.currentTarget.dataset
|
|
wx.previewImage({
|
|
current: url, // 当前显示图片的链接
|
|
urls: urls // 需要预览的图片链接列表
|
|
})
|
|
},
|
|
|
|
// 分享
|
|
onShareAppMessage() {
|
|
const { post } = this.data
|
|
return {
|
|
title: post.title,
|
|
path: `/pages/forum-detail/forum-detail?id=${post.id}`
|
|
}
|
|
}
|
|
})
|