技术栈:Go (Gin + GORM) + PHP + MySQL 5.7 + Redis 主要功能: - 多班级完全隔离(class_id 贯穿全系统) - 后端从 Python FastAPI 重写为 Go Gin(端口 56789) - 超级管理员独立登录(env 配置路径,默认账密 admin/Admin123) - 科任老师/课代表新角色 - 课代表作业管理页面 - 排行榜分项排行(操行分/考勤/作业) - 角色加减分上下限由班主任配置 - 家长改密功能(可开关) - 班级角色按需开关 - 宿舍号格式:南0-000 - 周度/月度重置功能 - MySQL 5.7 兼容 - Nginx 反向代理部署 开发者: Canglan 版权归属: Sea Network Technology Studio 许可证: Apache License 2.0
193 lines
5.1 KiB
Go
193 lines
5.1 KiB
Go
// ===========================================
|
|
// 多班级版班级管理系统 - Go 后端
|
|
//
|
|
// 开发者: Canglan
|
|
// 联系方式: admin@sea-studio.top
|
|
// 版权归属: Sea Network Technology Studio
|
|
// 许可证: Apache License 2.0
|
|
//
|
|
// 版权所有 © Sea Network Technology Studio
|
|
// ===========================================
|
|
|
|
package handler
|
|
|
|
import (
|
|
"strconv"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"hz-gitea.sea-studio.top/canglan/SharedClassManager/internal/middleware"
|
|
"hz-gitea.sea-studio.top/canglan/SharedClassManager/internal/repository"
|
|
"hz-gitea.sea-studio.top/canglan/SharedClassManager/internal/schema"
|
|
"hz-gitea.sea-studio.top/canglan/SharedClassManager/internal/service"
|
|
"hz-gitea.sea-studio.top/canglan/SharedClassManager/pkg/response"
|
|
)
|
|
|
|
// StudentHandler 学生端处理器
|
|
type StudentHandler struct {
|
|
studentService *service.StudentService
|
|
classRepo *repository.ClassRepo
|
|
}
|
|
|
|
// NewStudentHandler 创建学生端处理器
|
|
func NewStudentHandler(studentService *service.StudentService, classRepo *repository.ClassRepo) *StudentHandler {
|
|
return &StudentHandler{studentService: studentService, classRepo: classRepo}
|
|
}
|
|
|
|
// Dashboard 学生个人信息(仪表盘)
|
|
func (h *StudentHandler) Dashboard(c *gin.Context) {
|
|
studentID := middleware.GetStudentID(c)
|
|
if studentID == 0 {
|
|
response.BadRequest(c, "非学生用户")
|
|
return
|
|
}
|
|
|
|
result, err := h.studentService.GetStudentInfo(studentID)
|
|
if err != nil {
|
|
response.InternalError(c, err.Error())
|
|
return
|
|
}
|
|
response.Success(c, result, "操作成功")
|
|
}
|
|
|
|
// resolveStudentID 校验学生归属:学生只能查看自己的数据,家长只能查看关联子女,管理员可查看指定学生
|
|
func (h *StudentHandler) resolveStudentID(c *gin.Context) (int, bool) {
|
|
userType := middleware.GetUserType(c)
|
|
if userType == "student" {
|
|
// 学生只能查看自己的数据,忽略 URL 参数中的 student_id
|
|
studentID := middleware.GetStudentID(c)
|
|
if studentID == 0 {
|
|
response.BadRequest(c, "非学生用户")
|
|
return 0, false
|
|
}
|
|
return studentID, true
|
|
}
|
|
|
|
requestedID, ok := parseID(c, "student_id")
|
|
if !ok {
|
|
return 0, false
|
|
}
|
|
|
|
// 家长只能查看自己关联的子女数据
|
|
if userType == "parent" {
|
|
parentStudentID := middleware.GetStudentID(c)
|
|
if parentStudentID == 0 || parentStudentID != requestedID {
|
|
response.Forbidden(c, "无权访问该学生数据")
|
|
return 0, false
|
|
}
|
|
return requestedID, true
|
|
}
|
|
|
|
// 管理员/超级管理员允许查看(角色权限由路由中间件 RequireRole 控制)
|
|
return requestedID, true
|
|
}
|
|
|
|
// ConductHistory 学生操行分历史
|
|
func (h *StudentHandler) ConductHistory(c *gin.Context) {
|
|
studentID, ok := h.resolveStudentID(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
|
|
var query schema.StudentConductQuery
|
|
if err := c.ShouldBindQuery(&query); err != nil {
|
|
response.BadRequest(c, "参数错误")
|
|
return
|
|
}
|
|
|
|
result, err := h.studentService.GetConductHistory(studentID, query.Limit, query.Offset)
|
|
if err != nil {
|
|
response.InternalError(c, err.Error())
|
|
return
|
|
}
|
|
response.Success(c, result, "操作成功")
|
|
}
|
|
|
|
// Homework 学生作业情况
|
|
func (h *StudentHandler) Homework(c *gin.Context) {
|
|
studentID, ok := h.resolveStudentID(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
|
|
result, err := h.studentService.GetHomeworkStatus(studentID)
|
|
if err != nil {
|
|
response.InternalError(c, err.Error())
|
|
return
|
|
}
|
|
response.Success(c, result, "操作成功")
|
|
}
|
|
|
|
// Attendance 学生考勤记录
|
|
func (h *StudentHandler) Attendance(c *gin.Context) {
|
|
studentID, ok := h.resolveStudentID(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
|
|
month := c.Query("month")
|
|
result, err := h.studentService.GetAttendanceRecords(studentID, month)
|
|
if err != nil {
|
|
response.InternalError(c, err.Error())
|
|
return
|
|
}
|
|
response.Success(c, result, "操作成功")
|
|
}
|
|
|
|
// Ranking 操行分排行
|
|
func (h *StudentHandler) Ranking(c *gin.Context) {
|
|
classID := middleware.GetClassID(c)
|
|
|
|
// 检查班级功能开关:学生查看排行榜
|
|
feature, err := h.classRepo.GetFeature(classID, "student_view_ranking")
|
|
if err == nil && feature != nil && feature.Enabled == 0 {
|
|
response.Forbidden(c, "该功能暂未开放")
|
|
return
|
|
}
|
|
limit, _ := strconv.Atoi(c.DefaultQuery("limit", "50"))
|
|
if limit <= 0 {
|
|
limit = 50
|
|
}
|
|
if limit > 500 {
|
|
limit = 500
|
|
}
|
|
|
|
result, err := h.studentService.GetRanking(classID, limit)
|
|
if err != nil {
|
|
response.InternalError(c, err.Error())
|
|
return
|
|
}
|
|
response.Success(c, result, "操作成功")
|
|
}
|
|
|
|
// MyInfo 学生个人信息
|
|
func (h *StudentHandler) MyInfo(c *gin.Context) {
|
|
studentID := middleware.GetStudentID(c)
|
|
if studentID == 0 {
|
|
response.BadRequest(c, "非学生用户")
|
|
return
|
|
}
|
|
|
|
result, err := h.studentService.GetStudentInfo(studentID)
|
|
if err != nil {
|
|
response.InternalError(c, err.Error())
|
|
return
|
|
}
|
|
response.Success(c, result, "操作成功")
|
|
}
|
|
|
|
// SemesterRecords 学期归档记录
|
|
func (h *StudentHandler) SemesterRecords(c *gin.Context) {
|
|
studentID := middleware.GetStudentID(c)
|
|
if studentID <= 0 {
|
|
response.BadRequest(c, "非学生用户")
|
|
return
|
|
}
|
|
result, err := h.studentService.GetSemesterRecords(studentID)
|
|
if err != nil {
|
|
response.InternalError(c, err.Error())
|
|
return
|
|
}
|
|
response.Success(c, result, "操作成功")
|
|
}
|