package response import ( "net/http" "github.com/gin-gonic/gin" ) // Response 统一响应结构(与 kra 保持一致) type Response struct { Code int `json:"code"` Data interface{} `json:"data"` Msg string `json:"msg"` } const ( ERROR = 7 SUCCESS = 0 ) // Result 返回结果 func Result(code int, data interface{}, msg string, c *gin.Context) { c.JSON(http.StatusOK, Response{ code, data, msg, }) } // Ok 成功响应 func Ok(c *gin.Context) { Result(SUCCESS, map[string]interface{}{}, "操作成功", c) } // OkWithMessage 成功响应带消息 func OkWithMessage(message string, c *gin.Context) { Result(SUCCESS, map[string]interface{}{}, message, c) } // OkWithData 成功响应带数据 func OkWithData(data interface{}, c *gin.Context) { Result(SUCCESS, data, "成功", c) } // OkWithDetailed 成功响应带数据和消息 func OkWithDetailed(data interface{}, message string, c *gin.Context) { Result(SUCCESS, data, message, c) } // Fail 失败响应 func Fail(c *gin.Context) { Result(ERROR, map[string]interface{}{}, "操作失败", c) } // FailWithMessage 失败响应带消息 func FailWithMessage(message string, c *gin.Context) { Result(ERROR, map[string]interface{}{}, message, c) } // FailWithDetailed 失败响应带数据和消息 func FailWithDetailed(data interface{}, message string, c *gin.Context) { Result(ERROR, data, message, c) } // NoAuth 未授权响应 func NoAuth(message string, c *gin.Context) { c.JSON(http.StatusUnauthorized, Response{ 7, nil, message, }) } // PageResult 分页响应结构 type PageResult struct { List interface{} `json:"list"` Total int64 `json:"total"` Page int `json:"page"` PageSize int `json:"pageSize"` }