package common import ( "fmt" "io" "net/http" "strings" "github.com/gin-gonic/gin" "github.com/go-playground/validator/v10" ) var _engine *gin.Engine type R struct { Code int `json:"code"` Msg string `json:"msg"` Data any `json:"data"` } func Server(debug bool) { if !debug { gin.SetMode(gin.ReleaseMode) } _engine = gin.New() _engine.Use(gin.Recovery()) } func GetEngine() *gin.Engine { return _engine } func SetWriter(w io.Writer) { gin.DefaultWriter = w gin.DefaultErrorWriter = w } func RouteGroup(path string) *gin.RouterGroup { return _engine.Group(path) } func ServerRun(port int32) { _engine.Run(fmt.Sprintf(":%d", port)) } func OK(c *gin.Context) { c.JSON(http.StatusOK, R{Code: http.StatusOK, Msg: "ok"}) c.Abort() } func OKNoData(c *gin.Context) { c.JSON(http.StatusNoContent, R{Code: http.StatusOK, Msg: "ok"}) c.Abort() } func OKWithData(c *gin.Context, obj interface{}) { c.JSON(http.StatusOK, R{Code: http.StatusOK, Msg: "ok", Data: obj}) c.Abort() } func FailWithCodeAndMsg(c *gin.Context, code int, message string) { c.JSON(http.StatusBadRequest, R{Code: code, Msg: message, Data: nil}) c.Abort() } func FailWithBusiError(c *gin.Context, busiError *BusiError) { c.JSON(http.StatusBadRequest, R{Code: busiError.Code, Msg: busiError.Msg, Data: nil}) c.Abort() } func FailWithBusiErrorWithHttpStatus(c *gin.Context, status int, busiError *BusiError) { c.JSON(status, R{Code: busiError.Code, Msg: busiError.Msg, Data: nil}) c.Abort() } func FailWithMsgWithHttpStatus(c *gin.Context, status int, msg string) { c.JSON(status, R{Code: status, Msg: msg, Data: nil}) c.Abort() } func FailWithBusiErrorByServer(c *gin.Context, busiError *BusiError) { c.JSON(http.StatusInternalServerError, R{Code: busiError.Code, Msg: busiError.Msg, Data: nil}) c.Abort() } func FailWithMsgByServer(c *gin.Context, message string) { c.JSON(http.StatusInternalServerError, R{Code: http.StatusInternalServerError, Msg: message, Data: nil}) c.Abort() } func FailWithMsgAndErrByServer(c *gin.Context, message string, err error) { c.JSON(http.StatusInternalServerError, R{Code: http.StatusInternalServerError, Msg: message, Data: err}) c.Abort() } func FailWithBindError(c *gin.Context, busiError *BusiError, err error) { var msg string if validationErrors, ok := err.(validator.ValidationErrors); ok { errorMessages := validationErrors.Translate(GetTrans()) messages := make([]string, 0) for _, m := range errorMessages { messages = append(messages, m) } msg = strings.Join(messages, "; ") } c.JSON(http.StatusBadRequest, R{Code: busiError.Code, Msg: msg, Data: msg}) c.Abort() } func FailWithBusiErrorAndData(c *gin.Context, busiError *BusiError, data any) { c.JSON(http.StatusBadRequest, R{Code: busiError.Code, Msg: busiError.Msg, Data: data}) c.Abort() } func FailWithError(c *gin.Context, err error) { c.JSON(http.StatusBadRequest, R{Code: http.StatusBadRequest, Msg: err.Error(), Data: nil}) c.Abort() } func FailWithMsg(c *gin.Context, message string) { c.JSON(http.StatusBadRequest, R{Code: http.StatusInternalServerError, Msg: message, Data: nil}) c.Abort() } func FailWithMsgAndError(c *gin.Context, msg string, err error) { c.JSON(http.StatusBadRequest, R{Code: http.StatusBadRequest, Msg: msg, Data: nil}) c.Abort() }