package webutils import ( "net/http" "github.com/gin-gonic/gin" ut "github.com/go-playground/universal-translator" "github.com/sirupsen/logrus" ) const ( CodeSuccess = 20000 CodeFailed = -1 CodeIllegalToken = 50008 CodeTokenExpired = 50014 CodeOther = 50012 ) var ( trans ut.Translator ) type RespError struct { msg string } func NewRespError(msg string) RespError { return RespError{ msg: msg, } } func (p *RespError) Error() string { return "" } type Response struct { Code int `json:"code"` Msg string `json:"msg"` Data interface{} `json:"data"` } type ResponseGen[T interface{}] struct { Code int `json:"code"` Msg string `json:"msg"` Data T `json:"data"` } func FailedResponse(c *gin.Context, msg string) { if c == nil { return } c.JSON(http.StatusOK, &Response{ Code: CodeFailed, Msg: msg, Data: nil, }) panic(kkerror.NewRespError("failed")) } func FailedResponseErr(c *gin.Context, err error) { if c == nil { return } logrus.Error(err) FailedResponse(c, "内部错误") } func FailedResponseWithCode(c *gin.Context, code int, msg string) { c.JSON(http.StatusOK, &Response{ Code: code, Msg: msg, Data: nil, }) panic(kkerror.NewRespError("failed")) } func SuccessResponse(c *gin.Context, data interface{}) { c.JSON(http.StatusOK, &Response{ Code: CodeSuccess, Msg: "", Data: data, }) panic(kkerror.NewRespError("success")) }