package entity import ( "github.com/gin-gonic/gin" "net/http" ) /* { "code": 10000, // 程序中的错误码 "msg": xx, // 提示信息 "data": {}, // 数据 } */ type ResponseData struct { Code ResCode `json:"code"` Msg interface{} `json:"msg"` Body interface{} `json:"data,omitempty"` } type ResponseBodyData struct { Code ResCode `json:"code"` Msg interface{} `json:"msg"` Body interface{} `json:"body,omitempty"` } type ResponseNormalBody struct { Code int `json:"code"` Msg interface{} `json:"msg"` Body interface{} `json:"body,omitempty"` } type ResponseNormalData struct { Code int `json:"code"` Msg interface{} `json:"msg"` Body interface{} `json:"data,omitempty"` } // Response error func ResponseError(c *gin.Context, code ResCode) { c.JSON(http.StatusOK, &ResponseData{ Code: code, Msg: code.Msg(), Body: nil, }) } // Response error with msg func ResponseErrorWithMsg(c *gin.Context, code ResCode, msg interface{}) { c.JSON(http.StatusOK, &ResponseData{ Code: code, Msg: msg, Body: nil, }) } // Response success func ResponseSuccess(c *gin.Context, data interface{}) { c.JSON(http.StatusOK, &ResponseData{ Code: CodeSuccess, Msg: CodeSuccess.Msg(), Body: data, }) } // Response {code, msg, body} func ResponseNormal(c *gin.Context, code ResCode, msg interface{}, body interface{}) { c.JSON(http.StatusOK, &ResponseData{ Code: code, Msg: msg, Body: body, }) } func ResponseBody(c *gin.Context, code ResCode, msg interface{}, body interface{}) { c.JSON(http.StatusOK, &ResponseBodyData{ Code: code, Msg: msg, Body: body, }) } func ResponseHandleBody(c *gin.Context, code int, msg interface{}, body interface{}) { c.JSON(http.StatusOK, &ResponseNormalBody{ Code: code, Msg: msg, Body: body, }) } func EmptyBodyObject() map[string]interface{} { return map[string]interface{}{} } func EmptyBodyArray() []interface{} { return []interface{}{} }