/**
* 班级操行分管理系统 - 管理员管理页JS
*
* 开发者: Canglan
* 版权归属: Sea Network Technology Studio
*
* 版权所有 © Sea Network Technology Studio
*/
(function() {
'use strict';
const PERMISSION_LABELS = {
manage_students: '学生管理',
export_students: '导出学生名单',
manage_conduct: '操行分加减',
revoke_any: '撤销任何记录',
revoke_own: '撤销自己的记录',
restore_records: '反撤销记录',
manage_homework: '作业扣分',
manage_attendance: '考勤管理',
manage_subjects: '科目管理',
manage_admins: '管理员/权限组管理',
manage_semesters: '学期管理',
view_all_records: '查看全班/个人查询',
export_data: '导出德育分/历史',
manage_upgrade: '系统升级'
};
let currentEditUserId = null;
let currentResetUserId = null;
let groupsCache = [];
function groupCapsText(group) {
const add = group.can_add_points
? (group.max_add_points ? ('+' + group.max_add_points + '以内') : '不限')
: '禁止';
const subtract = group.can_subtract_points
? (group.max_subtract_points ? ('-' + group.max_subtract_points + '以内') : '不限')
: '禁止';
return { add: add, subtract: subtract };
}
function permissionSummary(group) {
const permissions = group.permissions_data || {};
const names = Object.keys(PERMISSION_LABELS)
.filter(key => permissions[key])
.map(key => PERMISSION_LABELS[key]);
return names.length ? names.join('、') : '无';
}
async function loadGroups() {
const res = await apiGet('/api/admin/permission-groups');
if (!res || !res.success) {
showToast((res && res.message) || '获取权限组失败', 'error');
return;
}
groupsCache = res.data.groups || [];
const options = groupsCache
.filter(group => !group.is_locked)
.map(group => ``)
.join('');
const placeholder = '';
document.getElementById('adminGroupId').innerHTML = placeholder + options;
document.getElementById('editAdminGroupId').innerHTML = placeholder + options;
renderGroupTable();
}
function renderGroupTable() {
let html = '';
groupsCache.forEach(group => {
const caps = groupCapsText(group);
const badges = [];
if (group.is_locked) {
badges.push('锁定');
} else if (group.is_system) {
badges.push('预置');
}
let actions = '-';
if (!group.is_locked) {
const deleteItem = group.is_system
? ''
: `删除`;
actions = `
`;
}
html += `
| ${escapeHtml(group.group_name)} ${badges.join(' ')} |
${escapeHtml(permissionSummary(group))} |
${caps.add} |
${caps.subtract} |
${group.member_count || 0} |
${escapeHtml(group.description || '-')} |
${actions} |
`;
});
document.getElementById('groupList').innerHTML = html || '| 暂无权限组 |
';
}
async function loadAdmins() {
const res = await apiGet('/api/admin/list');
if (res && res.success) {
let html = '';
res.data.admins.forEach(admin => {
const isActive = admin.status === 1;
const statusClass = isActive ? 'subject-status-active' : 'subject-status-inactive';
const statusText = isActive ? '启用' : '禁用';
const caps = {
add: admin.can_add_points ? (admin.max_add_points ? ('+' + admin.max_add_points + '以内') : '不限') : '禁止',
subtract: admin.can_subtract_points ? (admin.max_subtract_points ? ('-' + admin.max_subtract_points + '以内') : '不限') : '禁止'
};
const canEdit = !admin.is_locked && admin.group_id;
const editItem = canEdit
? `编辑`
: '';
html += `
| ${escapeHtml(admin.username)} |
${escapeHtml(admin.real_name)} |
${admin.email ? escapeHtml(admin.email) : '未绑定'} |
${escapeHtml(admin.group_name || admin.role_type || '-')}${admin.is_locked ? ' 锁定' : ''} |
加${caps.add} / 扣${caps.subtract} |
${statusText} |
|
`;
});
if (res.data.admins.length === 0) {
html = '| 暂无管理员 |
';
}
document.getElementById('adminList').innerHTML = html;
}
}
function showEditAdminModal(userId, username, realName, groupId, email) {
currentEditUserId = userId;
document.getElementById('editAdminUserId').value = userId;
document.getElementById('editAdminUsername').value = username;
document.getElementById('editAdminRealName').value = realName;
document.getElementById('editAdminGroupId').value = groupId || '';
document.getElementById('editAdminEmail').value = email || '';
document.getElementById('editAdminModal').style.display = 'flex';
}
async function submitEditAdmin() {
if (!currentEditUserId) return;
const groupId = parseInt(document.getElementById('editAdminGroupId').value);
if (!groupId) {
showToast('请选择权限组', 'warning');
return;
}
const res = await apiPut(`/api/admin/update/${currentEditUserId}`, {
real_name: document.getElementById('editAdminRealName').value.trim(),
group_id: groupId,
email: document.getElementById('editAdminEmail').value.trim()
});
if (res && res.success) {
showToast('管理员更新成功');
closeModal('editAdminModal');
loadAdmins();
} else {
showToast(res?.message || '更新失败', 'error');
}
}
async function toggleAdminStatus(userId, realName, currentStatus) {
const action = currentStatus === 1 ? '禁用' : '启用';
const warnMsg = currentStatus === 1
? `禁用后该管理员将无法登录,确定要禁用 "${realName}" 吗?`
: `确定要重新启用管理员 "${realName}" 吗?`;
if (!confirm(warnMsg)) {
return;
}
const res = await apiPut(`/api/admin/toggle-status/${userId}`);
if (res && res.success) {
showToast(res.message || `管理员已${action}`);
loadAdmins();
} else {
showToast(res?.message || '操作失败', 'error');
}
}
function resetAdminPassword(userId, realName) {
currentResetUserId = userId;
document.getElementById('resetPasswordUserId').value = userId;
document.getElementById('resetPasswordAdminName').value = realName;
document.getElementById('newPassword').value = '';
document.getElementById('resetPasswordModal').style.display = 'flex';
}
async function unlockUser(username, realName) {
if (!confirm(`确定要解除用户 "${realName}" 的登录锁定吗?\n(适用于多次登录失败被禁止登录的情况)`)) {
return;
}
const res = await apiPost('/api/admin/unlock-user', {
username: username
});
if (res && res.success) {
showToast(res.message || '解锁成功');
} else {
showToast(res?.message || '解锁失败', 'error');
}
}
async function submitResetPassword() {
if (!currentResetUserId) return;
const newPassword = document.getElementById('newPassword').value;
if (!newPassword || newPassword.length < 6) {
showToast('密码长度至少6位', 'warning');
return;
}
const res = await apiPost(`/api/admin/reset-password/${currentResetUserId}`, {
new_password: newPassword
});
if (res && res.success) {
showToast('密码重置成功');
closeModal('resetPasswordModal');
} else {
showToast(res?.message || '密码重置失败', 'error');
}
}
// ========== 权限组管理 ==========
function renderPermissionCheckboxes(permissions) {
const container = document.getElementById('groupPermissionList');
container.innerHTML = Object.keys(PERMISSION_LABELS).map(key =>
``
).join('');
}
function onGroupCapChange() {
const canAdd = document.getElementById('groupCanAdd').checked;
const canSubtract = document.getElementById('groupCanSubtract').checked;
document.getElementById('groupMaxAdd').disabled = !canAdd;
document.getElementById('groupMaxSubtract').disabled = !canSubtract;
if (!canAdd) document.getElementById('groupMaxAdd').value = '';
if (!canSubtract) document.getElementById('groupMaxSubtract').value = '';
}
function showAddGroupModal() {
document.getElementById('groupModalTitle').textContent = '新建权限组';
document.getElementById('editGroupId').value = '';
document.getElementById('groupName').value = '';
document.getElementById('groupDescription').value = '';
document.getElementById('groupCanAdd').checked = true;
document.getElementById('groupCanSubtract').checked = true;
document.getElementById('groupMaxAdd').value = '';
document.getElementById('groupMaxSubtract').value = '';
renderPermissionCheckboxes({});
onGroupCapChange();
document.getElementById('groupModal').style.display = 'flex';
}
function showEditGroupModal(groupId) {
const group = groupsCache.find(item => item.group_id === groupId);
if (!group) return;
if (group.is_locked) {
showToast('该权限组已锁定,不可编辑', 'warning');
return;
}
document.getElementById('groupModalTitle').textContent = '编辑权限组 - ' + group.group_name;
document.getElementById('editGroupId').value = group.group_id;
document.getElementById('groupName').value = group.group_name;
document.getElementById('groupDescription').value = group.description || '';
document.getElementById('groupCanAdd').checked = !!group.can_add_points;
document.getElementById('groupCanSubtract').checked = !!group.can_subtract_points;
document.getElementById('groupMaxAdd').value = group.max_add_points || '';
document.getElementById('groupMaxSubtract').value = group.max_subtract_points || '';
renderPermissionCheckboxes(group.permissions_data || {});
onGroupCapChange();
document.getElementById('groupModal').style.display = 'flex';
}
async function submitGroupForm() {
const groupId = document.getElementById('editGroupId').value;
const canAdd = document.getElementById('groupCanAdd').checked;
const canSubtract = document.getElementById('groupCanSubtract').checked;
const maxAddRaw = document.getElementById('groupMaxAdd').value;
const maxSubtractRaw = document.getElementById('groupMaxSubtract').value;
const maxAdd = canAdd && maxAddRaw !== '' ? parseInt(maxAddRaw) : null;
const maxSubtract = canSubtract && maxSubtractRaw !== '' ? parseInt(maxSubtractRaw) : null;
if (maxAdd !== null && (isNaN(maxAdd) || maxAdd < 1 || maxAdd > 100)) {
showToast('加分上限必须在1-100之间', 'warning');
return;
}
if (maxSubtract !== null && (isNaN(maxSubtract) || maxSubtract < 1 || maxSubtract > 100)) {
showToast('扣分上限必须在1-100之间', 'warning');
return;
}
const payload = {
group_name: document.getElementById('groupName').value.trim(),
description: document.getElementById('groupDescription').value.trim() || null,
permissions: Array.from(document.querySelectorAll('.group-perm:checked')).map(el => el.value),
can_add_points: canAdd,
max_add_points: maxAdd,
can_subtract_points: canSubtract,
max_subtract_points: maxSubtract
};
if (!payload.group_name) {
showToast('请填写权限组名称', 'warning');
return;
}
const res = groupId
? await apiPut(`/api/admin/permission-groups/${groupId}`, payload)
: await apiPost('/api/admin/permission-groups', payload);
if (res && res.success) {
showToast(groupId ? '权限组更新成功' : '权限组创建成功');
closeModal('groupModal');
loadGroups();
} else {
showToast(res?.message || '保存失败', 'error');
}
}
async function deleteGroup(groupId, groupName) {
if (!confirm(`确定要删除权限组 "${groupName}" 吗?删除后不可恢复。`)) {
return;
}
const res = await apiDelete(`/api/admin/permission-groups/${groupId}`);
if (res && res.success) {
showToast('权限组已删除');
loadGroups();
} else {
showToast(res?.message || '删除失败', 'error');
}
}
loadGroups();
loadAdmins();
window.loadAdmins = loadAdmins;
window.loadGroups = loadGroups;
window.showEditAdminModal = showEditAdminModal;
window.submitEditAdmin = submitEditAdmin;
window.toggleAdminStatus = toggleAdminStatus;
window.resetAdminPassword = resetAdminPassword;
window.unlockUser = unlockUser;
window.submitResetPassword = submitResetPassword;
window.showAddGroupModal = showAddGroupModal;
window.showEditGroupModal = showEditGroupModal;
window.submitGroupForm = submitGroupForm;
window.deleteGroup = deleteGroup;
window.onGroupCapChange = onGroupCapChange;
})();