- 替换全部 11 个文件中 36 处 alert() 为 showToast() - API 错误/异常使用 showToast(msg, 'error'),3秒自动消失 - 表单校验提示使用 showToast(msg, 'warning'),3秒自动消失 - 成功确认使用 showToast(msg, 'success'),3秒自动消失 - 移除 post-energize.js 中与全局 showToast 冲突的本地实现 - 删除 posts.css 中已失去引用的 .energize-toast 样式 - 保留 showConfirm() 自定义确认弹窗(需用户确认的操作)
119 lines
3.9 KiB
JavaScript
119 lines
3.9 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 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('无法给自己赋能', 'warning');
|
||
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('获取状态失败,请稍后重试', 'error');
|
||
return;
|
||
}
|
||
if (d.data.daily_exp_cap_reached) {
|
||
capHint.style.display = 'block';
|
||
}
|
||
var total = d.data.post_energize_total || 0;
|
||
if (total >= 20) {
|
||
showToast('对该文章赋能已达上限', 'warning');
|
||
return;
|
||
}
|
||
if (total >= 10) {
|
||
disableHeavy();
|
||
}
|
||
showOverlay();
|
||
})
|
||
.catch(function() {
|
||
btn.disabled = false;
|
||
showToast('获取状态失败,请稍后重试', 'error');
|
||
});
|
||
});
|
||
|
||
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) {
|
||
showToast(d.data && d.data.message ? d.data.message : '赋能成功!', 'success');
|
||
hideOverlay();
|
||
} else {
|
||
showToast(d.message || '赋能失败', 'error');
|
||
lightBtn.disabled = false;
|
||
heavyBtn.disabled = false;
|
||
}
|
||
})
|
||
.catch(function() {
|
||
showToast('请求失败', 'error');
|
||
lightBtn.disabled = false;
|
||
heavyBtn.disabled = false;
|
||
});
|
||
}
|
||
|
||
lightBtn.addEventListener('click', function() {
|
||
doEnergize(parseInt(lightBtn.dataset.amount));
|
||
});
|
||
heavyBtn.addEventListener('click', function() {
|
||
doEnergize(parseInt(heavyBtn.dataset.amount));
|
||
});
|
||
})();
|