/**
* 班级操行分管理系统 - 学生管理页JS
*
* 开发者: Canglan
* 版权归属: Sea Network Technology Studio
*
* 版权所有 © Sea Network Technology Studio
*/
(function() {
'use strict';
const canViewPersonalQuery = hasPermission('view_all_records');
const canManageStudents = hasPermission('manage_students');
const canManageConduct = hasPermission('manage_conduct');
const canExportStudents = hasPermission('export_students');
let currentPage = 1;
let totalPages = 1;
async function loadStudents(page = 1) {
currentPage = page;
const search = document.getElementById('searchInput').value;
const res = await apiGet('/api/admin/students', { page, page_size: 20, search });
if (res && res.success) {
let html = '';
res.data.students.forEach(student => {
const detailUrl = canViewPersonalQuery
? `/admin/record_query.php?student_id=${student.student_id}`
: `/admin/history.php?student_id=${student.student_id}`;
const pointsButton = canManageConduct
? ``
: '';
const moreButton = canManageStudents ? `
` : '';
html += `
|
${escapeHtml(student.student_no)} |
${escapeHtml(student.name)} |
${escapeHtml(student.dormitory_number || '-')} |
${student.total_points} |
${canManageStudents ? `${student.parent_phone ? student.parent_phone.slice(0,3) + '******' + student.parent_phone.slice(-2) : '-'} | ` : ''}
${pointsButton}
${moreButton}
|
`;
});
if (res.data.students.length === 0) {
const colCount = canManageStudents ? 7 : 6;
html = `| 暂无学生数据 |
`;
}
document.getElementById('studentList').innerHTML = html;
totalPages = res.data.total_pages || 1;
renderPagination();
}
}
function renderPagination() {
renderSmartPagination('pagination', currentPage, totalPages, function(page) {
loadStudents(page);
});
}
async function unlockStudent(studentNo, studentName) {
if (!confirm(`确定要解除学生 "${studentName}" 的登录锁定吗?\n(适用于多次登录失败被禁止登录的情况)`)) {
return;
}
const res = await apiPost('/api/admin/unlock-user', {
username: studentNo
});
if (res && res.success) {
showToast(res.message || '解锁成功');
} else {
showToast(res?.message || '解锁失败', 'error');
}
}
// ========== 导出学生名单 ==========
const EXPORT_COLUMNS = {
student_no: '学号',
name: '姓名',
dormitory_number: '宿舍号',
total_points: '当前操行分',
parent_phone: '家长手机号',
status: '状态'
};
function showExportStudentsModal() {
if (!canExportStudents) {
showToast('无导出学生名单权限', 'error');
return;
}
document.getElementById('exportStudentsModal').style.display = 'flex';
}
async function fetchAllStudentsForExport() {
const allStudents = [];
let page = 1;
while (page <= 100) {
const res = await apiGet('/api/admin/students', { page: page, page_size: 1000 });
if (!res || !res.success || !Array.isArray(res.data.students)) {
break;
}
allStudents.push(...res.data.students);
const total = res.data.total || 0;
if (allStudents.length >= total || res.data.students.length < 1000) {
break;
}
page++;
}
return allStudents;
}
async function exportStudents() {
if (!canExportStudents) {
showToast('无导出学生名单权限', 'error');
return;
}
const checked = Array.from(document.querySelectorAll('.export-student-col:checked')).map(el => el.value);
if (checked.length === 0) {
showToast('请至少选择一个导出字段', 'warning');
return;
}
const selected = ['student_no', 'name'].filter(v => checked.indexOf(v) === -1).concat(checked);
showToast('正在导出学生名单...', 'info');
try {
const students = await fetchAllStudentsForExport();
if (!students || students.length === 0) {
showToast('没有找到学生', 'warning');
return;
}
let csv = selected.map(key => EXPORT_COLUMNS[key]).join(',') + '\n';
students.forEach(student => {
const row = selected.map(key => {
if (key === 'status') {
return student.status === 1 ? '正常' : '停用';
}
return student[key] !== null && student[key] !== undefined ? student[key] : '';
});
csv += row.map(csvEscape).join(',') + '\n';
});
downloadCsv('学生名单_' + new Date().toISOString().slice(0,10) + '.csv', csv);
closeModal('exportStudentsModal');
showToast('导出成功,共' + students.length + '名学生');
} catch (err) {
showToast('导出失败:' + err.message, 'error');
}
}
loadStudents();
let searchTimeout;
document.getElementById('searchInput').addEventListener('input', () => {
clearTimeout(searchTimeout);
searchTimeout = setTimeout(() => loadStudents(1), 500);
});
window.loadStudents = loadStudents;
window.unlockStudent = unlockStudent;
window.showExportStudentsModal = showExportStudentsModal;
window.exportStudents = exportStudents;
})();