- 后端从 Python FastAPI 重写为 Go Gin(端口 56789) - 多班级完全隔离 - 超级管理员独立登录 - 课代表作业管理、排行榜分项排行 - 角色加减分上下限可配置 - 家长改密功能(可开关) - 周度/月度重置功能 - MySQL 5.7 兼容 - 43轮代码审查+全部修复 - Apache 2.0 许可证
107 lines
2.7 KiB
Go
107 lines
2.7 KiB
Go
// ===========================================
|
|
// 多班级版班级管理系统 - Go 后端
|
|
//
|
|
// 开发者: Canglan
|
|
// 联系方式: admin@sea-studio.top
|
|
// 版权归属: Sea Network Technology Studio
|
|
// 许可证: Apache License 2.0
|
|
//
|
|
// 版权所有 © Sea Network Technology Studio
|
|
// ===========================================
|
|
|
|
package response
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// Response 统一响应结构体
|
|
type Response struct {
|
|
Success bool `json:"success"`
|
|
Code int `json:"code"`
|
|
Message string `json:"message"`
|
|
Data interface{} `json:"data"`
|
|
}
|
|
|
|
// PageData 分页响应数据
|
|
type PageData struct {
|
|
Items interface{} `json:"items"`
|
|
Total int64 `json:"total"`
|
|
Page int `json:"page"`
|
|
PageSize int `json:"page_size"`
|
|
TotalPages int `json:"total_pages"`
|
|
}
|
|
|
|
// JSON 统一 JSON 响应
|
|
func JSON(c *gin.Context, httpCode int, success bool, code int, message string, data interface{}) {
|
|
c.JSON(httpCode, Response{
|
|
Success: success,
|
|
Code: code,
|
|
Message: message,
|
|
Data: data,
|
|
})
|
|
}
|
|
|
|
// Success 成功响应 (200)
|
|
func Success(c *gin.Context, data interface{}, message string) {
|
|
JSON(c, http.StatusOK, true, 200, message, data)
|
|
}
|
|
|
|
// SuccessWithMessage 成功响应(仅消息)
|
|
func SuccessWithMessage(c *gin.Context, message string) {
|
|
JSON(c, http.StatusOK, true, 200, message, nil)
|
|
}
|
|
|
|
// Created 创建成功响应 (201)
|
|
func Created(c *gin.Context, data interface{}, message string) {
|
|
JSON(c, http.StatusCreated, true, 201, message, data)
|
|
}
|
|
|
|
// BadRequest 参数错误 (400)
|
|
func BadRequest(c *gin.Context, message string) {
|
|
JSON(c, http.StatusBadRequest, false, 400, message, nil)
|
|
}
|
|
|
|
// Unauthorized 未授权 (401)
|
|
func Unauthorized(c *gin.Context, message string) {
|
|
JSON(c, http.StatusUnauthorized, false, 401, message, nil)
|
|
}
|
|
|
|
// Forbidden 禁止访问 (403)
|
|
func Forbidden(c *gin.Context, message string) {
|
|
JSON(c, http.StatusForbidden, false, 403, message, nil)
|
|
}
|
|
|
|
// NotFound 资源不存在 (404)
|
|
func NotFound(c *gin.Context, message string) {
|
|
JSON(c, http.StatusNotFound, false, 404, message, nil)
|
|
}
|
|
|
|
// Conflict 冲突 (409)
|
|
func Conflict(c *gin.Context, message string) {
|
|
JSON(c, http.StatusConflict, false, 409, message, nil)
|
|
}
|
|
|
|
// InternalError 服务器内部错误 (500)
|
|
func InternalError(c *gin.Context, message string) {
|
|
JSON(c, http.StatusInternalServerError, false, 500, message, nil)
|
|
}
|
|
|
|
// Paginated 分页成功响应
|
|
func Paginated(c *gin.Context, items interface{}, total int64, page, pageSize int) {
|
|
totalPages := int(total) / pageSize
|
|
if int(total)%pageSize > 0 {
|
|
totalPages++
|
|
}
|
|
|
|
Success(c, PageData{
|
|
Items: items,
|
|
Total: total,
|
|
Page: page,
|
|
PageSize: pageSize,
|
|
TotalPages: totalPages,
|
|
}, "操作成功")
|
|
}
|