## 实施
### 阶段 1:统一导航栏
- [x] 1.1 创建统一导航栏模板
【目标对象】`frontend/includes/nav.php`(新建)
【修改目的】将所有 admin 页面硬编码的导航栏抽取为共享模板,解决各页面导航不一致的问题;同时修复 dashboard.php 中密码链接 `passwork.php` 的拼写错误
【修改方式】新建 PHP 文件,定义导航栏 HTML 结构,直接读取 `$role` 和 `$current_page` 变量(已由 `header.php` 第 17-19 行定义)来动态生成导航项和 active 状态
【相关依赖】`frontend/includes/header.php`(第 17-19 行已定义 `$current_page`、`$user_type`、`$role` 变量,在 include nav.php 之前已可用)
【修改内容】
- 直接使用 `$role` 和 `$current_page` 变量(无需参数传递,因为 header.php 在 nav.php 之前被 include)
- 导航项及角色条件统一为:
- 首页(dashboard):所有管理员可见
- 学生管理(students):所有管理员可见
- 操行分管理(conduct):$role==='班主任' || $role==='班长'
- 作业管理(homework):$role==='班主任' || $role==='学习委员'
- 考勤管理(attendance):$role==='班主任' || $role==='考勤委员'
- 科目管理(subjects):$role==='班主任' || $role==='学习委员'
- 管理员管理(admins):$role==='班主任'
- 历史记录(history):所有管理员可见
- 修改密码(password):所有管理员可见
- 根据 `$current_page`(值为不含 `.php` 后缀的文件名,如 `dashboard`、`students`)为当前页面对应的导航项添加 `active` class
- 密码链接统一写为 `password.php`(修复 dashboard 中 `passwork.php` 拼写错误)
- 导航栏外层容器沿用现有 `
` 结构
- [x] 1.2 各 admin 页面引入统一导航模板
【目标对象】所有 `frontend/admin/*.php` 页面
【修改目的】移除各页面硬编码的 `
...
` 块,替换为 `include nav.php`,统一导航栏
【修改方式】在以下每个页面中,找到 `
` 到对应的 `
` 之间的导航栏块,整块替换为 ``
【相关依赖】`frontend/includes/nav.php`(任务 1.1 创建)
【修改内容】
- `frontend/admin/dashboard.php`:替换第 25-43 行的 `
...
` 为 include nav.php
- `frontend/admin/students.php`:替换第 25-43 行的 `
...
` 为 include nav.php
- `frontend/admin/conduct.php`:替换第 31-47 行的 `
...
` 为 include nav.php
- `frontend/admin/homework.php`:替换第 31-47 行的 `
...
` 为 include nav.php(注意:此页面后续会在任务 2.2 完全重写,此处仅替换导航栏部分)
- `frontend/admin/attendance.php`:替换第 31-47 行的 `
...
` 为 include nav.php(注意:此页面后续会在任务 3.3 完全重写,此处仅替换导航栏部分)
- `frontend/admin/history.php`:替换第 25-43 行的 `
...
` 为 include nav.php
- `frontend/admin/subjects.php`:替换第 30-48 行的 `
...
` 为 include nav.php
- `frontend/admin/admins.php`:替换第 30-46 行的 `
...
` 为 include nav.php
- `frontend/admin/password.php`:替换第 25-43 行的 `
...
` 为 include nav.php
- 替换后需确认 include 语句位于 `include header.php;` 之后、页面主体内容之前
### 阶段 2:作业管理改版
- [x] 2.1 清理 `admin.js` 中废弃的作业管理函数
【目标对象】`frontend/assets/js/admin.js`
【修改目的】移除作业管理改版后不再使用的旧作业管理函数,避免全局函数污染和潜在冲突
【修改方式】删除第 174-220 行的三个函数:`showAddAssignmentModal()`、`loadSubjectsForSelect()`、`submitAddAssignment()`
【相关依赖】无(homework.php 将在任务 2.2 完全重写,不再依赖这些全局函数)
【修改内容】
- 删除第 174-178 行的 `showAddAssignmentModal()` 函数
- 删除第 180-192 行的 `loadSubjectsForSelect()` 函数
- 删除第 194-220 行的 `submitAddAssignment()` 函数
- 注意:`admin.js` 中其他通用函数(如 `submitBatchPoints()`、`closeModal()`、`escapeHtml()`、`toggleSelectAll()`)保持不变,仍被 conduct.php 和 students.php 使用
- [x] 2.2 重写作业管理前端页面
【目标对象】`frontend/admin/homework.php`
【修改目的】将作业发布/提交管理模式改为单纯的加减操行分模式,交互方式参照 `conduct.php`
【修改方式】完全重写页面 HTML 内容和 `` 移到内联script之前;删除局部 `const API_BASE_URL` 和 `const JWT_STORAGE_KEY`/`USER_STORAGE_KEY`,改用 `window.API_BASE_URL = ''` 等全局变量(在common.js加载之前设置);将 `hw.subject` 改为 `hw.subject_name`
- save_session.php: 第106行 `$_SESSION['student_id'] = $data['user_id'];` 改为从请求中获取实际student_id:`$_SESSION['student_id'] = $data['student_id'] ?? $data['user_id'];`;同时添加student_id到requiredFields验证(仅学生时必须)
- index.php: 登录成功后save_session调用中添加 `student_id: userData.student_id` 参数
- student/attendance.php: 第21行 `$student_id = $_SESSION['student_id'];` 保持不变(修复save_session后此值会正确)
- admin/homework.php: 删除第176行重复的 `loadStudents();` 调用
- config.php: 第61行 `define('ICP_ENABLED', $config['ICP_ENABLED'] === 'false');` 改为 `define('ICP_ENABLED', $config['ICP_ENABLED'] !== 'false');`
- student/conduct_history.php: 文件为空,需要重定向到dashboard或创建基本页面。最简方案:创建一个重定向到 /student/dashboard.php 的页面
【目标对象】`backend/models/attendance.py`、`backend/services/attendance_service.py`、`backend/models/conduct.py`、`backend/services/conduct_service.py`、`backend/models/homework.py`、`backend/services/homework_service.py`、`backend/models/student.py`、`backend/middleware/permission.py`、`backend/services/auth_service.py`、`backend/schemas/student.py`、`backend/schemas/admin.py`、`backend/services/student_service.py`、`backend/routes/student.py`
【修改目的】数据库 schema(单班级系统)中 `students` 和 `assignments` 表没有 `class_id` 列,但后端代码中大量 SQL 引用了该列,导致 MySQL 报错 → 500 Internal Server Error。同时 `PermissionChecker.get_user_subject_ids()` 和 `check_can_manage_student()` 方法不存在但被调用。
【修改方式】全面移除所有 `class_id` 引用(单班级系统不需要),添加缺失的方法
【修改内容】
- attendance.py: get_class_records 移除 class_id 参数和 WHERE 条件
- attendance_service.py: 移除 class_id 传参和 check_can_manage_student 调用
- conduct.py: 移除 class_id 过滤条件
- conduct_service.py: 移除 class_id 传参和 check_can_manage_student 调用
- homework.py: 所有方法移除 class_id,get_assignments_by_class 重命名为 get_all_assignments
- homework_service.py: 移除 class_id 传参
- student.py: create 方法移除 class_id 参数和 INSERT 列
- permission.py: 添加 get_user_subject_ids 和 check_can_manage_student 方法
- auth_service.py: 移除 class_id 和 class_name 引用
- schemas/student.py 和 schemas/admin.py: 移除 class_id 和 class_name 字段
- student_service.py 和 routes/student.py: 移除 class_id 参数
- [x] 12.3 修复CRITICAL: total_points在所有Service中从未更新(操行分系统完全失效)
【目标对象】`backend/services/conduct_service.py`、`backend/services/attendance_service.py`、`backend/services/homework_service.py`
【修改目的】`StudentModel.update_total_points()` 方法已存在但从未被调用,导致 `students.total_points` 永远保持初始值60分,排行榜、仪表盘、家长端显示的所有总分都是错误的
【修改方式】
- conduct_service.py add_points: 在 `ConductModel.create_record()` 后添加 `await StudentModel.update_total_points(student_id, points_change)`
- conduct_service.py revoke_record: 重写撤销逻辑,先获取原记录,撤销后调用 `await StudentModel.update_total_points(record["student_id"], -record["points_change"])` 反向恢复总分
- attendance_service.py add_attendance: 在 `ConductModel.create_record()` 后添加 `await StudentModel.update_total_points(student_id, points_change)`
- homework_service.py update_submission_status: 在 `ConductModel.create_record()` 后添加 `await StudentModel.update_total_points(submission["student_id"], points_change)`
- [x] 12.4 修复前端: student/homework.php字段名 + nav.php劳动委员 + parent/attendance.php死链
【目标对象】`frontend/student/homework.php`、`frontend/includes/nav.php`、`frontend/parent/attendance.php`
【修改目的】1) homework.php中hw.subject字段名与后端API不匹配;2) 劳动委员无法在导航栏看到操行分管理入口(README要求±1分权限);3) parent/attendance.php有指向不存在的/parent/homework.php的死链接
【修改方式】
- student/homework.php 第56行: `hw.subject` → `hw.subject_name`
- nav.php 第4行: 添加 `|| $role === '劳动委员'` 到操行分管理导航条件
- parent/attendance.php: 删除指向不存在的 /parent/homework.php 的死链接,导航只保留"首页"和"考勤记录"
- [x] 12.5 修复学生端导航死链接: 3个页面的"操行分"链接指向不存在的/student/conduct.php
【目标对象】`frontend/student/homework.php`、`frontend/student/attendance.php`、`frontend/student/password.php`
【修改目的】学生端3个子页面导航栏中的"操行分"链接指向 `/student/conduct.php`,但实际文件名是 `conduct_history.php`,导致404
【修改方式】
- password.php 第26行: `/student/conduct.php` → `/student/conduct_history.php`
- [x] 12.6 修复劳动委员页面级权限被拦截 + 仪表盘快捷操作缺失
【目标对象】`frontend/admin/conduct.php`、`frontend/admin/dashboard.php`
【修改目的】README规定劳动委员有操行分管理权限(±1分),但conduct.php页面级权限检查只允许班主任和班长访问,劳动委员会被重定向到dashboard;dashboard.php快捷操作也缺少劳动委员
【修改方式】
- conduct.php 第23行: `['班主任', '班长']` → `['班主任', '班长', '劳动委员']`
- conduct.php 第115行: 分数变动提示文字改为 if/elseif/else 结构,劳动委员显示"劳动委员仅限±1分"
- dashboard.php 第61行: 快捷操作条件添加劳动委员
- attendance.php 第27行: `/student/conduct.php` → `/student/conduct_history.php`
- password.php 第26行: `/student/conduct.php` → `/student/conduct_history.php`
- [x] 12.7 同步学生端dashboard.php页眉(使用header.php/footer.php)
【目标对象】`frontend/student/dashboard.php`
【修改目的】学生端dashboard.php使用自定义HTML结构,与管理端不统一,缺少共享页眉和ICP备案号
【修改方式】
- 移除自定义的 ``, ``, `` 和自定义 header div
- 添加 `$page_title = '学生端'; include __DIR__ . '/../includes/header.php';`
- 移除重复的 `window.API_BASE_URL` 等变量设置(header.php已处理)
- 添加 `include __DIR__ . '/../includes/footer.php';` 以显示ICP备案号
- [x] 12.8 登录页面补充备案号悬挂
【目标对象】`frontend/index.php`
【修改目的】登录页面的页脚缺少ICP备案号
【修改方式】
- 在登录页 footer 的版权信息后,添加ICP备案号链接(使用 `ICP_ENABLED` 和 `ICP_NUMBER` 常量)
- 链接指向 https://beian.miit.gov.cn/,target="_blank"
- [x] 12.9 修复用户报告的4个运行时Bug
【目标对象】`backend/schemas/admin.py`、`frontend/admin/conduct.php`、`frontend/admin/homework.php`、`frontend/admin/attendance.php`
【修改目的】用户报告:1) 历史记录在加减分后无内容; 2) 管理员无法添加学生; 3) 加减分页面仅显示21人; 4) 管理端修改密码失败
【修改方式】
- 12.9a 历史记录问题:已在12.3中通过添加 `StudentModel.update_total_points()` 调用修复
- 12.9b 添加学生422错误:`add_student` 路由使用裸函数参数而非Pydantic body,FastAPI从query string读取导致422。创建 `AddStudentRequest` schema,路由改用Pydantic body
- 12.9c 仅显示21人:`loadStudents()` 未传 `page_size`,后端默认20。前端3个文件添加 `{page_size: 1000}`
- 12.9d 修改密码:已确认代码正确(`sha1_md5_password` + `need_change_password=0`)
- [x] 12.10 修复登录及管理员日志未写入数据库
【目标对象】`backend/models/log.py`(新建)、`backend/services/log_service.py`(新建)、`backend/services/auth_service.py`、`backend/routes/auth.py`、`backend/routes/admin.py`
【修改目的】数据库中 `login_logs` 和 `operation_logs` 表已存在,但没有任何后端代码写入数据。README中规划了 `models/operation_log.py` 和 `services/log_service.py`,但文件不存在
【修改方式】
- 新建 `backend/models/log.py`:包含 `LoginLogModel` 和 `OperationLogModel`,分别向 login_logs 和 operation_logs 表写入记录
- 新建 `backend/services/log_service.py`:`LogService` 提供 `write_login_log` 和 `write_operation_log` 方法,用 try-except 包裹确保日志写入失败不影响主业务
- 修改 `auth_service.py`:login 方法增加 `user_agent` 参数,在5个退出点(失败次数过多、用户不存在、密码错误、账号禁用、登录成功)均写入 login_logs
- 修改 `auth.py`:从 HTTP 请求头获取 user-agent 并传递给 AuthService.login()
- 修改 `admin.py`:8个管理操作成功后写入 operation_logs(import_students、add_student、add_points、revoke_record、create_assignment、update_submission、add_attendance、add_admin)
- [x] 12.11 修复CRITICAL: 登录返回数据缺少student_id导致学生端完全无法工作
【目标对象】`backend/services/auth_service.py`、`backend/routes/auth.py`
【修改目的】登录成功后返回给前端的数据中没有 student_id 字段,导致 index.php 无法传递 student_id 给 save_session.php,Session 中缺少 student_id,学生端所有页面 $student_id 为 null,所有 API 调用路径变成 /api/student/conduct/null 导致失败。这是学生端显示"假数据"的根本原因
【修改方式】
- auth_service.py: login 方法返回字典添加 `"student_id": user["student_id"]`
- auth.py: 登录路由 success_response data 字典添加 `"student_id": result.get("student_id")`
- [x] 12.12 批量修复6个管理端/学生端Bug
【目标对象】`backend/models/user.py`、`backend/routes/admin.py`、`frontend/assets/js/admin.js`、`backend/services/conduct_service.py`、`backend/models/conduct.py`、`frontend/admin/history.php`
【修改目的】用户报告6个运行时Bug:1) 修改密码失败;2) 管理端学生列表422;3) 添加管理员404;4) admin.js报错null;5) 历史记录500;6) history.php学生列表不完整
【修改方式】
- user.py: get_by_user_id SQL 添加 password_hash 字段(修改密码时验证旧密码需要此字段)
- admin.py: page_size 上限 le=100→le=1000;路由 /admin/add→/add, /admin/list→/list(避免双重/api/admin前缀)
- admin.js: showAddAdminModal 中 addAdminForm.reset() 改为 ?.reset()
- conduct_service.py: get_history 方法开头添加空字符串→None转换
- conduct.py: get_all_records 方法开头添加空字符串→None转换
- history.php: loadStudentsForSelect 传 {page_size: 1000}
- [x] 12.13 修复强制改密"原密码错误" + 科目管理/管理员管理500错误确认
【目标对象】`backend/schemas/auth.py`、`backend/services/auth_service.py`、`backend/routes/auth.py`、`frontend/student/dashboard.php`
【修改目的】用户报告3个Bug:1) 学生端首次登录强制改密时没有原密码输入框,但提交后报"原密码错误";2) 科目管理页/api/subject/list返回500;3) 管理员管理页/api/admin/list返回500
【修改方式】
- schemas/auth.py: ChangePasswordRequest中old_password改为可选(default=""),新增force字段(bool, default=False)
- auth_service.py: change_password方法新增force参数,当force=True时跳过旧密码验证
- auth.py: change_password路由从请求中读取force参数传递给服务层
- dashboard.php: 强制改密请求中添加force:true, old_password设为空字符串
- 科目管理500和管理员管理500: 经代码审查确认代码逻辑正确(SQL、路由、模型均无问题),500错误为后端服务未重启导致旧代码仍在运行
- [x] 12.14 添加全局异常处理器 + 4个500路由添加try-except + 历史记录page_size上限修复
【目标对象】`backend/main.py`、`backend/routes/subject.py`、`backend/routes/admin.py`、`backend/routes/student.py`
【修改目的】用户确认后端已重启但仍报500,4个路由持续返回500 Internal Server Error(科目管理、管理员管理、历史记录、学生端操行分)。无法直接查看后端日志,需通过全局异常处理器和路由级try-except捕获具体错误原因
【修改方式】
- main.py: 添加全局异常处理器 global_exception_handler,捕获所有未处理异常,返回包含str(exc)的message和可选的traceback detail(仅DEBUG模式)
- subject.py: get_subjects路由添加try-except,新增logger导入
- admin.py: get_admins和get_conduct_history路由添加try-except;get_conduct_history的page_size参数le=100→le=1000
- student.py: get_conduct_history路由添加try-except,新增logger导入