31 lines
879 B
Go
31 lines
879 B
Go
|
|
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)
|
|||
|
|
}
|