add manage student password

This commit is contained in:
2026-09-29 20:00:16 +08:00
parent 80bfd880ad
commit 0b2072a05f
6 changed files with 119 additions and 1 deletions

View File

@@ -1,6 +1,6 @@
const state = { classes: [], snapshot: null, activeClass: '', username: '' };
const el = id => document.getElementById(id);
const adminTabs = ['groups', 'ungrouped', 'settings'];
const adminTabs = ['groups', 'ungrouped', 'accounts', 'settings'];
function renderUnauthenticated() {
el('loginArea').classList.remove('hidden');
@@ -28,6 +28,56 @@ function renderState() {
renderStatistics();
renderGroups();
renderUngrouped();
renderStudentAccounts();
}
function allStudents() {
return [
...state.snapshot.groups.flatMap(group => group.members),
...state.snapshot.ungrouped
].sort((left, right) => String(left.student_no).localeCompare(String(right.student_no), 'zh-Hans-CN'));
}
function renderStudentAccounts() {
const keyword = el('studentNoSearch').value.trim().toLocaleLowerCase();
const students = allStudents().filter(student =>
String(student.student_no).toLocaleLowerCase().includes(keyword)
);
const select = el('studentSelect');
const previous = select.value;
select.innerHTML = students.length ? students.map(student => `
<option value="${student.id}">${text(student.student_no)} · ${text(student.name)}</option>
`).join('') : '<option value="">未找到匹配的学号</option>';
if (students.some(student => String(student.id) === previous)) {
select.value = previous;
}
}
async function submitStudentPassword(event) {
event.preventDefault();
const notice = el('studentPasswordNotice');
const select = el('studentSelect');
const student = allStudents().find(item => String(item.id) === select.value);
const newPassword = el('studentNewPassword').value;
const confirmPassword = el('studentConfirmPassword').value;
notice.textContent = '';
if (!student) {
notice.textContent = '请选择要修改密码的学生';
return;
}
if (newPassword !== confirmPassword) {
notice.textContent = '两次输入的新密码不一致';
return;
}
if (!requireConfirm(`确定修改 ${student.student_no} ${student.name} 的登录密码吗?修改后该学生需要重新登录。`)) return;
setButtonsDisabled(el('studentPasswordForm'), true);
const result = await api('teacher/student-password', {
method: 'POST', json: { student_id: Number(select.value), new_password: newPassword }
});
setButtonsDisabled(el('studentPasswordForm'), false);
notice.textContent = result.ok ? '' : result.message;
toast(result.message, !result.ok);
if (result.ok) el('studentPasswordForm').reset();
}
document.querySelectorAll('.admin-tab').forEach(button => {
@@ -263,6 +313,8 @@ async function logout() {
el('loginForm').addEventListener('submit', submitLogin);
el('teacherPasswordForm').addEventListener('submit', submitTeacherPassword);
el('studentPasswordForm').addEventListener('submit', submitStudentPassword);
el('studentNoSearch').addEventListener('input', renderStudentAccounts);
el('groupLimitForm').addEventListener('submit', submitGroupLimit);
el('logoutButton').addEventListener('click', logout);
el('lockButton').addEventListener('click', toggleLock);