/** * 教师端功能管理 */ class TeacherManager { constructor() { this.apiBase = '/api/teacher'; this.init(); } async init() { this.updateCurrentTime(); setInterval(() => this.updateCurrentTime(), 1000); await this.loadUserInfo(); // 页面路由逻辑 if (document.getElementById('courseList')) { this.initDashboard(); } if (document.getElementById('studentTableBody') && document.getElementById('courseSelect')) { this.initGradeEntry(); } if (document.getElementById('gradeTableBody')) { this.initGradeManagement(); } if (document.getElementById('profileForm')) { this.initProfilePage(); } } updateCurrentTime() { const timeElement = document.getElementById('currentTime'); if (timeElement) { const now = new Date(); const options = { year: 'numeric', month: 'long', day: 'numeric', hour: '2-digit', minute: '2-digit', second: '2-digit' }; timeElement.textContent = now.toLocaleString('zh-CN', options); } } async loadUserInfo() { try { const res = await fetch('/api/auth/me'); const result = await res.json(); if (result.success && result.data.user) { const user = result.data.user; this.user = user; // Update Sidebar const nameEls = document.querySelectorAll('#teacherName, #userName, #profileName'); const idEls = document.querySelectorAll('#teacherId, #profileId, #inputTeacherId'); nameEls.forEach(el => el.textContent = user.name); idEls.forEach(el => { if (el.tagName === 'INPUT') el.value = user.id; else el.textContent = user.id; }); // Profile Page Specific const inputName = document.getElementById('inputName'); const inputClass = document.getElementById('inputClass'); if (inputName) inputName.value = user.name; if (inputClass) inputClass.value = user.class || ''; } } catch (error) { console.error('Load user info failed:', error); } } // ================= Dashboard ================= async initDashboard() { await Promise.all([ this.loadCourses(), this.loadManagedClasses() ]); this.initAddCourse(); this.initEditCourse(); } async loadManagedClasses() { const managedClassesSection = document.getElementById('managedClassesSection'); const managedClassList = document.getElementById('managedClassList'); const classCountEl = document.getElementById('classCount'); if (!managedClassList) return; try { const res = await fetch(`${this.apiBase}/my-classes`); const result = await res.json(); if (result.success && result.data.classes.length > 0) { const classes = result.data.classes; classCountEl.textContent = classes.length; managedClassesSection.style.display = 'block'; managedClassList.innerHTML = classes.map(c => `
${c.major || '专业未设置'} | ${c.grade}级