package utils import ( "fmt" "github.com/gin-gonic/gin" "net/http" "srm/utils/code" ) type Response struct { Code int `json:"code"` Msg string `json:"msg"` Data interface{} `json:"data"` } type ResponseList struct { Code int `json:"code"` Msg string `json:"msg"` Data interface{} `json:"data"` Total int `json:"total"` Page int `json:"page"` PageSize int `json:"pageSize"` } // ResponseFormat 返回数据格式化 func ResponseFormat(c *gin.Context, respStatus *code.Code, data interface{}) { if respStatus == nil { respStatus = code.RequestParamError } if respStatus.Status != http.StatusOK { c.JSON(http.StatusOK, Response{ respStatus.Status, fmt.Sprintf("%v", data), nil, }) } else { c.JSON(http.StatusOK, Response{ respStatus.Status, respStatus.Message, data, }) } } // ResponseFail 失败返回 func ResponseFail(c *gin.Context, respStatus *code.Code) { if respStatus == nil { respStatus = code.RequestParamError } c.JSON(http.StatusOK, Response{ respStatus.Status, respStatus.Message, nil, }) } // ResponseFormatList 返回包含总数的列表 func ResponseFormatList(c *gin.Context, respStatus *code.Code, data interface{}, count int) { if respStatus == nil { respStatus = code.RequestParamError } c.JSON(http.StatusOK, ResponseList{ Code: respStatus.Status, Msg: respStatus.Message, Data: data, Total: count, }) } func ResponseFormatListWithPage(c *gin.Context, respStatus *code.Code, data interface{}, count int, page, pageSize int) { if respStatus == nil { respStatus = code.RequestParamError } c.JSON(respStatus.Status, ResponseList{ Code: respStatus.Status, Msg: respStatus.Message, Data: data, Total: count, Page: page, PageSize: pageSize, }) }