实现swagger
This commit is contained in:
@@ -1,11 +1,14 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// --- 通用响应结构 ---
|
||||
|
||||
// Response 定义统一的API响应结构体
|
||||
type Response struct {
|
||||
Code int `json:"code"` // 业务状态码
|
||||
@@ -14,7 +17,6 @@ type Response struct {
|
||||
}
|
||||
|
||||
// SendResponse 发送统一格式的JSON响应
|
||||
// httpStatus 参数现在将几乎总是 http.StatusOK,业务状态码通过 Response.Code 传递
|
||||
func SendResponse(ctx *gin.Context, code int, message string, data interface{}) {
|
||||
ctx.JSON(http.StatusOK, Response{
|
||||
Code: code,
|
||||
@@ -24,7 +26,9 @@ func SendResponse(ctx *gin.Context, code int, message string, data interface{})
|
||||
}
|
||||
|
||||
// SendErrorResponse 发送统一格式的错误响应
|
||||
// 错误响应通常不包含业务数据,因此 data 参数固定为 nil
|
||||
func SendErrorResponse(ctx *gin.Context, code int, message string) {
|
||||
SendResponse(ctx, code, message, nil)
|
||||
}
|
||||
|
||||
// Properties 是一个自定义类型,用于在 Swagger 中正确表示 JSON 对象
|
||||
type Properties json.RawMessage
|
||||
|
||||
Reference in New Issue
Block a user