feat: prompt/confirm 替换为自定义弹窗
- shared/utils.js 新增 showConfirm()/showPrompt() 通用函数(inline CSS,无需额外样式) - admin/common.js 移除重复 showConfirm(),统一使用 utils.js - admin/posts.js: 3 处 confirm/prompt 替换(审核/退回/锁定) - admin/comments.js: 1 处 confirm 替换 - settings/index.html: 4 处 confirm + 1 处 prompt 替换 - studio/posts.html + drafts.html: 3 处 confirm 替换 - post-view.js + login.js + post-favorite.js: 3 处 confirm 替换
This commit is contained in:
@ -55,9 +55,10 @@
|
||||
for (var i = 0; i < btns.length; i++) {
|
||||
if (btns[i]._bound) continue;
|
||||
btns[i]._bound = true;
|
||||
btns[i].addEventListener('click', function() {
|
||||
btns[i].addEventListener('click', async function() {
|
||||
var id = this.dataset.id;
|
||||
if (!confirm('确认删除此评论?')) return;
|
||||
var ok = await showConfirm('确认', '确认删除此评论?');
|
||||
if (!ok) return;
|
||||
fetch('/api/admin/comments/' + id, {
|
||||
method: 'DELETE',
|
||||
headers: { 'X-CSRF-Token': csrfToken }
|
||||
|
||||
@ -37,36 +37,6 @@ const api = {
|
||||
}
|
||||
};
|
||||
|
||||
// --- 确认弹窗 ---
|
||||
function showConfirm(title, message) {
|
||||
return new Promise((resolve) => {
|
||||
const overlay = document.createElement('div');
|
||||
overlay.className = 'modal-overlay show';
|
||||
overlay.innerHTML = `
|
||||
<div class="modal-box">
|
||||
<h3>${escapeHtml(title)}</h3>
|
||||
<p>${escapeHtml(message)}</p>
|
||||
<div class="modal-actions">
|
||||
<button class="btn btn-outline" id="modalCancel">取消</button>
|
||||
<button class="btn btn-danger" id="modalConfirm">确认</button>
|
||||
</div>
|
||||
</div>`;
|
||||
document.body.appendChild(overlay);
|
||||
|
||||
overlay.querySelector('#modalCancel').onclick = () => {
|
||||
overlay.remove();
|
||||
resolve(false);
|
||||
};
|
||||
overlay.querySelector('#modalConfirm').onclick = () => {
|
||||
overlay.remove();
|
||||
resolve(true);
|
||||
};
|
||||
overlay.addEventListener('click', (e) => {
|
||||
if (e.target === overlay) { overlay.remove(); resolve(false); }
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// --- 角色标签 ---
|
||||
// 数据源由服务端 model.RoleDisplayNames 注入为 window.__ROLE_NAMES
|
||||
function roleLabel(role) {
|
||||
|
||||
@ -1,48 +1,53 @@
|
||||
(function() {
|
||||
// CSRF token 由 shared/utils.js 的 getCSRFToken() 统一提供
|
||||
// showConfirm / showPrompt / escapeHtml 由 shared/utils.js 统一提供
|
||||
// api 对象 (api.get/api.post/api.put) 由 admin/common.js 统一提供
|
||||
|
||||
function refreshPage() { window.location.reload(); }
|
||||
|
||||
function handleClick(sel, actionFn) {
|
||||
function handleClick(sel, confirmMsg, actionFn) {
|
||||
var btns = document.querySelectorAll(sel);
|
||||
btns.forEach(function(btn) {
|
||||
btn.addEventListener('click', function() {
|
||||
btn.addEventListener('click', async function() {
|
||||
var id = btn.getAttribute('data-id');
|
||||
if (confirm('确定执行此操作吗?')) {
|
||||
actionFn(id);
|
||||
if (confirmMsg) {
|
||||
var ok = await showConfirm('确认', confirmMsg);
|
||||
if (!ok) return;
|
||||
}
|
||||
actionFn(id);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// 审核通过
|
||||
handleClick('.post-approve', function(id) {
|
||||
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('操作失败'); });
|
||||
});
|
||||
|
||||
// 退回
|
||||
handleClick('.post-reject', function(id) {
|
||||
var reason = prompt('请输入退回理由(必填):');
|
||||
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('操作失败'); });
|
||||
handleClick('.post-reject', null, function(id) {
|
||||
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('操作失败'); });
|
||||
});
|
||||
});
|
||||
|
||||
// 锁定
|
||||
handleClick('.post-lock', function(id) {
|
||||
var reason = prompt('请输入锁定理由(必填):');
|
||||
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('操作失败'); });
|
||||
handleClick('.post-lock', null, function(id) {
|
||||
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('操作失败'); });
|
||||
});
|
||||
});
|
||||
|
||||
// 解锁
|
||||
handleClick('.post-unlock', function(id) {
|
||||
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('操作失败'); });
|
||||
|
||||
Reference in New Issue
Block a user