refactor: 全局移除浏览器 alert 弹窗,统一使用页面内 showToast 通知

- 替换全部 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() 自定义确认弹窗(需用户确认的操作)
This commit is contained in:
2026-06-03 01:25:12 +08:00
parent 864a488ee0
commit 6c9a4414cb
11 changed files with 42 additions and 75 deletions

View File

@ -1368,24 +1368,3 @@
margin-top: 2px;
padding-top: 6px;
}
/* 赋能 Toast */
.energize-toast {
position: fixed;
top: 80px;
left: 50%;
transform: translateX(-50%);
background: #1f2937;
color: #fff;
padding: 10px 24px;
border-radius: 8px;
font-size: 14px;
opacity: 0;
transition: opacity .3s;
z-index: 10000;
pointer-events: none;
}
.energize-toast.show {
opacity: 1;
}

View File

@ -201,7 +201,7 @@
var input = document.getElementById('replyInput-' + id);
if (!input) return;
var body = input.value.trim();
if (!body) { alert('请输入回复内容'); return; }
if (!body) { showToast('请输入回复内容', 'warning'); return; }
btn.disabled = true;
api('POST', '/api/comments/' + id + '/reply', { body: body })
@ -236,12 +236,12 @@
}
updateTotalCount(1);
} else {
alert(d.message || '回复失败');
showToast(d.message || '回复失败', 'error');
}
})
.catch(function() {
btn.disabled = false;
alert('请求失败');
showToast('请求失败', 'error');
});
});
}
@ -406,7 +406,7 @@
if (commentSubmitBtn) {
commentSubmitBtn.addEventListener('click', function() {
var body = commentInput.value.trim();
if (!body) { alert('请输入评论内容'); return; }
if (!body) { showToast('请输入评论内容', 'warning'); return; }
commentSubmitBtn.disabled = true;
api('POST', '/api/posts/' + postId + '/comments', { body: body })
@ -419,12 +419,12 @@
loadComments(currentPage);
updateTotalCount(1);
} else {
alert(d.message || '发表失败');
showToast(d.message || '发表失败', 'error');
}
})
.catch(function() {
commentSubmitBtn.disabled = false;
alert('请求失败');
showToast('请求失败', 'error');
});
});
@ -483,14 +483,14 @@
input.focus();
input.selectionStart = input.selectionEnd = start + imgUrl.length;
} else {
alert(d.message || '上传失败');
showToast(d.message || '上传失败', 'error');
}
})
.catch(function() {
uploadLabel.style.pointerEvents = 'auto';
uploadLabel.style.opacity = '1';
uploadLabel.innerHTML = '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="16" height="16"><rect x="3" y="3" width="18" height="18" rx="2" ry="2"/><circle cx="8.5" cy="8.5" r="1.5"/><polyline points="21 15 16 10 5 21"/></svg> 图片';
alert('上传失败');
showToast('上传失败', 'error');
});
imageUploadInput.value = '';

View File

