// Package resp 统一 JSON 响应与错误格式。 package resp import ( "net/http" "github.com/gin-gonic/gin" ) type Body struct { Data any `json:"data,omitempty"` Error *Error `json:"error,omitempty"` } type Error struct { Message string `json:"message"` Type string `json:"type,omitempty"` Code string `json:"code,omitempty"` } // OK 200 func OK(c *gin.Context, data any) { c.JSON(http.StatusOK, Body{Data: data}) } // Created 201 func Created(c *gin.Context, data any) { c.JSON(http.StatusCreated, Body{Data: data}) } // Fail 业务错误(message 会展示给用户) func Fail(c *gin.Context, status int, message string) { c.JSON(status, Body{Error: &Error{Message: message}}) } // FailCode 带错误码的业务错误 func FailCode(c *gin.Context, status int, code, message string) { c.JSON(status, Body{Error: &Error{Message: message, Type: code}}) }