// ============================================================
// 📄 帖子详情 + 回复
// ============================================================
let currentPostId = null;
let reportTargetPostId = null;
function switchToPost(postId) {
const url = new URL(window.location);
url.searchParams.set('post', postId);
url.searchParams.delete('page');
window.history.pushState({ page: 'post', postId: postId }, '', url);
showPostPage(postId);
}
function showPostPage(postId) {
document.getElementById('app').style.display = 'none';
document.querySelectorAll('.page-slide').forEach(el => {
el.classList.remove('active', 'slide-out');
});
const el = document.getElementById('pagePost');
el.classList.add('active');
el.style.animation = 'none';
void el.offsetHeight;
el.style.animation = '';
currentPage = 'post';
renderPostDetail(postId);
}
async function renderPostDetail(postId) {
const container = document.getElementById('postDetailContent');
const replyArea = document.getElementById('replyArea');
container.innerHTML = '
加载中...
';
replyArea.style.display = 'none';
try {
const post = await API.getPost(postId);
const replies = await API.getReplies(postId);
document.getElementById('postDetailTitle').textContent = post.title || '帖子详情';
const username = post.username || '匿名用户';
const avatar = post.avatar_url ||
'https://ui-avatars.com/api/?name=' + encodeURIComponent(username) +
'&background=6366f1&color=fff&size=64';
const time = post.created_at ? new Date(post.created_at).toLocaleString('zh-CN') : '';
let imagesHtml = '';
if (post.images && post.images.length > 0) {
imagesHtml = '';
post.images.forEach(img => {
imagesHtml += '

';
});
imagesHtml += '
';
}
let html = `
${post.title || '无标题'}
${(post.content || '').replace(/\n/g, '
')}
${imagesHtml}
`;
if (replies.length === 0) {
html += '
还没有回复,快来抢沙发吧 🛋️
';
} else {
replies.forEach(r => {
const rUsername = r.username || '匿名用户';
const rAvatar = r.avatar_url ||
'https://ui-avatars.com/api/?name=' + encodeURIComponent(rUsername) +
'&background=6366f1&color=fff&size=64';
const rTime = r.created_at ? new Date(r.created_at).toLocaleString('zh-CN') : '';
html += `
${(r.content || '').replace(/\n/g, '
')}
`;
});
}
html += '
';
container.innerHTML = html;
replyArea.style.display = 'block';
document.getElementById('replyContent').value = '';
document.getElementById('replyCaptchaInput').value = '';
refreshCaptcha('reply');
const deleteBtn = document.getElementById('detailDeleteBtn');
if (deleteBtn) {
deleteBtn.addEventListener('click', function() {
if (confirm('确定要删除这条帖子吗?')) {
API.deletePost(postId).then(() => {
alert('删除成功');
switchPage('feed');
renderFeed();
renderStats();
}).catch(err => alert('删除失败:' + err.message));
}
});
}
document.querySelectorAll('.reply-delete-btn').forEach(btn => {
btn.addEventListener('click', function(e) {
e.stopPropagation();
const replyId = this.dataset.replyid;
if (confirm('确定要删除这条回复吗?')) {
API.deleteReply(replyId).then(() => {
renderPostDetail(postId);
}).catch(err => alert('删除失败:' + err.message));
}
});
});
const replySubmit = document.getElementById('replySubmit');
const newReplySubmit = replySubmit.cloneNode(true);
replySubmit.parentNode.replaceChild(newReplySubmit, replySubmit);
newReplySubmit.addEventListener('click', async function() {
await handleReplySubmit(postId);
});
const replyInput = document.getElementById('replyContent');
replyInput.addEventListener('keydown', function(e) {
if (e.key === 'Enter' && (e.ctrlKey || e.metaKey)) {
e.preventDefault();
handleReplySubmit(postId);
}
});
currentPostId = postId;
} catch (err) {
container.innerHTML = '