Files
mce/templates/MetaLab-2026/static/js/post-energize.js
Victor_Jay 1af9fba291 feat: 赋能溢出截断 + 前端按已赋能量禁用按钮
- 单篇赋能上限由硬拒绝改为溢出截断,类似每日经验处理
- 前端弹窗时查询文章已赋能总量,>=10 禁用重赋,>=20 全部禁用
- GetEnergyInfo 支持 ?post_id= 返回 post_energize_total
- 文案精简为'赋能成功!+N 经验'等短句
2026-06-03 00:13:03 +08:00

122 lines
3.9 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;
}
fetch('/api/energy/info?post_id=' + postId)
.then(function(r) { return r.json(); })
.then(function(d) {
if (d.success && d.data) {
if (d.data.daily_exp_cap_reached) {
capHint.style.display = 'block';
}
var total = d.data.post_energize_total || 0;
if (total >= 20) {
disableAll();
} else if (total >= 10) {
disableHeavy();
}
}
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));
});
})();