fix: 修复管理后台 JS api 变量冲突导致页面加载失败

- common.js 中 const api 改为 var api,避免与 utils.js 的 function api() 重复声明冲突,导致 roleLabel/statusLabel 等未定义
- common.js 新增 api.del() 方法
- comments.js 升级为对象式 api.get/api.del,修复 api('GET') 函数调用失败
- comments.js 修复 csrfToken 未定义导致删除请求 403
- users.js/audit.js catch 块改为输出真实错误信息
This commit is contained in:
2026-06-02 21:13:58 +08:00
parent ee313675ca
commit 983f539268
4 changed files with 40 additions and 22 deletions

View File

@ -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 = '<tr><td colspan="6" style="text-align:center;padding:40px;color:#dc2626">加载失败</td></tr>';
});
}
@ -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'); });
});
});
}
}