与牧同行-小程序用户端
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.
 

328 lines
7.1 KiB

import http from '../../../utils/api'
const baseUrl = require('../../../utils/baseUrl')
Page({
data: {
postId: null, // 存储帖子ID
post: null,
postHf: [], // 帖子回复列表
replyContent: '',
baseUrl: baseUrl,
replyTarget: {
type: '', // 'post'或'reply'或'comment'
id: '', // 回复或评论的ID
nickName: '',
replyIndex: null,
commentIndex: null,
parentId: null // 父级ID(用于二级评论)
},
replyPlaceholder: '输入您的回复...',
isInputFocused: false,
inputTransformY: '0',
isSubmitting: false,
showPreview: false,
previewImages: [],
previewIndex: 0,
loading: false,
scrollToId: '',
keyboardHeight: 0
},
onLoad: function (options) {
const postId = options.id
this.setData({
postId: postId
})
this.loadPostDetail(postId);
this.getforumReply(postId)
// 监听键盘高度变化
wx.onKeyboardHeightChange(res => {
if (res.height > 0) {
this.setData({
keyboardHeight: res.height
});
}
});
},
// 加载帖子详情
loadPostDetail: function (postId) {
this.setData({
loading: true
});
http.forumDetails({
data: {
id: postId
},
success: res => {
console.log('帖子详情:', res);
const postData = res.data
const images = postData.images ? postData.images.split(',') : []
this.setData({
post: postData,
images: images,
loading: false
});
},
fail: err => {
console.error('加载帖子详情失败:', err);
wx.showToast({
title: '加载失败',
icon: 'none'
});
this.setData({
loading: false
});
}
})
},
// 帖子回复列表
getforumReply(postId) {
http.forumReply({
data: {
questionId: postId
},
success: res => {
console.log('回复列表:', res);
this.setData({
postHf: res.rows || []
})
},
fail: err => {
console.error('加载回复列表失败:', err);
this.setData({
postHf: []
})
}
})
},
// 滚动监听
onScroll: function (e) {
// 可以在这里实现滚动相关逻辑
},
// 输入框获取焦点
onInputFocus: function (e) {
this.setData({
isInputFocused: true,
inputTransformY: `-${e.detail.height}px`
});
},
// 输入框失去焦点
onInputBlur: function () {
this.setData({
isInputFocused: false,
inputTransformY: '0'
});
},
// 回复输入
onReplyInput: function (e) {
this.setData({
replyContent: e.detail.value
});
},
// 回复一级评论
replyToUser: function (e) {
console.log(567,e);
const {
type,
index,
username
} = e.currentTarget.dataset;
const replyItem = this.data.postHf[index];
this.setData({
replyTarget: {
type: 'reply', // 回复一级评论
id: replyItem.id,
nickName: username,
replyIndex: parseInt(index),
parentId: null // 一级评论没有父级ID
},
replyPlaceholder: `回复 @${username}...`,
replyContent: '',
isInputFocused: true
});
// 滚动到对应回复位置
setTimeout(() => {
this.setData({
scrollToId: `reply-${replyItem.id}`
});
}, 100);
},
// 清除回复目标
clearReplyTarget: function () {
this.setData({
replyTarget: {
type: '',
id: '',
nickName: '',
replyIndex: null,
commentIndex: null,
parentId: null
},
replyPlaceholder: '输入您的回复...',
replyContent: ''
});
},
// 提交回复
submitReply: function () {
const {
replyContent,
replyTarget,
postId
} = this.data;
const content = replyContent.trim();
if (!content) {
wx.showToast({
title: '请输入内容',
icon: 'none'
});
return;
}
if (content.length > 500) {
wx.showToast({
title: '回复内容不能超过500字',
icon: 'none'
});
return;
}
this.setData({
isSubmitting: true
});
// 准备提交数据
const submitData = {
questionId: postId, // 帖子ID
content: content // 回复内容
};
// 如果有回复目标,添加父级ID
if (replyTarget.type === 'reply' && replyTarget.id) {
submitData.parentId = replyTarget.id; // 回复一级评论
} else if (replyTarget.type === 'comment' && replyTarget.id) {
submitData.parentId = replyTarget.id; // 回复二级评论
}
// 如果type为'post'或为空,则表示直接回复帖子,parentId为空
console.log('提交数据:', submitData);
// 调用提交接口
http.commentReply({
data: submitData,
success: res => {
console.log('回复成功:', res);
// 提交成功后的处理
this.handleReplySuccess(content, replyTarget);
// 刷新回复列表
setTimeout(() => {
this.getforumReply(postId);
}, 500);
},
fail: err => {
console.error('回复失败:', err);
wx.showToast({
title: '回复失败,请重试',
icon: 'none',
duration: 2000
});
this.setData({
isSubmitting: false
});
}
});
},
// 处理回复成功后的UI更新
handleReplySuccess: function (content, replyTarget) {
// 清空输入框和回复目标
this.setData({
replyContent: '',
replyTarget: {
type: '',
id: '',
nickName: '',
replyIndex: null,
commentIndex: null,
parentId: null
},
replyPlaceholder: '输入您的回复...',
isInputFocused: false,
isSubmitting: false
});
// 如果是直接回复帖子,滚动到底部
if (!replyTarget.type || replyTarget.type === 'post') {
setTimeout(() => {
wx.pageScrollTo({
scrollTop: 9999,
duration: 300
});
}, 300);
}
},
// 图片预览
previewImage: function (e) {
const imgIndex = e.currentTarget.dataset.imgIndex;
const images = this.data.images;
this.setData({
showPreview: true,
previewImages: images,
previewIndex: imgIndex
});
},
// 图片预览滑动
onSwiperChange: function (e) {
this.setData({
previewIndex: e.detail.current
});
},
// 隐藏预览
hidePreview: function () {
this.setData({
showPreview: false
});
},
// 下拉刷新
onPullDownRefresh: function () {
if (this.data.postId) {
this.loadPostDetail(this.data.postId);
this.getforumReply(this.data.postId);
}
wx.stopPullDownRefresh();
},
// 页面上拉触底
onReachBottom: function () {
// 这里可以实现加载更多回复的逻辑
console.log('加载更多回复');
},
// 页面卸载
onUnload: function () {
// 移除键盘高度监听
wx.offKeyboardHeightChange();
}
});