删除部分多余内容。
This commit is contained in:
+14
-14
@@ -3,15 +3,15 @@
|
||||
*/
|
||||
|
||||
// 评论分页加载
|
||||
async function loadComments(postSlug, page = 1) {
|
||||
async function loadComments(postID, page = 1) {
|
||||
try {
|
||||
const url = `/posts/${postSlug}/comments?page=${page}`;
|
||||
const url = `/posts/${postID}/comments?cpage=${page}`;
|
||||
const res = await fetch(url);
|
||||
if (!res.ok) throw new Error('加载评论失败');
|
||||
|
||||
const data = await res.json();
|
||||
renderComments(data.data || [], postSlug);
|
||||
renderCommentPagination(data.page, data.total_pages, postSlug);
|
||||
renderComments(data.data || []);
|
||||
renderCommentPagination(data.page, data.total_pages, postID);
|
||||
} catch (err) {
|
||||
console.error('加载评论失败:', err);
|
||||
document.querySelector('.comments-list').innerHTML = '<p class="no-comments">评论加载失败</p>';
|
||||
@@ -19,7 +19,7 @@ async function loadComments(postSlug, page = 1) {
|
||||
}
|
||||
|
||||
// 渲染评论列表
|
||||
function renderComments(comments, postSlug) {
|
||||
function renderComments(comments) {
|
||||
const listEl = document.querySelector('.comments-list');
|
||||
if (!listEl) return;
|
||||
|
||||
@@ -66,7 +66,7 @@ function buildCommentHTML(comment) {
|
||||
}
|
||||
|
||||
// 渲染评论分页
|
||||
function renderCommentPagination(currentPage, totalPages, postSlug) {
|
||||
function renderCommentPagination(currentPage, totalPages, postID) {
|
||||
const paginationEl = document.querySelector('.comment-pagination');
|
||||
if (!paginationEl) return;
|
||||
|
||||
@@ -79,13 +79,13 @@ function renderCommentPagination(currentPage, totalPages, postSlug) {
|
||||
let html = '';
|
||||
|
||||
if (currentPage > 1) {
|
||||
html += `<a href="javascript:void(0)" onclick="loadComments('${postSlug}', ${currentPage - 1})" class="prev">← 上一页</a>`;
|
||||
html += `<a href="javascript:void(0)" onclick="loadComments('${postID}', ${currentPage - 1})" class="prev">← 上一页</a>`;
|
||||
}
|
||||
|
||||
html += `<span class="page-info">${currentPage} / ${totalPages}</span>`;
|
||||
|
||||
if (currentPage < totalPages) {
|
||||
html += `<a href="javascript:void(0)" onclick="loadComments('${postSlug}', ${currentPage + 1})" class="next">下一页 →</a>`;
|
||||
html += `<a href="javascript:void(0)" onclick="loadComments('${postID}', ${currentPage + 1})" class="next">下一页 →</a>`;
|
||||
}
|
||||
|
||||
paginationEl.innerHTML = html;
|
||||
@@ -129,8 +129,8 @@ async function submitComment(e) {
|
||||
alert('评论提交成功,等待审核');
|
||||
form.reset();
|
||||
// 重新加载评论(回到第一页)
|
||||
const postSlug = window.location.pathname.split('/').pop();
|
||||
loadComments(postSlug, 1);
|
||||
const postID = form.querySelector('input[name="post_id"]').value;
|
||||
loadComments(postID, 1);
|
||||
} else {
|
||||
alert(result.error || '提交失败');
|
||||
}
|
||||
@@ -171,12 +171,12 @@ document.addEventListener('DOMContentLoaded', function() {
|
||||
commentForm.addEventListener('submit', submitComment);
|
||||
}
|
||||
|
||||
// 如果当前是文章详情页,自动加载评论
|
||||
const postSlug = window.location.pathname.match(/\/post\/([^\/]+)/);
|
||||
if (postSlug && postSlug[1]) {
|
||||
// 如果当前是文章详情页,使用表单中的数字 postID 加载评论
|
||||
const postIDInput = commentForm && commentForm.querySelector('input[name="post_id"]');
|
||||
if (postIDInput && postIDInput.value) {
|
||||
// 从 URL 获取初始页码
|
||||
const urlParams = new URLSearchParams(window.location.search);
|
||||
const cpage = parseInt(urlParams.get('cpage')) || 1;
|
||||
loadComments(postSlug[1], cpage);
|
||||
loadComments(postIDInput.value, cpage);
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user