package response import ( "net/http" "github.com/gin-gonic/gin" ) type Response struct { Cmd int `json:"cmd"` // 命令 Data interface{} `json:"data"` // 数据 Time string `json:"time"` // 时间 MsgID string `json:"msg_id"` // 消息id Err int `json:"err"` // 请求状态 ErrMsg string `json:"err_msg"` // 错误信息 } type BaseResp struct { Cmd int `json:"cmd"` // 命令 Time string `json:"time"` MsgID string `json:"msg_id"` // 消息id } const ( ERROR = 7 API_ERROR = 1 //接口返回错误 SUCCESS = 0 ) func Result(code int, base BaseResp, data interface{}, msg string, c *gin.Context) { // 开始时间 c.JSON(http.StatusOK, Response{ base.Cmd, data, base.Time, base.MsgID, code, msg, }) } func Ok(c *gin.Context) { Result(SUCCESS, BaseResp{}, map[string]interface{}{}, "操作成功", c) } func OkWithMessage(message string, base BaseResp, c *gin.Context) { Result(SUCCESS, base, map[string]interface{}{}, message, c) } func OkWithDataMessage(data interface{}, base BaseResp, message string, c *gin.Context) { Result(SUCCESS, base, data, message, c) } func OkWithData(data interface{}, base BaseResp, c *gin.Context) { Result(SUCCESS, base, data, "操作成功", c) } func OkWithDetailed(data interface{}, base BaseResp, message string, c *gin.Context) { Result(SUCCESS, base, data, message, c) } func Fail(base BaseResp, c *gin.Context) { Result(ERROR, base, map[string]interface{}{}, "操作失败", c) } func FailWithMessage(message string, base BaseResp, c *gin.Context) { Result(API_ERROR, base, map[string]interface{}{}, message, c) } func FailWithDetailed(data interface{}, base BaseResp, message string, c *gin.Context) { Result(ERROR, base, data, message, c) }