123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- package app
- import (
- "github.com/gin-gonic/gin"
- "net/http"
- "zhiyuan/pkg/errcode"
- )
- type ResponseData struct {
- Code int `json:"code"`
- Msg string `json:"message"`
- Data interface{} `json:"data"`
- }
- func Response(c *gin.Context, httpCode int, err errcode.Err, data interface{}) {
- ResponseBase(c, httpCode, err.Code, err.Msg, data)
- }
- func ResponseBase(c *gin.Context, httpCode int, code int, msg string, data interface{}) {
- if data == nil {
- data = make(map[string]interface{})
- }
- c.JSON(httpCode, ResponseData{
- Code: code,
- Msg: msg,
- Data: data,
- })
- return
- }
- func Error(c *gin.Context, msg string) {
- ResponseBase(c, http.StatusOK, errcode.InvalidParams.Code, msg, nil)
- return
- }
- func ErrorMsg(c *gin.Context, msg string, data interface{}) {
- ResponseBase(c, http.StatusOK, errcode.InvalidParams.Code, msg, data)
- return
- }
- func ErrorCode(c *gin.Context, err errcode.Err, data interface{}) {
- Response(c, http.StatusOK, err, data)
- return
- }
- func SuccessMsg(c *gin.Context, msg string, data interface{}) {
- ResponseBase(c, http.StatusOK, errcode.Success.Code, msg, data)
- return
- }
- func Success(c *gin.Context, data interface{}) {
- Response(c, http.StatusOK, errcode.Success, data)
- return
- }
|