/**
* 班级操行分管理系统 - 管理员管理页JS
*
* 开发者: Canglan
* 版权归属: Sea Network Technology Studio
*
* 版权所有 © Sea Network Technology Studio
*/
(function() {
'use strict';
let currentEditUserId = null;
let currentResetUserId = null;
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 ? '启用' : '禁用';
html += `
| ${escapeHtml(admin.username)} |
${escapeHtml(admin.real_name)} |
${escapeHtml(admin.role_type)} |
${statusText} |
|
`;
});
if (res.data.admins.length === 0) {
html = '| 暂无管理员 |
';
}
document.getElementById('adminList').innerHTML = html;
}
}
function showEditAdminModal(userId, username, realName, roleType) {
currentEditUserId = userId;
document.getElementById('editAdminUserId').value = userId;
document.getElementById('editAdminUsername').value = username;
document.getElementById('editAdminRealName').value = realName;
document.getElementById('editAdminRole').value = roleType;
document.getElementById('editAdminModal').style.display = 'flex';
}
async function submitEditAdmin() {
if (!currentEditUserId) return;
const roleType = document.getElementById('editAdminRole').value;
if (!roleType) {
showToast('请选择角色', 'warning');
return;
}
const res = await apiPut(`/api/admin/update/${currentEditUserId}`, {
real_name: document.getElementById('editAdminRealName').value,
role_type: roleType
});
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');
}
}
loadAdmins();
window.loadAdmins = loadAdmins;
window.showEditAdminModal = showEditAdminModal;
window.submitEditAdmin = submitEditAdmin;
window.toggleAdminStatus = toggleAdminStatus;
window.resetAdminPassword = resetAdminPassword;
window.unlockUser = unlockUser;
window.submitResetPassword = submitResetPassword;
})();