Files
pig-farm-controller/internal/app/controller/response.go
2025-09-12 17:43:42 +08:00

35 lines
869 B
Go

package controller
import (
"encoding/json"
"net/http"
"github.com/gin-gonic/gin"
)
// --- 通用响应结构 ---
// 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