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:
@ -705,7 +705,7 @@ function createFolder() {
|
|||||||
headers: {'Content-Type': 'application/json'},
|
headers: {'Content-Type': 'application/json'},
|
||||||
body: JSON.stringify({name: name, is_public: false})
|
body: JSON.stringify({name: name, is_public: false})
|
||||||
}).then(function(r) { return r.json(); }).then(function(d) {
|
}).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'},
|
headers: {'Content-Type': 'application/json'},
|
||||||
body: JSON.stringify({field: field, value: value})
|
body: JSON.stringify({field: field, value: value})
|
||||||
}).then(function(r){return r.json()}).then(function(d){
|
}).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));
|
}.bind(this));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@ -73,7 +73,7 @@ function submitPost(id) {
|
|||||||
if (data.success) {
|
if (data.success) {
|
||||||
location.reload();
|
location.reload();
|
||||||
} else {
|
} else {
|
||||||
alert(data.message || '提交失败');
|
showToast(data.message || '提交失败', 'error');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@ -94,7 +94,7 @@ function deletePost(id) {
|
|||||||
if (data.success) {
|
if (data.success) {
|
||||||
location.reload();
|
location.reload();
|
||||||
} else {
|
} else {
|
||||||
alert(data.message || '删除失败');
|
showToast(data.message || '删除失败', 'error');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@ -94,7 +94,7 @@ function deletePost(id) {
|
|||||||
if (data.success) {
|
if (data.success) {
|
||||||
location.reload();
|
location.reload();
|
||||||
} else {
|
} else {
|
||||||
alert(data.message || '删除失败');
|
showToast(data.message || '删除失败', 'error');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@ -1368,24 +1368,3 @@
|
|||||||
margin-top: 2px;
|
margin-top: 2px;
|
||||||
padding-top: 6px;
|
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;
|
|
||||||
}
|
|
||||||
|
|||||||
@ -201,7 +201,7 @@
|
|||||||
var input = document.getElementById('replyInput-' + id);
|
var input = document.getElementById('replyInput-' + id);
|
||||||
if (!input) return;
|
if (!input) return;
|
||||||
var body = input.value.trim();
|
var body = input.value.trim();
|
||||||
if (!body) { alert('请输入回复内容'); return; }
|
if (!body) { showToast('请输入回复内容', 'warning'); return; }
|
||||||
|
|
||||||
btn.disabled = true;
|
btn.disabled = true;
|
||||||
api('POST', '/api/comments/' + id + '/reply', { body: body })
|
api('POST', '/api/comments/' + id + '/reply', { body: body })
|
||||||
@ -236,12 +236,12 @@
|
|||||||
}
|
}
|
||||||
updateTotalCount(1);
|
updateTotalCount(1);
|
||||||
} else {
|
} else {
|
||||||
alert(d.message || '回复失败');
|
showToast(d.message || '回复失败', 'error');
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch(function() {
|
.catch(function() {
|
||||||
btn.disabled = false;
|
btn.disabled = false;
|
||||||
alert('请求失败');
|
showToast('请求失败', 'error');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -406,7 +406,7 @@
|
|||||||
if (commentSubmitBtn) {
|
if (commentSubmitBtn) {
|
||||||
commentSubmitBtn.addEventListener('click', function() {
|
commentSubmitBtn.addEventListener('click', function() {
|
||||||
var body = commentInput.value.trim();
|
var body = commentInput.value.trim();
|
||||||
if (!body) { alert('请输入评论内容'); return; }
|
if (!body) { showToast('请输入评论内容', 'warning'); return; }
|
||||||
|
|
||||||
commentSubmitBtn.disabled = true;
|
commentSubmitBtn.disabled = true;
|
||||||
api('POST', '/api/posts/' + postId + '/comments', { body: body })
|
api('POST', '/api/posts/' + postId + '/comments', { body: body })
|
||||||
@ -419,12 +419,12 @@
|
|||||||
loadComments(currentPage);
|
loadComments(currentPage);
|
||||||
updateTotalCount(1);
|
updateTotalCount(1);
|
||||||
} else {
|
} else {
|
||||||
alert(d.message || '发表失败');
|
showToast(d.message || '发表失败', 'error');
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch(function() {
|
.catch(function() {
|
||||||
commentSubmitBtn.disabled = false;
|
commentSubmitBtn.disabled = false;
|
||||||
alert('请求失败');
|
showToast('请求失败', 'error');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -483,14 +483,14 @@
|
|||||||
input.focus();
|
input.focus();
|
||||||
input.selectionStart = input.selectionEnd = start + imgUrl.length;
|
input.selectionStart = input.selectionEnd = start + imgUrl.length;
|
||||||
} else {
|
} else {
|
||||||
alert(d.message || '上传失败');
|
showToast(d.message || '上传失败', 'error');
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch(function() {
|
.catch(function() {
|
||||||
uploadLabel.style.pointerEvents = 'auto';
|
uploadLabel.style.pointerEvents = 'auto';
|
||||||
uploadLabel.style.opacity = '1';
|
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> 图片';
|
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 = '';
|
imageUploadInput.value = '';
|
||||||
|
|||||||
@ -12,18 +12,6 @@
|
|||||||
var isAuthor = btn.dataset.uid && btn.dataset.authorUid &&
|
var isAuthor = btn.dataset.uid && btn.dataset.authorUid &&
|
||||||
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() {
|
function showOverlay() {
|
||||||
overlay.style.display = 'flex';
|
overlay.style.display = 'flex';
|
||||||
}
|
}
|
||||||
@ -55,7 +43,7 @@
|
|||||||
|
|
||||||
btn.addEventListener('click', function() {
|
btn.addEventListener('click', function() {
|
||||||
if (isAuthor) {
|
if (isAuthor) {
|
||||||
showToast('无法给自己赋能');
|
showToast('无法给自己赋能', 'warning');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
btn.disabled = true;
|
btn.disabled = true;
|
||||||
@ -64,7 +52,7 @@
|
|||||||
.then(function(d) {
|
.then(function(d) {
|
||||||
btn.disabled = false;
|
btn.disabled = false;
|
||||||
if (!d.success || !d.data) {
|
if (!d.success || !d.data) {
|
||||||
showToast('获取状态失败,请稍后重试');
|
showToast('获取状态失败,请稍后重试', 'error');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (d.data.daily_exp_cap_reached) {
|
if (d.data.daily_exp_cap_reached) {
|
||||||
@ -72,7 +60,7 @@
|
|||||||
}
|
}
|
||||||
var total = d.data.post_energize_total || 0;
|
var total = d.data.post_energize_total || 0;
|
||||||
if (total >= 20) {
|
if (total >= 20) {
|
||||||
showToast('对该文章赋能已达上限');
|
showToast('对该文章赋能已达上限', 'warning');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (total >= 10) {
|
if (total >= 10) {
|
||||||
@ -82,7 +70,7 @@
|
|||||||
})
|
})
|
||||||
.catch(function() {
|
.catch(function() {
|
||||||
btn.disabled = false;
|
btn.disabled = false;
|
||||||
showToast('获取状态失败,请稍后重试');
|
showToast('获取状态失败,请稍后重试', 'error');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -106,16 +94,16 @@
|
|||||||
.then(function(r) { return r.json(); })
|
.then(function(r) { return r.json(); })
|
||||||
.then(function(d) {
|
.then(function(d) {
|
||||||
if (d.success) {
|
if (d.success) {
|
||||||
alert(d.data.message || '赋能成功!');
|
showToast(d.data && d.data.message ? d.data.message : '赋能成功!', 'success');
|
||||||
hideOverlay();
|
hideOverlay();
|
||||||
} else {
|
} else {
|
||||||
alert(d.message || '赋能失败');
|
showToast(d.message || '赋能失败', 'error');
|
||||||
lightBtn.disabled = false;
|
lightBtn.disabled = false;
|
||||||
heavyBtn.disabled = false;
|
heavyBtn.disabled = false;
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch(function() {
|
.catch(function() {
|
||||||
alert('请求失败');
|
showToast('请求失败', 'error');
|
||||||
lightBtn.disabled = false;
|
lightBtn.disabled = false;
|
||||||
heavyBtn.disabled = false;
|
heavyBtn.disabled = false;
|
||||||
});
|
});
|
||||||
|
|||||||
@ -117,7 +117,7 @@
|
|||||||
updateBtn(true);
|
updateBtn(true);
|
||||||
hideModal();
|
hideModal();
|
||||||
} else {
|
} else {
|
||||||
alert(d.message || '操作失败');
|
showToast(d.message || '操作失败', 'error');
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch(function() { loading = false; });
|
.catch(function() { loading = false; });
|
||||||
@ -137,7 +137,7 @@
|
|||||||
if (favCountEl) favCountEl.textContent = Math.max(0, (parseInt(favCountEl.textContent) || 0) - 1);
|
if (favCountEl) favCountEl.textContent = Math.max(0, (parseInt(favCountEl.textContent) || 0) - 1);
|
||||||
hideModal();
|
hideModal();
|
||||||
} else {
|
} else {
|
||||||
alert(d.message || '操作失败');
|
showToast(d.message || '操作失败', 'error');
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch(function() { loading = false; });
|
.catch(function() { loading = false; });
|
||||||
@ -155,7 +155,7 @@
|
|||||||
loading = false;
|
loading = false;
|
||||||
newBtn.disabled = false;
|
newBtn.disabled = false;
|
||||||
newBtn.textContent = '创建并收藏';
|
newBtn.textContent = '创建并收藏';
|
||||||
alert(d.message || '创建失败');
|
showToast(d.message || '创建失败', 'error');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
var newId = d.data && d.data.id;
|
var newId = d.data && d.data.id;
|
||||||
@ -170,7 +170,7 @@
|
|||||||
if (favCountEl) favCountEl.textContent = (parseInt(favCountEl.textContent) || 0) + 1;
|
if (favCountEl) favCountEl.textContent = (parseInt(favCountEl.textContent) || 0) + 1;
|
||||||
hideModal();
|
hideModal();
|
||||||
} else {
|
} else {
|
||||||
alert(d2.message || '操作失败');
|
showToast(d2.message || '操作失败', 'error');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
@ -212,8 +212,8 @@
|
|||||||
if (newBtn && newInput) {
|
if (newBtn && newInput) {
|
||||||
newBtn.addEventListener('click', function() {
|
newBtn.addEventListener('click', function() {
|
||||||
var name = newInput.value.trim();
|
var name = newInput.value.trim();
|
||||||
if (!name) { alert('请输入收藏夹名称'); return; }
|
if (!name) { showToast('请输入收藏夹名称', 'warning'); return; }
|
||||||
if (name.length > 50) { alert('收藏夹名称不能超过 50 个字符'); return; }
|
if (name.length > 50) { showToast('收藏夹名称不能超过 50 个字符', 'warning'); return; }
|
||||||
createAndAdd(name);
|
createAndAdd(name);
|
||||||
});
|
});
|
||||||
newInput.addEventListener('keydown', function(e) {
|
newInput.addEventListener('keydown', function(e) {
|
||||||
|
|||||||
@ -41,7 +41,7 @@
|
|||||||
if (d.success) {
|
if (d.success) {
|
||||||
updateUI(d.data.liked ? 'liked' : 'none', d.data.likes_count);
|
updateUI(d.data.liked ? 'liked' : 'none', d.data.likes_count);
|
||||||
} else {
|
} else {
|
||||||
alert(d.message || '操作失败');
|
showToast(d.message || '操作失败', 'error');
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch(function() { likeBtn.disabled = false; });
|
.catch(function() { likeBtn.disabled = false; });
|
||||||
@ -63,7 +63,7 @@
|
|||||||
if (d.success) {
|
if (d.success) {
|
||||||
updateUI(d.data.disliked ? 'disliked' : 'none');
|
updateUI(d.data.disliked ? 'disliked' : 'none');
|
||||||
} else {
|
} else {
|
||||||
alert(d.message || '操作失败');
|
showToast(d.message || '操作失败', 'error');
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch(function() { dislikeBtn.disabled = false; });
|
.catch(function() { dislikeBtn.disabled = false; });
|
||||||
|
|||||||
@ -41,9 +41,9 @@
|
|||||||
.then(function(r) { return r.json(); })
|
.then(function(r) { return r.json(); })
|
||||||
.then(function(d) {
|
.then(function(d) {
|
||||||
if (d.success) { window.location.reload(); }
|
if (d.success) { window.location.reload(); }
|
||||||
else { alert(d.message || '提交失败'); }
|
else { showToast(d.message || '提交失败', 'error'); }
|
||||||
})
|
})
|
||||||
.catch(function() { alert('请求失败'); });
|
.catch(function() { showToast('请求失败', 'error'); });
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@ -169,10 +169,10 @@ async function handleSubmit(e) {
|
|||||||
try {
|
try {
|
||||||
const postId = document.getElementById('postId')?.value
|
const postId = document.getElementById('postId')?.value
|
||||||
const title = document.getElementById('postTitle')?.value.trim()
|
const title = document.getElementById('postTitle')?.value.trim()
|
||||||
if (!title) { alert('请输入标题'); return }
|
if (!title) { showToast('请输入标题', 'warning'); return }
|
||||||
|
|
||||||
const body = vditor ? vditor.getValue() : ''
|
const body = vditor ? vditor.getValue() : ''
|
||||||
if (!body.trim()) { alert('请输入正文'); return }
|
if (!body.trim()) { showToast('请输入正文', 'warning'); return }
|
||||||
|
|
||||||
const isEdit = !!postId
|
const isEdit = !!postId
|
||||||
const url = isEdit ? '/api/studio/posts/' + postId : '/api/studio/posts'
|
const url = isEdit ? '/api/studio/posts/' + postId : '/api/studio/posts'
|
||||||
@ -197,10 +197,10 @@ async function handleSubmit(e) {
|
|||||||
window.location.href = '/studio/posts'
|
window.location.href = '/studio/posts'
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
alert(data.message || '操作失败')
|
showToast(data.message || '操作失败', 'error')
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
alert('请求失败,请重试')
|
showToast('请求失败,请重试', 'error')
|
||||||
} finally {
|
} finally {
|
||||||
submitting = false
|
submitting = false
|
||||||
if (submitBtn) {
|
if (submitBtn) {
|
||||||
|
|||||||
@ -22,8 +22,8 @@
|
|||||||
// 审核通过
|
// 审核通过
|
||||||
handleClick('.post-approve', '确定审核通过此文章吗?', function(id) {
|
handleClick('.post-approve', '确定审核通过此文章吗?', function(id) {
|
||||||
api.post('/api/admin/posts/' + id + '/approve')
|
api.post('/api/admin/posts/' + id + '/approve')
|
||||||
.then(function(d) { if (d.success) refreshPage(); else alert(d.message); })
|
.then(function(d) { if (d.success) refreshPage(); else showToast(d.message || '操作失败', 'error'); })
|
||||||
.catch(function() { alert('操作失败'); });
|
.catch(function() { showToast('操作失败', 'error'); });
|
||||||
});
|
});
|
||||||
|
|
||||||
// 退回
|
// 退回
|
||||||
@ -31,8 +31,8 @@
|
|||||||
showPrompt('请输入退回理由(必填):').then(function(reason) {
|
showPrompt('请输入退回理由(必填):').then(function(reason) {
|
||||||
if (!reason || !reason.trim()) return;
|
if (!reason || !reason.trim()) return;
|
||||||
api.post('/api/admin/posts/' + id + '/reject', { reason: reason.trim() })
|
api.post('/api/admin/posts/' + id + '/reject', { reason: reason.trim() })
|
||||||
.then(function(d) { if (d.success) refreshPage(); else alert(d.message); })
|
.then(function(d) { if (d.success) refreshPage(); else showToast(d.message || '操作失败', 'error'); })
|
||||||
.catch(function() { alert('操作失败'); });
|
.catch(function() { showToast('操作失败', 'error'); });
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -41,15 +41,15 @@
|
|||||||
showPrompt('请输入锁定理由(必填):').then(function(reason) {
|
showPrompt('请输入锁定理由(必填):').then(function(reason) {
|
||||||
if (!reason || !reason.trim()) return;
|
if (!reason || !reason.trim()) return;
|
||||||
api.post('/api/admin/posts/' + id + '/lock', { reason: reason.trim() })
|
api.post('/api/admin/posts/' + id + '/lock', { reason: reason.trim() })
|
||||||
.then(function(d) { if (d.success) refreshPage(); else alert(d.message); })
|
.then(function(d) { if (d.success) refreshPage(); else showToast(d.message || '操作失败', 'error'); })
|
||||||
.catch(function() { alert('操作失败'); });
|
.catch(function() { showToast('操作失败', 'error'); });
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// 解锁
|
// 解锁
|
||||||
handleClick('.post-unlock', '确定解锁此文章吗?', function(id) {
|
handleClick('.post-unlock', '确定解锁此文章吗?', function(id) {
|
||||||
api.post('/api/admin/posts/' + id + '/unlock')
|
api.post('/api/admin/posts/' + id + '/unlock')
|
||||||
.then(function(d) { if (d.success) refreshPage(); else alert(d.message); })
|
.then(function(d) { if (d.success) refreshPage(); else showToast(d.message || '操作失败', 'error'); })
|
||||||
.catch(function() { alert('操作失败'); });
|
.catch(function() { showToast('操作失败', 'error'); });
|
||||||
});
|
});
|
||||||
})();
|
})();
|
||||||
|
|||||||
Reference in New Issue
Block a user