refactor: posts/show.html 内联 JS 拆分为独立文件
- 拆分 926 行内联 JS 为 5 个独立模块文件 - post-view.js: Vditor 渲染 + 提交审核按钮 (~50 行) - post-energize.js: 赋能按钮 + 弹窗 (~76 行) - post-reactions.js: 赞/踩系统 (~71 行) - post-favorite.js: 收藏按钮 + 收藏夹弹窗 (~218 行) - post-comments.js: 评论系统 (~456 行) - show.html 从 1080 行缩减至 159 行
This commit is contained in:
71
templates/MetaLab-2026/static/js/post-reactions.js
Normal file
71
templates/MetaLab-2026/static/js/post-reactions.js
Normal file
@ -0,0 +1,71 @@
|
||||
// 赞/踩系统
|
||||
(function() {
|
||||
var container = document.getElementById('postReactions');
|
||||
var likeBtn = document.getElementById('likeBtn');
|
||||
var dislikeBtn = document.getElementById('dislikeBtn');
|
||||
var likesCountEl = document.getElementById('likesCount');
|
||||
if (!container || !likeBtn || !dislikeBtn) return;
|
||||
|
||||
var postId = container.dataset.postId;
|
||||
|
||||
// 页面加载时获取当前反应状态
|
||||
fetch('/api/posts/' + postId + '/reaction')
|
||||
.then(function(r) { return r.json(); })
|
||||
.then(function(d) {
|
||||
if (!d.success || !d.data) return;
|
||||
updateUI(d.data.reaction, d.data.likes_count);
|
||||
})
|
||||
.catch(function() {});
|
||||
|
||||
function updateUI(reaction, count) {
|
||||
likeBtn.classList.remove('active');
|
||||
dislikeBtn.classList.remove('active');
|
||||
if (reaction === 'liked') likeBtn.classList.add('active');
|
||||
if (reaction === 'disliked') dislikeBtn.classList.add('active');
|
||||
if (typeof count === 'number') likesCountEl.textContent = count;
|
||||
}
|
||||
|
||||
likeBtn.addEventListener('click', function() {
|
||||
likeBtn.disabled = true;
|
||||
fetch('/api/posts/' + postId + '/like', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json', 'X-CSRF-Token': getCSRFToken() }
|
||||
})
|
||||
.then(function(r) {
|
||||
if (r.status === 401) { window.location.href = '/auth/login?redirect=/posts/' + postId; return null; }
|
||||
return r.json();
|
||||
})
|
||||
.then(function(d) {
|
||||
likeBtn.disabled = false;
|
||||
if (!d) return;
|
||||
if (d.success) {
|
||||
updateUI(d.data.liked ? 'liked' : 'none', d.data.likes_count);
|
||||
} else {
|
||||
alert(d.message || '操作失败');
|
||||
}
|
||||
})
|
||||
.catch(function() { likeBtn.disabled = false; });
|
||||
});
|
||||
|
||||
dislikeBtn.addEventListener('click', function() {
|
||||
dislikeBtn.disabled = true;
|
||||
fetch('/api/posts/' + postId + '/dislike', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json', 'X-CSRF-Token': getCSRFToken() }
|
||||
})
|
||||
.then(function(r) {
|
||||
if (r.status === 401) { window.location.href = '/auth/login?redirect=/posts/' + postId; return null; }
|
||||
return r.json();
|
||||
})
|
||||
.then(function(d) {
|
||||
dislikeBtn.disabled = false;
|
||||
if (!d) return;
|
||||
if (d.success) {
|
||||
updateUI(d.data.disliked ? 'disliked' : 'none');
|
||||
} else {
|
||||
alert(d.message || '操作失败');
|
||||
}
|
||||
})
|
||||
.catch(function() { dislikeBtn.disabled = false; });
|
||||
});
|
||||
})();
|
||||
Reference in New Issue
Block a user