1. users 表新增 email 列,管理员添加/编辑支持绑定邮箱,修改密码页支持自助绑定 2. 新增 SMTP 邮件发送工具与配置(FRONTEND_URL/SMTP_* 等) 3. 新增忘记密码/重置密码公开接口与页面:一次性令牌、30分钟有效、请求限流、重置后强制重新登录 4. 登录与 /api/auth/me 返回邮箱并同步 PHP Session 5. 版本升级至4.1(sql/upgrades/v4.1.sql)
377 lines
15 KiB
JavaScript
377 lines
15 KiB
JavaScript
/**
|
|
* 班级操行分管理系统 - 管理员管理页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 => `<option value="${group.group_id}">${escapeHtml(group.group_name)}</option>`)
|
|
.join('');
|
|
const placeholder = '<option value="">请选择权限组</option>';
|
|
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('<span class="subject-status subject-status-active">锁定</span>');
|
|
} else if (group.is_system) {
|
|
badges.push('<span class="subject-status">预置</span>');
|
|
}
|
|
let actions = '<span class="text-muted">-</span>';
|
|
if (!group.is_locked) {
|
|
const deleteItem = group.is_system
|
|
? ''
|
|
: `<a class="danger" onclick="deleteGroup(${group.group_id}, '${escapeHtml(group.group_name)}')">删除</a>`;
|
|
actions = `<div class="action-dropdown">
|
|
<button class="btn btn-sm action-dropdown-toggle" onclick="toggleActionDropdown(this)">操作 ▼</button>
|
|
<div class="action-dropdown-menu">
|
|
<a onclick="showEditGroupModal(${group.group_id})">编辑</a>
|
|
${deleteItem}
|
|
</div>
|
|
</div>`;
|
|
}
|
|
html += `<tr>
|
|
<td>${escapeHtml(group.group_name)} ${badges.join(' ')}</td>
|
|
<td style="max-width: 340px; white-space: normal;">${escapeHtml(permissionSummary(group))}</td>
|
|
<td>${caps.add}</td>
|
|
<td>${caps.subtract}</td>
|
|
<td>${group.member_count || 0}</td>
|
|
<td>${escapeHtml(group.description || '-')}</td>
|
|
<td>${actions}</td>
|
|
</tr>`;
|
|
});
|
|
document.getElementById('groupList').innerHTML = html || '<tr><td colspan="7" style="text-align:center;">暂无权限组</td></tr>';
|
|
}
|
|
|
|
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
|
|
? `<a onclick="showEditAdminModal(${admin.user_id}, '${escapeHtml(admin.username)}', '${escapeHtml(admin.real_name)}', ${admin.group_id}, '${escapeHtml(admin.email || '')}')">编辑</a>`
|
|
: '';
|
|
html += `<tr>
|
|
<td>${escapeHtml(admin.username)}</td>
|
|
<td>${escapeHtml(admin.real_name)}</td>
|
|
<td>${admin.email ? escapeHtml(admin.email) : '<span class="text-muted">未绑定</span>'}</td>
|
|
<td>${escapeHtml(admin.group_name || admin.role_type || '-')}${admin.is_locked ? ' <span class="subject-status subject-status-active">锁定</span>' : ''}</td>
|
|
<td>加${caps.add} / 扣${caps.subtract}</td>
|
|
<td><span class="subject-status ${statusClass}">${statusText}</span></td>
|
|
<td>
|
|
<div class="action-dropdown">
|
|
<button class="btn btn-sm action-dropdown-toggle" onclick="toggleActionDropdown(this)">操作 ▼</button>
|
|
<div class="action-dropdown-menu">
|
|
${editItem}
|
|
<a onclick="resetAdminPassword(${admin.user_id}, '${escapeHtml(admin.real_name)}')">重置密码</a>
|
|
<a onclick="unlockUser('${escapeHtml(admin.username)}', '${escapeHtml(admin.real_name)}')">解锁</a>
|
|
<a class="${isActive ? 'danger' : ''}" onclick="toggleAdminStatus(${admin.user_id}, '${escapeHtml(admin.real_name)}', ${isActive ? 1 : 0})">${isActive ? '禁用' : '启用'}</a>
|
|
</div>
|
|
</div>
|
|
</td>
|
|
</tr>`;
|
|
});
|
|
if (res.data.admins.length === 0) {
|
|
html = '<tr><td colspan="7" style="text-align:center;">暂无管理员</td></tr>';
|
|
}
|
|
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 =>
|
|
`<label style="display:flex; align-items:center; gap:6px;">
|
|
<input type="checkbox" class="group-perm" value="${key}"${permissions[key] ? ' checked' : ''}> ${PERMISSION_LABELS[key]}
|
|
</label>`
|
|
).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;
|
|
|
|
})();
|