1. 考勤默认出勤:按日期范围给缺失学生批量补"出勤"(跳过周末/法定节假日,调休补班与手动补课日照常,已有记录不覆盖) 2. 节假日/补课校历:法定节假日与调休补班来自在线接口(缓存+失败降级),支持手动添加放假/补课(存 system_settings,优先于接口) 3. 作业按次登记:一次登记覆盖多次作业,配置总提交次数,按每人缺交次数自动扣分(单次缺交扣分x次数),并写入作业提交数据供"数据报表-作业统计"使用 4. 权限体系:新增 homework_register/default_attendance/manage_calendar 权限项;数据报表/操作日志/系统设置拆分为 view_reports/view_logs/manage_settings,共20项功能权限可配置;预置组默认值与 init.sql 同步 5. 权限审计修复:学生端接口管理员越权修复(管理员查看需 view_all_records,学生仅本人);学生列表手机号仅向学生管理/导出名单权限下发;操行分管理页与导航补权限门控 6. v4.5.sql:为预置权限组补充新权限默认值(JSON_SET,幂等可重复执行)
408 lines
17 KiB
JavaScript
408 lines
17 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: '作业扣分',
|
|
homework_register: '作业按次登记',
|
|
manage_attendance: '考勤管理',
|
|
default_attendance: '考勤默认出勤',
|
|
manage_calendar: '节假日/补课设置',
|
|
manage_subjects: '科目管理',
|
|
manage_admins: '管理员/权限组管理',
|
|
manage_semesters: '学期管理',
|
|
view_all_records: '查看全班/个人查询',
|
|
view_reports: '数据报表查看',
|
|
export_data: '导出德育分/历史',
|
|
view_logs: '操作日志查看',
|
|
manage_settings: '系统设置',
|
|
manage_upgrade: '系统升级'
|
|
};
|
|
|
|
const PERMISSION_SECTIONS = [
|
|
{ title: '学生', keys: ['manage_students', 'export_students'] },
|
|
{ title: '操行分', keys: ['manage_conduct', 'revoke_any', 'revoke_own', 'restore_records', 'view_all_records', 'export_data'] },
|
|
{ title: '教务', keys: ['manage_homework', 'homework_register', 'manage_attendance', 'default_attendance', 'manage_calendar', 'manage_subjects'] },
|
|
{ title: '报表与审计', keys: ['view_reports', 'view_logs'] },
|
|
{ title: '系统管理', keys: ['manage_admins', 'manage_semesters', 'manage_settings', 'manage_upgrade'] }
|
|
];
|
|
|
|
let currentEditUserId = null;
|
|
let currentResetUserId = null;
|
|
let groupsCache = [];
|
|
const currentUserId = (window.PAGE_CONFIG && window.PAGE_CONFIG.userId) ? parseInt(window.PAGE_CONFIG.userId) : null;
|
|
|
|
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.group_id)
|
|
.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);
|
|
let actions = '<span class="text-muted">-</span>';
|
|
if (!group.readonly && group.group_id) {
|
|
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>
|
|
<a class="danger" onclick="deleteGroup(${group.group_id}, '${escapeHtml(group.group_name)}')">删除</a>
|
|
</div>
|
|
</div>`;
|
|
}
|
|
html += `<tr>
|
|
<td>${escapeHtml(group.group_name)}</td>
|
|
<td class="cell-wrap">${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.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 || '-')}</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;
|
|
const groupSelect = document.getElementById('editAdminGroupId');
|
|
groupSelect.value = groupId || '';
|
|
const isSelf = currentUserId !== null && parseInt(userId) === currentUserId;
|
|
groupSelect.disabled = isSelf;
|
|
const hint = document.getElementById('editAdminGroupHint');
|
|
if (hint) hint.style.display = isSelf ? 'block' : 'none';
|
|
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;
|
|
const policyError = validatePasswordByPolicy(newPassword);
|
|
if (policyError) {
|
|
showToast(policyError, '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 = PERMISSION_SECTIONS.map(section => {
|
|
const items = section.keys.map(key =>
|
|
`<label class="check-item">
|
|
<input type="checkbox" class="group-perm" value="${key}"${permissions[key] ? ' checked' : ''}> ${PERMISSION_LABELS[key]}
|
|
</label>`
|
|
).join('');
|
|
return `<div class="perm-section">
|
|
<div class="perm-section-title">${section.title}</div>
|
|
<div class="check-grid">${items}</div>
|
|
</div>`;
|
|
}).join('');
|
|
}
|
|
|
|
function selectAllPermissions(checked) {
|
|
document.querySelectorAll('.group-perm').forEach(el => { el.checked = !!checked; });
|
|
}
|
|
|
|
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;
|
|
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');
|
|
}
|
|
}
|
|
|
|
async function testSmtpEmail() {
|
|
const email = prompt('请输入接收测试邮件的邮箱地址:');
|
|
if (!email) return;
|
|
const res = await apiPost('/api/admin/email/test', { email: email.trim() });
|
|
if (res && res.success) {
|
|
showToast(res.message || '测试邮件已发送');
|
|
} else {
|
|
alert('邮件发送失败:\n' + ((res && res.message) || '未知错误'));
|
|
}
|
|
}
|
|
|
|
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;
|
|
window.selectAllPermissions = selectAllPermissions;
|
|
window.testSmtpEmail = testSmtpEmail;
|
|
|
|
})();
|