This repository has been archived on 2026-06-21. You can view files and clone it, but cannot push or open issues or pull requests.
Files
MetaLab/templates/MetaLab-2026/static/js/post-energize.js
Victor_Jay 0af6f7c971 feat: 文章详情页右下角悬浮操作栏 + 作者自赋能拦截
- 赞/踩/赋能/收藏/评论/回顶部移至右下角悬浮操作栏,固定定位
- 回顶部按钮页面顶部隐藏,滚动后显示,平滑滚动
- 评论按钮点击定位到评论区(NAV 下方)
- 赋能按钮作者可见但禁止自赋能,点击弹出 toast 提示
- 未登录用户赋能/收藏点击跳转登录页
- postReactions 引用统一改为 postFloatBar
2026-06-02 23:45:51 +08:00

95 lines
3.1 KiB
JavaScript

// 赋能按钮 + 弹窗
(function() {
var btn = document.getElementById('energizeBtn');
var overlay = document.getElementById('energizeOverlay');
var closeBtn = document.getElementById('energizeClose');
var capHint = document.getElementById('energizeCapHint');
var lightBtn = document.getElementById('energizeLight');
var heavyBtn = document.getElementById('energizeHeavy');
if (!btn || !overlay) return;
var postId = btn.dataset.id;
var isAuthor = btn.dataset.uid && btn.dataset.authorUid &&
btn.dataset.uid === btn.dataset.authorUid;
function showToast(msg) {
var toast = document.createElement('div');
toast.className = 'energize-toast';
toast.textContent = msg;
document.body.appendChild(toast);
setTimeout(function() { toast.classList.add('show'); }, 10);
setTimeout(function() {
toast.classList.remove('show');
setTimeout(function() { toast.remove(); }, 300);
}, 2000);
}
function showOverlay() {
overlay.style.display = 'flex';
}
function hideOverlay() {
overlay.style.display = 'none';
capHint.style.display = 'none';
lightBtn.disabled = false;
heavyBtn.disabled = false;
}
btn.addEventListener('click', function() {
if (isAuthor) {
showToast('无法给自己赋能');
return;
}
fetch('/api/energy/info')
.then(function(r) { return r.json(); })
.then(function(d) {
if (d.success && d.data && d.data.daily_exp_cap_reached) {
capHint.style.display = 'block';
}
showOverlay();
})
.catch(function() { showOverlay(); });
});
closeBtn.addEventListener('click', hideOverlay);
overlay.addEventListener('click', function(e) {
if (e.target === overlay) hideOverlay();
});
function doEnergize(amount) {
lightBtn.disabled = true;
heavyBtn.disabled = true;
fetch('/api/energy/energize', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-CSRF-Token': getCSRFToken()
},
body: JSON.stringify({ post_id: parseInt(postId), amount: amount })
})
.then(function(r) { return r.json(); })
.then(function(d) {
if (d.success) {
alert(d.data.message || '赋能成功!');
hideOverlay();
} else {
alert(d.message || '赋能失败');
lightBtn.disabled = false;
heavyBtn.disabled = false;
}
})
.catch(function() {
alert('请求失败');
lightBtn.disabled = false;
heavyBtn.disabled = false;
});
}
lightBtn.addEventListener('click', function() {
doEnergize(parseInt(lightBtn.dataset.amount));
});
heavyBtn.addEventListener('click', function() {
doEnergize(parseInt(heavyBtn.dataset.amount));
});
})();