v0.8.5测试

This commit is contained in:
2026-04-16 10:36:34 +08:00
parent 4cbf294cd9
commit 112dc94f7c
12 changed files with 297 additions and 90 deletions

View File

@@ -99,3 +99,10 @@ class UserModel:
sql = "SELECT 1 FROM users WHERE username = %s" sql = "SELECT 1 FROM users WHERE username = %s"
result = await execute_one(sql, (username,)) result = await execute_one(sql, (username,))
return result is not None return result is not None
@staticmethod
async def update_status(user_id: int, status: int) -> bool:
"""更新用户状态0=禁用1=启用)"""
sql = "UPDATE users SET status = %s WHERE user_id = %s"
result = await execute_update(sql, (status, user_id))
return result > 0

View File

@@ -26,7 +26,8 @@ from services.log_service import LogService
from schemas.admin import ( from schemas.admin import (
AddPointsRequest, RevokeRequest, AddAdminRequest, AddPointsRequest, RevokeRequest, AddAdminRequest,
AddStudentRequest, AddStudentRequest,
UpdateHomeworkStatusRequest, AddAttendanceRequest UpdateHomeworkStatusRequest, AddAttendanceRequest,
UpdateAdminRequest, DeleteAdminRequest
) )
from utils.response import success_response, error_response from utils.response import success_response, error_response
from utils.logger import get_logger from utils.logger import get_logger
@@ -375,3 +376,63 @@ async def get_admins(request: Request):
except Exception as e: except Exception as e:
logger.error(f"获取管理员列表失败: {e}", exc_info=True) logger.error(f"获取管理员列表失败: {e}", exc_info=True)
return error_response(message=f"获取管理员列表失败: {str(e)}") return error_response(message=f"获取管理员列表失败: {str(e)}")
@router.put("/update/{user_id}")
async def update_admin(request: Request, user_id: int, req: UpdateAdminRequest):
"""更新管理员信息(班主任)"""
user = await get_current_user(request)
is_teacher = await PermissionChecker.check_is_teacher(user["user_id"])
if not is_teacher:
return error_response(message="仅班主任可更新管理员", code=403)
if req.role_type not in ["班长", "学习委员", "考勤委员", "劳动委员", "志愿委员"]:
return error_response(message="无效的角色类型", code=400)
from models.admin_role import AdminRoleModel
result = await AdminRoleModel.update_role(
user_id=user_id,
role_type=req.role_type
)
if result:
await LogService.write_operation_log(
operator_id=user["user_id"], operator_name=user["username"],
operator_role="班主任", operation_type="update_admin",
target_type="admin", target_id=user_id,
details=f"更新管理员角色为: {req.role_type}",
ip=request.client.host
)
return success_response(message="管理员更新成功")
else:
return error_response(message="更新失败或管理员不存在")
@router.delete("/delete/{user_id}")
async def delete_admin(request: Request, user_id: int):
"""删除管理员(班主任)"""
user = await get_current_user(request)
is_teacher = await PermissionChecker.check_is_teacher(user["user_id"])
if not is_teacher:
return error_response(message="仅班主任可删除管理员", code=403)
# 防止删除自己
if user_id == user["user_id"]:
return error_response(message="不能删除当前登录的管理员", code=400)
from models.admin_role import AdminRoleModel
from models.user import UserModel
# 先删除角色记录
role_deleted = await AdminRoleModel.delete(user_id)
if role_deleted:
# 再删除用户账号(软删除,将状态设为禁用)
await UserModel.update_status(user_id, 0)
await LogService.write_operation_log(
operator_id=user["user_id"], operator_name=user["username"],
operator_role="班主任", operation_type="delete_admin",
target_type="admin", target_id=user_id,
details=f"删除管理员: ID={user_id}",
ip=request.client.host
)
return success_response(message="管理员删除成功")
else:
return error_response(message="删除失败或管理员不存在")

View File

@@ -86,3 +86,15 @@ class AddAttendanceRequest(BaseModel):
reason: Optional[str] = None reason: Optional[str] = None
apply_deduction: bool = True apply_deduction: bool = True
custom_deduction: Optional[int] = Field(default=None, gt=0, description="自定义扣分值") custom_deduction: Optional[int] = Field(default=None, gt=0, description="自定义扣分值")
class UpdateAdminRequest(BaseModel):
"""更新管理员请求"""
user_id: int = Field(..., description="用户ID")
real_name: str = Field(..., min_length=1, max_length=50, description="真实姓名")
role_type: str = Field(..., description="角色类型")
class DeleteAdminRequest(BaseModel):
"""删除管理员请求"""
user_id: int = Field(..., description="用户ID")

