Files
mce/templates/MetaLab-2026/static/js/post-energize.js
Victor_Jay 2d05da6709 fix: 赋能弹窗已满时不再弹出 + API 补上 post_energize_total 字段
- 赋能按钮点击时先查已赋能总量,>=20 直接 showToast 不弹窗
- 修复控制响应缺失 post_energize_total 字段导致前端永远走到 showOverlay
- fetch 失败/.catch 改为 showToast 而非 showOverlay
2026-06-03 00:23:35 +08:00

131 lines
4.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 赋能按钮 + 弹窗
(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;
heavyBtn.title = '';
heavyBtn.style.opacity = '';
lightBtn.title = '';
lightBtn.style.opacity = '';
}
function disableHeavy() {
heavyBtn.disabled = true;
heavyBtn.title = '已达单篇上限1/2';
heavyBtn.style.opacity = '0.4';
}
function disableAll() {
lightBtn.disabled = true;
heavyBtn.disabled = true;
lightBtn.title = '已达单篇上限2/2';
lightBtn.style.opacity = '0.4';
heavyBtn.title = '已达单篇上限2/2';
heavyBtn.style.opacity = '0.4';
}
btn.addEventListener('click', function() {
if (isAuthor) {
showToast('无法给自己赋能');
return;
}
btn.disabled = true;
fetch('/api/energy/info?post_id=' + postId)
.then(function(r) { return r.json(); })
.then(function(d) {
btn.disabled = false;
if (!d.success || !d.data) {
showToast('获取状态失败,请稍后重试');
return;
}
if (d.data.daily_exp_cap_reached) {
capHint.style.display = 'block';
}
var total = d.data.post_energize_total || 0;
if (total >= 20) {
showToast('对该文章赋能已达上限');
return;
}
if (total >= 10) {
disableHeavy();
}
showOverlay();
})
.catch(function() {
btn.disabled = false;
showToast('获取状态失败,请稍后重试');
});
});
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));
});
})();