package controller import ( "encoding/json" "net/http" "github.com/gin-gonic/gin" ) // --- 业务状态码 --- const ( // 成功状态码 (2000-2999) CodeSuccess = 2000 // 操作成功 CodeCreated = 2001 // 创建成功 // 客户端错误状态码 (4000-4999) CodeBadRequest = 4000 // 请求参数错误 CodeUnauthorized = 4001 // 未授权 CodeNotFound = 4004 // 资源未找到 CodeConflict = 4009 // 资源冲突 // 服务器错误状态码 (5000-5999) CodeInternalError = 5000 // 服务器内部错误 CodeServiceUnavailable = 5003 // 服务不可用 ) // --- 通用响应结构 --- // Response 定义统一的API响应结构体 type Response struct { Code int `json:"code"` // 业务状态码 Message string `json:"message"` // 提示信息 Data interface{} `json:"data"` // 业务数据 } // SendResponse 发送统一格式的JSON响应 func SendResponse(ctx *gin.Context, code int, message string, data interface{}) { ctx.JSON(http.StatusOK, Response{ Code: code, Message: message, Data: data, }) } // SendErrorResponse 发送统一格式的错误响应 func SendErrorResponse(ctx *gin.Context, code int, message string) { SendResponse(ctx, code, message, nil) } // Properties 是一个自定义类型,用于在 Swagger 中正确表示 JSON 对象 type Properties json.RawMessage