Files
pig-farm-controller/internal/app/controller/response.go
huang 196e63f40d 1. 调整目录结构
2. 实现user_controller
2025-09-11 23:48:06 +08:00

31 lines
879 B
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package controller
import (
"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响应
// httpStatus 参数现在将几乎总是 http.StatusOK业务状态码通过 Response.Code 传递
func SendResponse(ctx *gin.Context, code int, message string, data interface{}) {
ctx.JSON(http.StatusOK, Response{
Code: code,
Message: message,
Data: data,
})
}
// SendErrorResponse 发送统一格式的错误响应
// 错误响应通常不包含业务数据,因此 data 参数固定为 nil
func SendErrorResponse(ctx *gin.Context, code int, message string) {
SendResponse(ctx, code, message, nil)
}