View File

@@ -78,12 +78,6 @@ class AttendanceService:
# 创建扣分记录 # 创建扣分记录
student = await StudentModel.get_by_id(student_id) student = await StudentModel.get_by_id(student_id)
if student: if student:
# 检查分数是否会超出范围(防止溢出)
current_points = student.get("total_points", 0)
new_points = current_points + points_change
if new_points < 0:
return {"success": False, "message": f"分数不能为负(当前{current_points},扣{abs(points_change)}"}
# 获取操作人姓名 # 获取操作人姓名
user = await UserModel.get_by_user_id(recorder_id) user = await UserModel.get_by_user_id(recorder_id)
recorder_name = user.get("real_name", "班主任") if user else "班主任" recorder_name = user.get("real_name", "班主任") if user else "班主任"

View File

@@ -80,18 +80,6 @@ class ConductService:
fail_count += 1 fail_count += 1
continue continue
# 检查分数是否会超出范围(防止溢出)
current_points = student.get("total_points", 0)
new_points = current_points + points_change
if new_points < 0:
details.append({"student_id": student_id, "error": f"分数不能为负(当前{current_points},操作{points_change}"})
fail_count += 1
continue
if new_points > 100:
details.append({"student_id": student_id, "error": f"分数不能超过100当前{current_points},操作{points_change}"})
fail_count += 1
continue
# 创建记录 # 创建记录
record_id = await ConductModel.create_record( record_id = await ConductModel.create_record(
student_id=student_id, student_id=student_id,

View File

@@ -109,12 +109,6 @@ class HomeworkService:
# 创建扣分记录 # 创建扣分记录
student = await StudentModel.get_by_id(submission["student_id"]) student = await StudentModel.get_by_id(submission["student_id"])
if student: if student:
# 检查分数是否会超出范围(防止溢出)
current_points = student.get("total_points", 0)
new_points = current_points + points_change
if new_points < 0:
return {"success": False, "message": f"分数不能为负(当前{current_points},扣{abs(points_change)}"}
# 获取操作人姓名 # 获取操作人姓名
from models.user import UserModel from models.user import UserModel
user = await UserModel.get_by_user_id(operator_id) user = await UserModel.get_by_user_id(operator_id)

View File

@@ -37,7 +37,7 @@ include __DIR__ . '/../includes/header.php';
<div class="table-wrapper"> <div class="table-wrapper">
<table class="table"> <table class="table">
<thead> <thead>
<tr><th>用户名</th><th>姓名</th><th>角色</th></tr> <tr><th>用户名</th><th>姓名</th><th>角色</th><th>操作</th></tr>
</thead> </thead>
<tbody id="adminList"></tbody> <tbody id="adminList"></tbody>
</table> </table>
@@ -85,7 +85,45 @@ include __DIR__ . '/../includes/header.php';
</div> </div>
</div> </div>
<!-- 编辑管理员模态框 -->
<div id="editAdminModal" class="modal">
<div class="modal-content">
<div class="modal-header">
<h3>编辑管理员</h3>
<button class="modal-close" onclick="closeModal('editAdminModal')">&times;</button>
</div>
<form onsubmit="event.preventDefault(); submitEditAdmin()">
<input type="hidden" id="editAdminUserId">
<div class="form-group">
<label>用户名</label>
<input type="text" id="editAdminUsername" disabled>
</div>
<div class="form-group">
<label>姓名</label>
<input type="text" id="editAdminRealName" disabled>
</div>
<div class="form-group">
<label>角色</label>
<select id="editAdminRole" required>
<option value="">请选择角色</option>
<option value='班长'>班长</option>
<option value='学习委员'>学习委员</option>
<option value='考勤委员'>考勤委员</option>
<option value='劳动委员'>劳动委员</option>
<option value='志愿委员'>志愿委员</option>
</select>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-primary">保存</button>
<button type="button" class="btn" onclick="closeModal('editAdminModal')">取消</button>
</div>
</form>
</div>
</div>
<script> <script>
var currentEditUserId = null;
async function loadAdmins() { async function loadAdmins() {
const res = await apiGet('/api/admin/list'); const res = await apiGet('/api/admin/list');
if (res && res.success) { if (res && res.success) {
@@ -95,10 +133,14 @@ async function loadAdmins() {
<td>${escapeHtml(admin.username)}</td> <td>${escapeHtml(admin.username)}</td>
<td>${escapeHtml(admin.real_name)}</td> <td>${escapeHtml(admin.real_name)}</td>
<td>${escapeHtml(admin.role_type)}</td> <td>${escapeHtml(admin.role_type)}</td>
<td>
<button class="btn btn-sm btn-primary" onclick="showEditAdminModal(${admin.user_id}, '${escapeHtml(admin.username)}', '${escapeHtml(admin.real_name)}', '${escapeHtml(admin.role_type)}')">编辑</button>
<button class="btn btn-sm btn-danger" onclick="deleteAdmin(${admin.user_id}, '${escapeHtml(admin.real_name)}')">删除</button>
</td>
</tr>`; </tr>`;
}); });
if (res.data.admins.length === 0) { if (res.data.admins.length === 0) {
html = '<tr><td colspan="3" style="text-align:center;">暂无管理员</td></tr>'; html = '<tr><td colspan="4" style="text-align:center;">暂无管理员</td></tr>';
} }
document.getElementById('adminList').innerHTML = html; document.getElementById('adminList').innerHTML = html;
} }
@@ -106,7 +148,10 @@ async function loadAdmins() {
function showAddAdminModal() { function showAddAdminModal() {
document.getElementById('addAdminModal').style.display = 'flex'; document.getElementById('addAdminModal').style.display = 'flex';
document.getElementById('addAdminForm')?.reset(); document.getElementById('adminUsername').value = '';
document.getElementById('adminRealName').value = '';
document.getElementById('adminPassword').value = '';
document.getElementById('adminRole').value = '';
} }
async function submitAddAdmin() { async function submitAddAdmin() {
@@ -135,6 +180,53 @@ async function submitAddAdmin() {
} }
} }
function showEditAdminModal(userId, username, realName, roleType) {
currentEditUserId = userId;
document.getElementById('editAdminUserId').value = userId;
document.getElementById('editAdminUsername').value = username;
document.getElementById('editAdminRealName').value = realName;
document.getElementById('editAdminRole').value = roleType;
document.getElementById('editAdminModal').style.display = 'flex';
}
async function submitEditAdmin() {
if (!currentEditUserId) return;
const roleType = document.getElementById('editAdminRole').value;
if (!roleType) {
showToast('请选择角色', 'warning');
return;
}
const res = await apiPut(`/api/admin/update/${currentEditUserId}`, {
user_id: currentEditUserId,
real_name: document.getElementById('editAdminRealName').value,
role_type: roleType
});
if (res && res.success) {
showToast('管理员更新成功');
closeModal('editAdminModal');
loadAdmins();
} else {
showToast(res?.message || '更新失败', 'error');
}
}
async function deleteAdmin(userId, realName) {
if (!confirm(`确定要删除管理员 "${realName}" 吗?此操作不可恢复。`)) {
return;
}
const res = await apiDelete(`/api/admin/delete/${userId}`);
if (res && res.success) {
showToast('管理员删除成功');
loadAdmins();
} else {
showToast(res?.message || '删除失败', 'error');
}
}
function closeModal(modalId) { function closeModal(modalId) {
const modal = document.getElementById(modalId); const modal = document.getElementById(modalId);
if (modal) modal.style.display = 'none'; if (modal) modal.style.display = 'none';

View File

@@ -38,6 +38,14 @@ include __DIR__ . '/../includes/header.php';
<label>日期</label> <label>日期</label>
<input type="date" id="attendanceDate" value="<?php echo date('Y-m-d'); ?>"> <input type="date" id="attendanceDate" value="<?php echo date('Y-m-d'); ?>">
</div> </div>
<div class="form-group" style="margin:0">
<label>时段</label>
<select id="attendanceSlot">
<option value="morning">早 8:15</option>
<option value="afternoon">下午 14:00</option>
<option value="evening">晚 19:30</option>
</select>
</div>
<div class="status-group"> <div class="status-group">
<button class="status-btn active" data-status="absent" onclick="selectStatus(this)" data-default-deduction="3">缺勤</button> <button class="status-btn active" data-status="absent" onclick="selectStatus(this)" data-default-deduction="3">缺勤</button>
<button class="status-btn" data-status="late" onclick="selectStatus(this)" data-default-deduction="1">迟到</button> <button class="status-btn" data-status="late" onclick="selectStatus(this)" data-default-deduction="1">迟到</button>
@@ -166,20 +174,7 @@ async function submitAttendance() {
const customDeduction = document.getElementById('customDeduction').value; const customDeduction = document.getElementById('customDeduction').value;
const customDeductionValue = customDeduction ? parseInt(customDeduction) : null; const customDeductionValue = customDeduction ? parseInt(customDeduction) : null;
// 检查是否有已存在记录的学生 // 批量提交(不再检查已有记录,允许同一学生同一天多次考勤)
const hasRecordStudents = [];
selectedCells.forEach(cell => {
if (cell.classList.contains('has-record')) {
hasRecordStudents.push(cell.dataset.name);
}
});
if (hasRecordStudents.length > 0) {
const confirmed = confirm(`以下学生已有考勤记录:${hasRecordStudents.join('、')},是否继续提交?`);
if (!confirmed) return;
}
// 批量提交
const promises = []; const promises = [];
selectedCells.forEach(cell => { selectedCells.forEach(cell => {
const studentId = parseInt(cell.dataset.id); const studentId = parseInt(cell.dataset.id);

View File

@@ -36,7 +36,7 @@ include __DIR__ . '/../includes/header.php';
<div class="action-buttons"> <div class="action-buttons">
<button class="btn btn-primary" onclick="showBatchPointsModal()">批量加减分</button> <button class="btn btn-primary" onclick="showBatchPointsModal()">批量加减分</button>
<?php if ($role === '班主任'): ?> <?php if ($role === '班主任'): ?>
<button class="btn btn-secondary" onclick="exportConductRecords()">导出记录</button> <button class="btn btn-secondary" onclick="exportMoralityRecords()">导出德育分记录</button>
<?php endif; ?> <?php endif; ?>
</div> </div>
</div> </div>
@@ -96,33 +96,48 @@ function showSinglePointsModal(studentId, studentName) {
document.getElementById('batchPointsModal').style.display = 'flex'; document.getElementById('batchPointsModal').style.display = 'flex';
} }
// 导出操行分记录 // 导出德育分记录(格式:学号 姓名 分数 加分历史 减分记录
async function exportConductRecords() { async function exportMoralityRecords() {
const startDate = prompt('请输入开始日期格式YYYY-MM-DD留空则不限:', ''); showToast('正在导出德育分记录...', 'info');
if (startDate === null) return;
const endDate = prompt('请输入结束日期格式YYYY-MM-DD留空则不限:', '');
if (endDate === null) return;
showToast('正在导出...', 'info');
try { try {
const params = { page: 1, page_size: 1000 }; // 获取所有学生
if (startDate) params.start_date = startDate; const studentsRes = await apiGet('/api/admin/students', { page_size: 1000 });
if (endDate) params.end_date = endDate; if (!studentsRes || !studentsRes.success) {
showToast('获取学生列表失败', 'error');
const res = await apiGet('/api/admin/conduct/history', params);
if (res && res.success && res.data.records) {
const records = res.data.records;
if (records.length === 0) {
showToast('没有找到记录', 'warning');
return; return;
} }
const students = studentsRes.data.students;
if (students.length === 0) {
showToast('没有找到学生', 'warning');
return;
}
// 获取每个学生的历史记录
const studentRecords = [];
for (const student of students) {
const historyRes = await apiGet(`/api/student/conduct/${student.student_id}`, { limit: 1000 });
if (historyRes && historyRes.success) {
const records = historyRes.data.records || [];
const positiveRecords = records.filter(r => r.points_change > 0).map(r => `${r.reason}(${r.points_change > 0 ? '+' : ''}${r.points_change})`);
const negativeRecords = records.filter(r => r.points_change < 0).map(r => `${r.reason}(${r.points_change})`);
studentRecords.push({
student_no: student.student_no,
name: student.name,
total_points: historyRes.data.total_points || 0,
positive_history: positiveRecords.join(', '),
negative_history: negativeRecords.join(', ')
});
}
}
// 构建CSV内容 // 构建CSV内容
let csv = '\uFEFF'; // BOM for UTF-8 let csv = '\uFEFF'; // BOM for UTF-8
csv += '时间,学号,姓名,分数变动,原因,操作人\n'; csv += '学号,姓名,分数,加分历史,减分记录\n';
records.forEach(r => { studentRecords.forEach(s => {
csv += `${r.created_at || ''},${r.student_no || ''},${r.student_name || ''},${r.points_change > 0 ? '+' : ''}${r.points_change},${(r.reason || '').replace(/,/g, ';')},${r.recorder_name || ''}\n`; csv += `${s.student_no},${s.name},${s.total_points},"${(s.positive_history || '').replace(/"/g, '""')}","${(s.negative_history || '').replace(/"/g, '""')}"\n`;
}); });
// 下载文件 // 下载文件
@@ -130,16 +145,13 @@ async function exportConductRecords() {
const url = URL.createObjectURL(blob); const url = URL.createObjectURL(blob);
const link = document.createElement('a'); const link = document.createElement('a');
link.href = url; link.href = url;
link.download = `操行分记录_${new Date().toISOString().slice(0,10)}.csv`; link.download = `德育分记录_${new Date().toISOString().slice(0,10)}.csv`;
document.body.appendChild(link); document.body.appendChild(link);
link.click(); link.click();
document.body.removeChild(link); document.body.removeChild(link);
URL.revokeObjectURL(url); URL.revokeObjectURL(url);
showToast(`导出成功,共${records.length}条记录`); showToast(`导出成功,共${studentRecords.length}名学生`);
} else {
showToast('导出失败:' + (res?.message || '未知错误'), 'error');
}
} catch (err) { } catch (err) {
showToast('导出失败:' + err.message, 'error'); showToast('导出失败:' + err.message, 'error');
} }

View File

@@ -63,7 +63,7 @@ async function loadDashboard() {
} }
if ('<?php echo $role; ?>' === '班主任') { if ('<?php echo $role; ?>' === '班主任') {
quickActions += '<button class="btn btn-success" onclick="location.href=\'/admin/students.php\'">导入学生</button>'; quickActions += '<button class="btn btn-success" onclick="location.href=\'/admin/students.php\'">导入学生</button>';
quickActions += '<button class="btn btn-secondary" onclick="location.href=\'/admin/conduct.php\'">导出记录</button>'; quickActions += '<button class="btn btn-secondary" onclick="location.href=\'/admin/conduct.php\'">导出德育分记录</button>';
} }
document.getElementById('quickActions').innerHTML = quickActions || '<p>暂无快捷操作</p>'; document.getElementById('quickActions').innerHTML = quickActions || '<p>暂无快捷操作</p>';

View File

@@ -42,6 +42,9 @@ include __DIR__ . '/../includes/header.php';
</select> </select>
</div> </div>
<button class="btn btn-primary" onclick="loadHistory(1)">查询</button> <button class="btn btn-primary" onclick="loadHistory(1)">查询</button>
<?php if ($role === '班主任'): ?>
<button class="btn btn-secondary" onclick="exportHistoryRecords()">导出历史记录</button>
<?php endif; ?>
</div> </div>
<div class="table-wrapper"> <div class="table-wrapper">
@@ -142,6 +145,55 @@ function renderHistoryPagination() {
container.innerHTML = html; container.innerHTML = html;
} }
// 导出历史记录
async function exportHistoryRecords() {
const startDate = document.getElementById('historyStartDate').value;
const endDate = document.getElementById('historyEndDate').value;
const studentId = document.getElementById('historyStudentId').value;
showToast('正在导出历史记录...', 'info');
try {
const params = { page: 1, page_size: 1000 };
if (startDate) params.start_date = startDate;
if (endDate) params.end_date = endDate;
if (studentId) params.student_id = studentId;
const res = await apiGet('/api/admin/conduct/history', params);
if (res && res.success && res.data.records) {
const records = res.data.records;
if (records.length === 0) {
showToast('没有找到记录', 'warning');
return;
}
// 构建CSV内容
let csv = '\uFEFF'; // BOM for UTF-8
csv += '时间,学号,姓名,分数变动,原因,操作人\n';
records.forEach(r => {
csv += `${r.created_at || ''},${r.student_no || ''},${r.student_name || ''},${r.points_change > 0 ? '+' : ''}${r.points_change},${(r.reason || '').replace(/,/g, ';')},${r.recorder_name || ''}\n`;
});
// 下载文件
const blob = new Blob([csv], { type: 'text/csv;charset=utf-8;' });
const url = URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = url;
link.download = `历史记录_${new Date().toISOString().slice(0,10)}.csv`;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
URL.revokeObjectURL(url);
showToast(`导出成功,共${records.length}条记录`);
} else {
showToast('导出失败:' + (res?.message || '未知错误'), 'error');
}
} catch (err) {
showToast('导出失败:' + err.message, 'error');
}
}
loadStudentsForSelect(); loadStudentsForSelect();
loadHistory(); loadHistory();
</script> </script>

View File

@@ -301,7 +301,7 @@ include __DIR__ . '/../includes/header.php';
const ranking = rankingRes.data.ranking || []; const ranking = rankingRes.data.ranking || [];
const rank = ranking.find(s => s.student_id === parseInt(STUDENT_ID)); const rank = ranking.find(s => s.student_id === parseInt(STUDENT_ID));
if (rank) { if (rank) {
document.getElementById('studentRank').textContent = `第${rank.rank}名 / ${ranking.length}人`; document.getElementById('studentRank').textContent = rank.rank;
} else { } else {
document.getElementById('studentRank').textContent = '--'; document.getElementById('studentRank').textContent = '--';
} }