Files
ClassManager/frontend/assets/js/history.js
T
canglan 768ea281e0 v4.4 数据报表/操作日志/系统设置与月度分排行
1. 月度分排行:管理端首页支持总分/月度分切换与月份选择,新增排行榜导出CSV
2. 数据报表:新增考勤/作业统计页(日期范围、汇总与每人明细、CSV导出)
3. 操作日志:新增查看页(操作类型/操作人/日期筛选,分页)
4. 系统设置:密码策略可配置(长度/字符种类),全站改密与重置统一校验、前端提示同步;策略通过公开配置下发
5. 学期归档对比:查看归档时展示与上一学期的分数变化与名次升降
6. 历史记录导出支持按学生分表(每人一段,便于发家长)
7. 移动端适配:导航横向滚动、表格触控滚动、字号与弹窗优化
8. 版本升级至4.4(无数据库结构变更)
2026-10-09 15:00:32 +08:00

404 lines
17 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.
/**
* 班级操行分管理系统 - 历史记录页JS
*
* 开发者: Canglan
* 版权归属: Sea Network Technology Studio
*
* 版权所有 © Sea Network Technology Studio
*/
(function() {
'use strict';
const currentUserId = window.PAGE_CONFIG.userId;
const canRevokeAny = hasPermission('revoke_any');
const canRevokeOwn = hasPermission('revoke_own');
const canRestore = hasPermission('restore_records');
const canRevoke = canRevokeAny || canRevokeOwn;
let currentHistoryPage = 1;
let totalHistoryPages = 1;
function typeMap(relatedType) {
var map = { manual: '手动', homework: '作业', attendance: '考勤' };
return map[relatedType] || '手动';
}
async function loadStudentsForSelect() {
const res = await apiGet('/api/admin/students', {page_size: 1000});
if (res && res.success) {
let html = '<option value="">全部</option>';
res.data.students.forEach(s => {
html += '<option value="' + s.student_id + '">' + escapeHtml(s.student_no) + ' - ' + escapeHtml(s.name) + '</option>';
});
document.getElementById('historyStudentId').innerHTML = html;
}
}
// 加载科目下拉列表
async function loadSubjectsForFilter() {
var subjectSelect = document.getElementById('historySubjectFilter');
if (!subjectSelect) return;
var res = await apiGet('/api/subject/list', { is_active: true });
if (res && res.success && res.data && res.data.subjects) {
let html = '<option value="">全部科目</option>';
res.data.subjects.forEach(s => {
html += '<option value="' + escapeHtml(s.subject_name) + '">' + escapeHtml(s.subject_name) + '</option>';
});
subjectSelect.innerHTML = html;
}
}
// 加载学期下拉列表(默认选中当前学期)
async function loadSemestersForFilter() {
await loadSemesterOptions('historySemesterFilter', { includeAll: true });
}
// 筛选学生时自动取消合并记录
function onStudentFilterChange() {
var studentId = document.getElementById('historyStudentId').value;
if (studentId) {
var grouped = document.getElementById('historyGrouped');
if (grouped) grouped.checked = false;
}
}
// 科目筛选变化时,取消扣分类型筛选(互斥)
function onSubjectFilterChange() {
var subjectVal = document.getElementById('historySubjectFilter').value;
if (subjectVal) {
document.getElementById('historyReasonFilter').value = '';
}
}
// 折叠/展开筛选面板
function toggleFilterPanel() {
var panel = document.getElementById('advancedFilters');
var btn = document.getElementById('filterToggleBtn');
if (!panel || !btn) return;
if (panel.style.display === 'none') {
panel.style.display = 'block';
btn.textContent = '收起筛选 ▲';
} else {
panel.style.display = 'none';
btn.textContent = '展开筛选 ▼';
}
}
async function loadHistory(page) {
page = page || 1;
currentHistoryPage = page;
var startDate = document.getElementById('historyStartDate').value;
var endDate = document.getElementById('historyEndDate').value;
var studentId = document.getElementById('historyStudentId').value;
var semesterId = document.getElementById('historySemesterFilter') ? document.getElementById('historySemesterFilter').value : '';
var reasonFilter = document.getElementById('historyReasonFilter').value;
var subjectFilter = document.getElementById('historySubjectFilter').value;
var reasonSearch = document.getElementById('historyReasonSearch').value.trim();
var isGrouped = document.getElementById('historyGrouped').checked;
var statusFilter = document.getElementById('historyStatusFilter') ? document.getElementById('historyStatusFilter').value : '';
// 筛选学生时强制取消合并
if (studentId) isGrouped = false;
var params = {
page: page, page_size: 20,
start_date: startDate,
end_date: endDate
};
if (semesterId) params.semester_id = semesterId;
if (studentId) params.student_id = studentId;
// 科目筛选优先于扣分类型筛选
if (subjectFilter) {
params.reason_prefix = '[' + subjectFilter + ']';
} else if (reasonFilter) {
params.reason_prefix = reasonFilter;
}
if (reasonSearch) params.reason_search = reasonSearch;
if (isGrouped) params.grouped = true;
if (statusFilter !== '') params.is_revoked = parseInt(statusFilter);
var res = await apiGet('/api/admin/conduct/history', params);
if (res && res.success) {
var nowrapStyle = ' style="white-space:nowrap;min-width:80px;"';
var headHtml = '';
if (isGrouped) {
headHtml = '<th>类型</th><th>分值</th><th>原因</th><th>学生名单</th><th' + nowrapStyle + '>操作人</th><th>时间</th>';
if (canRevoke) {
headHtml += '<th>操作</th>';
}
} else {
headHtml = '<th>类型</th><th>分值</th><th>原因</th><th>学生</th><th' + nowrapStyle + '>操作人</th><th>时间</th>';
if (canRevoke) {
headHtml += '<th>操作</th>';
}
}
document.getElementById('historyTableHead').innerHTML = headHtml;
var html = '';
if (isGrouped) {
res.data.records.forEach(function(record) {
var pointsClass = record.points_change > 0 ? 'plus' : 'minus';
var typeLabel = typeMap(record.related_type);
var names = record.student_names || '';
var allRevoked = record.all_revoked;
var revokedStyle = allRevoked ? ' style="opacity:0.5;text-decoration:line-through;"' : '';
html += '<tr' + revokedStyle + '>' +
'<td>' + typeLabel + '</td>' +
'<td class="' + pointsClass + '">' + (record.points_change > 0 ? '+' : '') + record.points_change + '&times;' + record.student_count + '</td>' +
'<td class="history-reason">' + escapeHtml(record.reason) + '</td>' +
'<td class="history-students">' + escapeHtml(names) + '</td>' +
'<td>' + escapeHtml(record.recorder_name || '') + '</td>' +
'<td class="history-time">' + formatDateTime(record.created_at) + '</td>';
if (canRevoke) {
if (allRevoked) {
html += '<td><span class="text-muted">已撤销</span></td>';
} else if (canRevokeAny) {
html += '<td><button class="btn btn-sm btn-outline" onclick="batchRevokeGrouped(\'' + escapeHtml(record.reason).replace(/'/g, "\\'") + '\',' + record.points_change + ',\'' + escapeHtml(record.recorder_name || '').replace(/'/g, "\\'") + '\',\'' + formatDateTime(record.created_at) + '\')">批量撤销</button></td>';
} else {
html += '<td><span class="text-muted">-</span></td>';
}
}
html += '</tr>';
});
if (res.data.records.length === 0) {
var colSpan = canRevoke ? 7 : 6;
html = '<tr><td colspan="' + colSpan + '" style="text-align:center;">暂无记录</td></tr>';
}
} else {
res.data.records.forEach(function(record) {
var pointsClass = record.points_change > 0 ? 'plus' : 'minus';
var typeLabel = typeMap(record.related_type);
var revokedStyle = record.is_revoked == 1 ? ' style="opacity:0.5;text-decoration:line-through;"' : '';
html += '<tr' + revokedStyle + '>' +
'<td>' + typeLabel + '</td>' +
'<td class="' + pointsClass + '">' + (record.points_change > 0 ? '+' : '') + record.points_change + '</td>' +
'<td class="history-reason">' + escapeHtml(record.reason) + '</td>' +
'<td>' + escapeHtml(record.student_name) + '</td>' +
'<td>' + escapeHtml(record.recorder_name) + '</td>' +
'<td class="history-time">' + formatDateTime(record.created_at) + '</td>';
if (canRevoke) {
if (record.is_revoked == 1) {
var revokerInfo = record.revoker_name ? '由 ' + escapeHtml(record.revoker_name) + ' 撤销' : '已撤销';
if (canRestore) {
html += '<td><span class="text-muted" style="margin-right:4px;">' + revokerInfo + '</span><button class="btn btn-sm btn-outline" onclick="restoreRecord(' + record.record_id + ')">反撤销</button></td>';
} else {
html += '<td><span class="text-muted">' + revokerInfo + '</span></td>';
}
} else if (canRevokeAny || (canRevokeOwn && record.recorder_id == currentUserId)) {
html += '<td><button class="btn btn-sm btn-outline" onclick="revokeRecord(' + record.record_id + ')">撤销</button></td>';
} else {
html += '<td><span class="text-muted">-</span></td>';
}
}
html += '</tr>';
});
if (res.data.records.length === 0) {
var colSpan = canRevoke ? 7 : 6;
html = '<tr><td colspan="' + colSpan + '" style="text-align:center;">暂无记录</td></tr>';
}
}
document.getElementById('historyList').innerHTML = html;
totalHistoryPages = res.data.total_pages || 1;
renderHistoryPagination();
}
}
function renderHistoryPagination() {
renderSmartPagination('historyPagination', currentHistoryPage, totalHistoryPages, function(page) {
loadHistory(page);
});
}
async function exportHistoryRecords() {
var startDate = document.getElementById('historyStartDate').value;
var endDate = document.getElementById('historyEndDate').value;
var studentId = document.getElementById('historyStudentId').value;
var semesterSelect = document.getElementById('historySemesterFilter');
var semesterId = semesterSelect ? semesterSelect.value : '';
showToast('正在导出历史记录...', 'info');
try {
var reasonFilter = document.getElementById('historyReasonFilter').value;
var subjectFilter = document.getElementById('historySubjectFilter').value;
var reasonSearch = document.getElementById('historyReasonSearch').value.trim();
var params = { is_revoked: 0 };
if (startDate) params.start_date = startDate;
if (endDate) params.end_date = endDate;
if (studentId) params.student_id = studentId;
if (semesterId) params.semester_id = semesterId;
if (subjectFilter) {
params.reason_prefix = '[' + subjectFilter + ']';
} else if (reasonFilter) {
params.reason_prefix = reasonFilter;
}
if (reasonSearch) params.reason_search = reasonSearch;
var records = await apiGetAll('/api/admin/conduct/history', params);
if (!records || records.length === 0) {
showToast('没有找到记录', 'warning');
return;
}
var mode = getHistoryExportMode();
var csv;
if (mode === 'per_student') {
csv = buildPerStudentCsv(records);
} else {
csv = '时间,学号,姓名,学期,类型,分数变动,原因,操作人\n';
records.forEach(function(r) {
csv += [
r.created_at || '',
r.student_no || '',
r.student_name || '',
r.semester_name || '',
typeMap(r.related_type),
(r.points_change > 0 ? '+' : '') + r.points_change,
r.reason || '',
r.recorder_name || ''
].map(csvEscape).join(',') + '\n';
});
}
var semesterLabel = '全部学期';
if (semesterSelect && semesterId && semesterSelect.selectedIndex >= 0) {
semesterLabel = semesterSelect.options[semesterSelect.selectedIndex].textContent.replace(/((当前|已归档))$/, '');
}
var safeLabel = semesterLabel.replace(/[\\/:*?"<>|]/g, '_');
var modeLabel = mode === 'per_student' ? '_按学生分表' : '';
downloadCsv('历史记录_' + safeLabel + modeLabel + '_' + new Date().toISOString().slice(0,10) + '.csv', csv);
closeModal('exportHistoryModal');
showToast('导出成功,共' + records.length + '条记录');
} catch (err) {
showToast('导出失败:' + err.message, 'error');
}
}
function showExportHistoryModal() {
document.getElementById('exportHistoryModal').style.display = 'flex';
}
function getHistoryExportMode() {
var el = document.querySelector('input[name="historyExportMode"]:checked');
return el ? el.value : 'detail';
}
// 按学生分表:每个学生一段(汇总 + 明细),便于发给家长
function buildPerStudentCsv(records) {
var groups = {};
var order = [];
records.forEach(function(r) {
var key = r.student_id;
if (!groups[key]) {
groups[key] = { no: r.student_no || '', name: r.student_name || '', records: [] };
order.push(key);
}
groups[key].records.push(r);
});
order.sort(function(a, b) {
return String(groups[a].no).localeCompare(String(groups[b].no));
});
var csv = '';
order.forEach(function(key) {
var group = groups[key];
var bonus = 0;
var deduction = 0;
group.records.forEach(function(r) {
if (r.points_change > 0) bonus += r.points_change;
else deduction += r.points_change;
});
csv += ['学生', group.no + ' ' + group.name, '加分合计', '+' + bonus, '减分合计', deduction]
.map(csvEscape).join(',') + '\n';
csv += '时间,学期,类型,分值,原因,操作人\n';
group.records.forEach(function(r) {
csv += [
r.created_at || '',
r.semester_name || '',
typeMap(r.related_type),
(r.points_change > 0 ? '+' : '') + r.points_change,
r.reason || '',
r.recorder_name || ''
].map(csvEscape).join(',') + '\n';
});
csv += '\n';
});
return csv;
}
// 批量撤销合并记录(按条件查找并撤销)
async function batchRevokeGrouped(reason, pointsChange, recorderName, createdAt) {
if (!confirm('确定要撤销所有"' + reason + '"(' + (pointsChange > 0 ? '+' : '') + pointsChange + '分)的记录吗?')) return;
showToast('正在批量撤销...', 'info');
try {
var params = {
page: 1, page_size: 1000,
start_date: document.getElementById('historyStartDate').value,
end_date: document.getElementById('historyEndDate').value,
reason_prefix: reason.substring(0, 4),
grouped: false
};
var semesterSelect = document.getElementById('historySemesterFilter');
if (semesterSelect && semesterSelect.value) params.semester_id = semesterSelect.value;
var res = await apiGet('/api/admin/conduct/history', params);
if (!res || !res.success || !res.data.records) {
showToast('查询记录失败', 'error');
return;
}
var matchedIds = [];
res.data.records.forEach(function(r) {
if (r.reason === reason && r.points_change === pointsChange && r.is_revoked == 0) {
matchedIds.push(r.record_id);
}
});
if (matchedIds.length === 0) {
showToast('没有找到可撤销的记录', 'warning');
return;
}
var revokeRes = await apiPost('/api/admin/conduct/batch-revoke', { record_ids: matchedIds });
if (revokeRes && revokeRes.success) {
showToast('批量撤销完成: ' + (revokeRes.data ? revokeRes.data.success_count : 0) + '条成功');
loadHistory(currentHistoryPage);
} else {
showToast(revokeRes && revokeRes.message || '批量撤销失败', 'error');
}
} catch (err) {
showToast('批量撤销失败: ' + err.message, 'error');
}
}
// 初始化:并行加载学生、科目、学期列表,然后加载历史记录
Promise.all([loadStudentsForSelect(), loadSubjectsForFilter(), loadSemestersForFilter()]).then(function() {
var urlParams = new URLSearchParams(window.location.search);
var preStudentId = urlParams.get('student_id');
if (preStudentId) {
document.getElementById('historyStudentId').value = preStudentId;
onStudentFilterChange();
}
loadHistory();
});
window.loadHistory = loadHistory;
window.loadStudentsForSelect = loadStudentsForSelect;
window.showExportHistoryModal = showExportHistoryModal;
window.exportHistoryRecords = exportHistoryRecords;
window.batchRevokeGrouped = batchRevokeGrouped;
window.onStudentFilterChange = onStudentFilterChange;
window.onSubjectFilterChange = onSubjectFilterChange;
window.toggleFilterPanel = toggleFilterPanel;
})();