@ -12,18 +12,6 @@
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';
}
@ -55,7 +43,7 @@
btn.addEventListener('click', function() {
if (isAuthor) {
showToast('无法给自己赋能');
showToast('无法给自己赋能', 'warning');
return;
}
btn.disabled = true;
@ -64,7 +52,7 @@
.then(function(d) {
btn.disabled = false;
if (!d.success || !d.data) {
showToast('获取状态失败,请稍后重试');
showToast('获取状态失败,请稍后重试', 'error');
return;
}
if (d.data.daily_exp_cap_reached) {
@ -72,7 +60,7 @@
}
var total = d.data.post_energize_total || 0;
if (total >= 20) {
showToast('对该文章赋能已达上限');
showToast('对该文章赋能已达上限', 'warning');
return;
}
if (total >= 10) {
@ -82,7 +70,7 @@
})
.catch(function() {
btn.disabled = false;
showToast('获取状态失败,请稍后重试');
showToast('获取状态失败,请稍后重试', 'error');
});
});
@ -106,16 +94,16 @@
.then(function(r) { return r.json(); })
.then(function(d) {
if (d.success) {
alert(d.data.message || '赋能成功!');
showToast(d.data && d.data.message ? d.data.message : '赋能成功!', 'success');
hideOverlay();
} else {
alert(d.message || '赋能失败');
showToast(d.message || '赋能失败', 'error');
lightBtn.disabled = false;
heavyBtn.disabled = false;
}
})
.catch(function() {
alert('请求失败');
showToast('请求失败', 'error');
lightBtn.disabled = false;
heavyBtn.disabled = false;
});

View File

@ -117,7 +117,7 @@
updateBtn(true);
hideModal();
} else {
alert(d.message || '操作失败');
showToast(d.message || '操作失败', 'error');
}
})
.catch(function() { loading = false; });
@ -137,7 +137,7 @@
if (favCountEl) favCountEl.textContent = Math.max(0, (parseInt(favCountEl.textContent) || 0) - 1);
hideModal();
} else {
alert(d.message || '操作失败');
showToast(d.message || '操作失败', 'error');
}
})
.catch(function() { loading = false; });
@ -155,7 +155,7 @@
loading = false;
newBtn.disabled = false;
newBtn.textContent = '创建并收藏';
alert(d.message || '创建失败');
showToast(d.message || '创建失败', 'error');
return;
}
var newId = d.data && d.data.id;
@ -170,7 +170,7 @@
if (favCountEl) favCountEl.textContent = (parseInt(favCountEl.textContent) || 0) + 1;
hideModal();
} else {
alert(d2.message || '操作失败');
showToast(d2.message || '操作失败', 'error');
}
});
})
@ -212,8 +212,8 @@
if (newBtn && newInput) {
newBtn.addEventListener('click', function() {
var name = newInput.value.trim();
if (!name) { alert('请输入收藏夹名称'); return; }
if (name.length > 50) { alert('收藏夹名称不能超过 50 个字符'); return; }
if (!name) { showToast('请输入收藏夹名称', 'warning'); return; }
if (name.length > 50) { showToast('收藏夹名称不能超过 50 个字符', 'warning'); return; }
createAndAdd(name);
});
newInput.addEventListener('keydown', function(e) {

View File

@ -41,7 +41,7 @@
if (d.success) {
updateUI(d.data.liked ? 'liked' : 'none', d.data.likes_count);
} else {
alert(d.message || '操作失败');
showToast(d.message || '操作失败', 'error');
}
})
.catch(function() { likeBtn.disabled = false; });
@ -63,7 +63,7 @@
if (d.success) {
updateUI(d.data.disliked ? 'disliked' : 'none');
} else {
alert(d.message || '操作失败');
showToast(d.message || '操作失败', 'error');
}
})
.catch(function() { dislikeBtn.disabled = false; });

View File

@ -41,9 +41,9 @@
.then(function(r) { return r.json(); })
.then(function(d) {
if (d.success) { window.location.reload(); }
else { alert(d.message || '提交失败'); }
else { showToast(d.message || '提交失败', 'error'); }
})
.catch(function() { alert('请求失败'); });
.catch(function() { showToast('请求失败', 'error'); });
});
});
}

View File

@ -169,10 +169,10 @@ async function handleSubmit(e) {
try {
const postId = document.getElementById('postId')?.value
const title = document.getElementById('postTitle')?.value.trim()
if (!title) { alert('请输入标题'); return }
if (!title) { showToast('请输入标题', 'warning'); return }
const body = vditor ? vditor.getValue() : ''
if (!body.trim()) { alert('请输入正文'); return }
if (!body.trim()) { showToast('请输入正文', 'warning'); return }
const isEdit = !!postId
const url = isEdit ? '/api/studio/posts/' + postId : '/api/studio/posts'
@ -197,10 +197,10 @@ async function handleSubmit(e) {
window.location.href = '/studio/posts'
}
} else {
alert(data.message || '操作失败')
showToast(data.message || '操作失败', 'error')
}
} catch (err) {
alert('请求失败,请重试')
showToast('请求失败,请重试', 'error')
} finally {
submitting = false
if (submitBtn) {