From 6c9a4414cbaca10cf36e05aba62e301ba7c64a7a Mon Sep 17 00:00:00 2001 From: Victor_Jay Date: Wed, 3 Jun 2026 01:25:12 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E5=85=A8=E5=B1=80=E7=A7=BB?= =?UTF-8?q?=E9=99=A4=E6=B5=8F=E8=A7=88=E5=99=A8=20alert=20=E5=BC=B9?= =?UTF-8?q?=E7=AA=97=EF=BC=8C=E7=BB=9F=E4=B8=80=E4=BD=BF=E7=94=A8=E9=A1=B5?= =?UTF-8?q?=E9=9D=A2=E5=86=85=20showToast=20=E9=80=9A=E7=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 替换全部 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() 自定义确认弹窗(需用户确认的操作) --- templates/MetaLab-2026/html/space/index.html | 4 +-- .../MetaLab-2026/html/studio/drafts.html | 4 +-- templates/MetaLab-2026/html/studio/posts.html | 2 +- templates/MetaLab-2026/static/css/posts.css | 21 --------------- .../MetaLab-2026/static/js/post-comments.js | 16 ++++++------ .../MetaLab-2026/static/js/post-energize.js | 26 +++++-------------- .../MetaLab-2026/static/js/post-favorite.js | 12 ++++----- .../MetaLab-2026/static/js/post-reactions.js | 4 +-- templates/MetaLab-2026/static/js/post-view.js | 4 +-- .../MetaLab-2026/static/js/studio-editor.js | 8 +++--- templates/admin/static/js/posts.js | 16 ++++++------ 11 files changed, 42 insertions(+), 75 deletions(-) diff --git a/templates/MetaLab-2026/html/space/index.html b/templates/MetaLab-2026/html/space/index.html index 187eeb1..5179367 100644 --- a/templates/MetaLab-2026/html/space/index.html +++ b/templates/MetaLab-2026/html/space/index.html @@ -705,7 +705,7 @@ function createFolder() { headers: {'Content-Type': 'application/json'}, body: JSON.stringify({name: name, is_public: false}) }).then(function(r) { return r.json(); }).then(function(d) { - if (d.success) { location.reload(); } else { alert(d.message || '创建失败'); } + if (d.success) { location.reload(); } else { showToast(d.message || '创建失败', 'error'); } }); } @@ -743,7 +743,7 @@ document.querySelectorAll('#privacy-follow-list, #privacy-follower-list').forEac headers: {'Content-Type': 'application/json'}, body: JSON.stringify({field: field, value: value}) }).then(function(r){return r.json()}).then(function(d){ - if(!d.success) { this.checked=!value; alert(d.message||'保存失败'); } + if(!d.success) { this.checked=!value; showToast(d.message||'保存失败', 'error'); } }.bind(this)); }); }); diff --git a/templates/MetaLab-2026/html/studio/drafts.html b/templates/MetaLab-2026/html/studio/drafts.html index 5768f27..837ea6b 100644 --- a/templates/MetaLab-2026/html/studio/drafts.html +++ b/templates/MetaLab-2026/html/studio/drafts.html @@ -73,7 +73,7 @@ function submitPost(id) { if (data.success) { location.reload(); } else { - alert(data.message || '提交失败'); + showToast(data.message || '提交失败', 'error'); } }); }); @@ -94,7 +94,7 @@ function deletePost(id) { if (data.success) { location.reload(); } else { - alert(data.message || '删除失败'); + showToast(data.message || '删除失败', 'error'); } }); }); diff --git a/templates/MetaLab-2026/html/studio/posts.html b/templates/MetaLab-2026/html/studio/posts.html index 56971a2..aa73fda 100644 --- a/templates/MetaLab-2026/html/studio/posts.html +++ b/templates/MetaLab-2026/html/studio/posts.html @@ -94,7 +94,7 @@ function deletePost(id) { if (data.success) { location.reload(); } else { - alert(data.message || '删除失败'); + showToast(data.message || '删除失败', 'error'); } }); }); diff --git a/templates/MetaLab-2026/static/css/posts.css b/templates/MetaLab-2026/static/css/posts.css index 97c6ffe..6c2efd3 100644 --- a/templates/MetaLab-2026/static/css/posts.css +++ b/templates/MetaLab-2026/static/css/posts.css @@ -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; -} diff --git a/templates/MetaLab-2026/static/js/post-comments.js b/templates/MetaLab-2026/static/js/post-comments.js index a196bd4..96f31ad 100644 --- a/templates/MetaLab-2026/static/js/post-comments.js +++ b/templates/MetaLab-2026/static/js/post-comments.js @@ -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 = ' 图片'; - alert('上传失败'); + showToast('上传失败', 'error'); }); imageUploadInput.value = ''; diff --git a/templates/MetaLab-2026/static/js/post-energize.js b/templates/MetaLab-2026/static/js/post-energize.js index 2cf37f6..9d17fe4 100644 --- a/templates/MetaLab-2026/static/js/post-energize.js +++ b/templates/MetaLab-2026/static/js/post-energize.js @@ -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; }); diff --git a/templates/MetaLab-2026/static/js/post-favorite.js b/templates/MetaLab-2026/static/js/post-favorite.js index dec708d..824ac1a 100644 --- a/templates/MetaLab-2026/static/js/post-favorite.js +++ b/templates/MetaLab-2026/static/js/post-favorite.js @@ -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) { diff --git a/templates/MetaLab-2026/static/js/post-reactions.js b/templates/MetaLab-2026/static/js/post-reactions.js index 9bedce2..5d5f4bc 100644 --- a/templates/MetaLab-2026/static/js/post-reactions.js +++ b/templates/MetaLab-2026/static/js/post-reactions.js @@ -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; }); diff --git a/templates/MetaLab-2026/static/js/post-view.js b/templates/MetaLab-2026/static/js/post-view.js index beaf5ca..c0ea656 100644 --- a/templates/MetaLab-2026/static/js/post-view.js +++ b/templates/MetaLab-2026/static/js/post-view.js @@ -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'); }); }); }); } diff --git a/templates/MetaLab-2026/static/js/studio-editor.js b/templates/MetaLab-2026/static/js/studio-editor.js index 8a2f95f..e884c63 100644 --- a/templates/MetaLab-2026/static/js/studio-editor.js +++ b/templates/MetaLab-2026/static/js/studio-editor.js @@ -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) { diff --git a/templates/admin/static/js/posts.js b/templates/admin/static/js/posts.js index 9113a5f..23ba280 100644 --- a/templates/admin/static/js/posts.js +++ b/templates/admin/static/js/posts.js @@ -22,8 +22,8 @@ // 审核通过 handleClick('.post-approve', '确定审核通过此文章吗?', function(id) { api.post('/api/admin/posts/' + id + '/approve') - .then(function(d) { if (d.success) refreshPage(); else alert(d.message); }) - .catch(function() { alert('操作失败'); }); + .then(function(d) { if (d.success) refreshPage(); else showToast(d.message || '操作失败', 'error'); }) + .catch(function() { showToast('操作失败', 'error'); }); }); // 退回 @@ -31,8 +31,8 @@ showPrompt('请输入退回理由(必填):').then(function(reason) { if (!reason || !reason.trim()) return; api.post('/api/admin/posts/' + id + '/reject', { reason: reason.trim() }) - .then(function(d) { if (d.success) refreshPage(); else alert(d.message); }) - .catch(function() { alert('操作失败'); }); + .then(function(d) { if (d.success) refreshPage(); else showToast(d.message || '操作失败', 'error'); }) + .catch(function() { showToast('操作失败', 'error'); }); }); }); @@ -41,15 +41,15 @@ showPrompt('请输入锁定理由(必填):').then(function(reason) { if (!reason || !reason.trim()) return; api.post('/api/admin/posts/' + id + '/lock', { reason: reason.trim() }) - .then(function(d) { if (d.success) refreshPage(); else alert(d.message); }) - .catch(function() { alert('操作失败'); }); + .then(function(d) { if (d.success) refreshPage(); else showToast(d.message || '操作失败', 'error'); }) + .catch(function() { showToast('操作失败', 'error'); }); }); }); // 解锁 handleClick('.post-unlock', '确定解锁此文章吗?', function(id) { api.post('/api/admin/posts/' + id + '/unlock') - .then(function(d) { if (d.success) refreshPage(); else alert(d.message); }) - .catch(function() { alert('操作失败'); }); + .then(function(d) { if (d.success) refreshPage(); else showToast(d.message || '操作失败', 'error'); }) + .catch(function() { showToast('操作失败', 'error'); }); }); })();