Files
mce/templates/MetaLab-2026/static/js/post-reactions.js
Victor_Jay 0af6f7c971 feat: 文章详情页右下角悬浮操作栏 + 作者自赋能拦截
- 赞/踩/赋能/收藏/评论/回顶部移至右下角悬浮操作栏,固定定位
- 回顶部按钮页面顶部隐藏,滚动后显示,平滑滚动
- 评论按钮点击定位到评论区(NAV 下方)
- 赋能按钮作者可见但禁止自赋能,点击弹出 toast 提示
- 未登录用户赋能/收藏点击跳转登录页
- postReactions 引用统一改为 postFloatBar
2026-06-02 23:45:51 +08:00

72 lines
2.6 KiB
JavaScript

// 赞/踩系统
(function() {
var container = document.getElementById('postFloatBar');
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; });
});
})();