Files
mce/templates/admin/static/js/common.js
Victor_Jay 983f539268 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 块改为输出真实错误信息
2026-06-02 21:13:58 +08:00

61 lines
2.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/* =====================================================
MetaLab 管理面板 - 通用 JS
单一职责AJAX 封装、Toast 通知、确认弹窗
不包含任何页面特有逻辑
===================================================== */
// --- AJAX 封装 (管理后台专用对象式 API) ---
// 注意escapeHtml / formatTime / showToast / getCSRFToken 由 shared/utils.js 统一提供
// 使用变量赋值覆盖 utils.js 的 function api(),避免 const 重复声明冲突
var api = {
async get(url) {
const res = await fetch(url, {
headers: { 'Accept': 'application/json' }
});
return res.json();
},
async put(url, body) {
const res = await fetch(url, {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
'X-CSRF-Token': getCSRFToken()
},
body: JSON.stringify(body)
});
return res.json();
},
async post(url, body) {
const res = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-CSRF-Token': getCSRFToken()
},
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();
}
};
// --- 角色标签 ---
// 数据源由服务端 model.RoleDisplayNames 注入为 window.__ROLE_NAMES
function roleLabel(role) {
var map = window.__ROLE_NAMES || { user: '普通用户', moderator: '版主', admin: '管理员', owner: '站长' };
return '<span class="role-tag role-' + role + '">' + (map[role] || role) + '</span>';
}
// --- 状态标签 ---
// 数据源由服务端 model.StatusDisplayNames 注入为 window.__STATUS_NAMES
function statusLabel(status) {
var map = window.__STATUS_NAMES || { active: '正常', banned: '已封禁', deleted: '已注销', locked: '已锁定' };
return '<span class="badge badge-' + status + '">' + (map[status] || status) + '</span>';
}