diff --git a/templates/admin/static/js/audit.js b/templates/admin/static/js/audit.js index b2aa786..1945d58 100644 --- a/templates/admin/static/js/audit.js +++ b/templates/admin/static/js/audit.js @@ -55,7 +55,10 @@ function loadTableAudits() { showToast(res.message || '加载失败', 'error'); } }) - .catch(() => showToast('网络错误', 'error')); + .catch((e) => { + console.error('loadTableAudits error:', e); + showToast('加载失败: ' + (e.message || '网络错误'), 'error'); + }); } function loadAvatarAudits() { @@ -69,7 +72,10 @@ function loadAvatarAudits() { showToast(res.message || '加载失败', 'error'); } }) - .catch(() => showToast('网络错误', 'error')); + .catch((e) => { + console.error('loadAvatarAudits error:', e); + showToast('加载失败: ' + (e.message || '网络错误'), 'error'); + }); } function loadHistory() { @@ -83,7 +89,10 @@ function loadHistory() { showToast(res.message || '加载失败', 'error'); } }) - .catch(() => showToast('网络错误', 'error')); + .catch((e) => { + console.error('loadHistory error:', e); + showToast('加载失败: ' + (e.message || '网络错误'), 'error'); + }); } /* ========================================= diff --git a/templates/admin/static/js/comments.js b/templates/admin/static/js/comments.js index 49cd797..b28aff3 100644 --- a/templates/admin/static/js/comments.js +++ b/templates/admin/static/js/comments.js @@ -11,16 +11,17 @@ var pagination = document.getElementById('commentPagination'); var loading = document.getElementById('commentLoading'); - // escapeHtml / formatTime / api 由 shared/utils.js 统一提供 + // escapeHtml / formatTime / showToast / showConfirm / getCSRFToken 由 shared/utils.js 提供 + // api (对象式) 由 admin/static/js/common.js 提供 function loadComments(page) { loading.style.display = 'block'; var url = '/api/admin/comments?page=' + page + '&page_size=20'; if (currentKeyword) url += '&keyword=' + encodeURIComponent(currentKeyword); - api('GET', url).then(function(d) { + api.get(url).then(function(d) { loading.style.display = 'none'; - if (!d.success) return; + if (!d.success) { showToast(d.message || '加载失败', 'error'); return; } totalPages = d.data.total_pages || 1; tbody.innerHTML = ''; var items = d.data.items || []; @@ -46,6 +47,7 @@ bindDeleteBtns(); }).catch(function() { loading.style.display = 'none'; + showToast('加载失败', 'error'); tbody.innerHTML = '加载失败'; }); } @@ -55,21 +57,19 @@ for (var i = 0; i < btns.length; i++) { if (btns[i]._bound) continue; btns[i]._bound = true; - btns[i].addEventListener('click', async function() { + btns[i].addEventListener('click', function() { var id = this.dataset.id; - var ok = await showConfirm('确认', '确认删除此评论?'); - if (!ok) return; - fetch('/api/admin/comments/' + id, { - method: 'DELETE', - headers: { 'X-CSRF-Token': csrfToken } - }).then(function(r) { return r.json(); }) - .then(function(d) { - if (d.success) { - loadComments(currentPage); - } else { - alert(d.message || '删除失败'); - } - }).catch(function() { alert('请求失败'); }); + showConfirm('确认', '确认删除此评论?').then(function(ok) { + if (!ok) return; + api.del('/api/admin/comments/' + id).then(function(d) { + if (d.success) { + showToast(d.message, 'success'); + loadComments(currentPage); + } else { + showToast(d.message || '删除失败', 'error'); + } + }).catch(function() { showToast('请求失败', 'error'); }); + }); }); } } diff --git a/templates/admin/static/js/common.js b/templates/admin/static/js/common.js index 407e749..fd98783 100644 --- a/templates/admin/static/js/common.js +++ b/templates/admin/static/js/common.js @@ -6,7 +6,8 @@ // --- AJAX 封装 (管理后台专用对象式 API) --- // 注意:escapeHtml / formatTime / showToast / getCSRFToken 由 shared/utils.js 统一提供 -const api = { +// 使用变量赋值覆盖 utils.js 的 function api(),避免 const 重复声明冲突 +var api = { async get(url) { const res = await fetch(url, { headers: { 'Accept': 'application/json' } @@ -34,6 +35,13 @@ const api = { body: body ? JSON.stringify(body) : undefined }); return res.json(); + }, + async del(url) { + const res = await fetch(url, { + method: 'DELETE', + headers: { 'X-CSRF-Token': getCSRFToken() } + }); + return res.json(); } }; diff --git a/templates/admin/static/js/users.js b/templates/admin/static/js/users.js index 0a9c2f0..2c96c5a 100644 --- a/templates/admin/static/js/users.js +++ b/templates/admin/static/js/users.js @@ -44,7 +44,8 @@ async function loadUsers() { showToast(res.message || '加载失败', 'error'); } } catch (e) { - showToast('网络错误', 'error'); + console.error('loadUsers error:', e); + showToast('加载失败: ' + (e.message || '网络错误'), 'error'); } }