package gin import ( "context" "github.com/gin-gonic/gin" "github.com/gin-gonic/gin/binding" ) type Handler[ReqType any, RspType any] func(ctx context.Context, req ReqType) (rsp RspType, err error) type HandlerWithGinContext[ReqType any, RspType any] func(ctx context.Context, ginCtx *gin.Context, req ReqType) (rsp RspType, err error) type HandlerNoReqWithGinContext[RspType any] func(ctx context.Context, ginCtx *gin.Context) (rsp RspType, err error) type NoReqHandler[RspType any] func(ctx context.Context) (rsp RspType, err error) type NoRspHandler[ReqType any] func(ctx context.Context, req ReqType) (err error) type NoHandler func(ctx context.Context) error func Wrap[ReqType any, RspType any](f Handler[*ReqType, RspType]) gin.HandlerFunc { return func(c *gin.Context) { req := new(ReqType) b := binding.Default(c.Request.Method, c.ContentType()) err := c.ShouldBindWith(req, b) if err != nil { _ = c.Error(err) c.JSON(200, gin.H{"result": false, "code": 400, "msg": err.Error()}) return } rsp, err := f(c, req) if err != nil { _ = c.Error(err) c.JSON(200, gin.H{"result": false, "code": 400, "msg": err.Error()}) return } c.JSON(200, gin.H{"result": true, "msg": "ok", "data": rsp}) } } func WrapWithGinContext[ReqType any, RspType any](f HandlerWithGinContext[*ReqType, RspType]) gin.HandlerFunc { return func(c *gin.Context) { req := new(ReqType) b := binding.Default(c.Request.Method, c.ContentType()) err := c.ShouldBindWith(req, b) if err != nil { _ = c.Error(err) c.JSON(200, gin.H{"result": false, "code": 400, "msg": err.Error()}) return } rsp, err := f(c, c, req) if err != nil { _ = c.Error(err) c.JSON(200, gin.H{"result": false, "code": 400, "msg": err.Error()}) return } c.JSON(200, gin.H{"result": true, "msg": "ok", "data": rsp}) } } func CheckError(c *gin.Context, err error) bool { if err == nil { return true } c.JSON(200, gin.H{"result": false, "code": 400, "msg": err.Error()}) return false } func Bind(c *gin.Context, obj interface{}) error { b := binding.Default(c.Request.Method, c.ContentType()) return c.ShouldBindWith(obj, b) } func SendSuccess(c *gin.Context, code int, data ...interface{}) { dataLen := len(data) if dataLen > 1 { var items []interface{} items = append(items, data...) c.JSON(code, gin.H{"result": true, "msg": "ok", "data": items}) } else if dataLen == 1 { c.JSON(200, gin.H{"result": true, "msg": "ok", "data": data[0]}) } else { c.JSON(code, gin.H{"result": true, "msg": "ok"}) } } func WrapNoReq[RspType any](f NoReqHandler[RspType]) gin.HandlerFunc { return func(c *gin.Context) { rsp, err := f(c) if err != nil { _ = c.Error(err) c.JSON(200, gin.H{"result": false, "code": 400, "msg": err.Error()}) return } c.JSON(200, gin.H{"result": true, "msg": "ok", "data": rsp}) } } func WrapNoRsp[ReqType any](f NoRspHandler[*ReqType]) gin.HandlerFunc { return func(c *gin.Context) { req := new(ReqType) b := binding.Default(c.Request.Method, c.ContentType()) err := c.ShouldBindWith(req, b) if err != nil { _ = c.Error(err) c.JSON(200, gin.H{"result": false, "code": 400, "msg": err.Error()}) return } err = f(c, req) if err != nil { _ = c.Error(err) c.JSON(200, gin.H{"result": false, "code": 400, "msg": err.Error()}) return } c.JSON(200, gin.H{"result": true, "msg": "ok"}) } } func WrapNoReqWithGinContext[RspType any](f HandlerNoReqWithGinContext[RspType]) gin.HandlerFunc { return func(c *gin.Context) { rsp, err := f(c, c) if err != nil { _ = c.Error(err) c.JSON(200, gin.H{"result": false, "code": 400, "msg": err.Error()}) return } c.JSON(200, gin.H{"result": true, "msg": "ok", "data": rsp}) } } func ConvertBody(c *gin.Context, obj interface{}) error { b := binding.Default(c.Request.Method, c.ContentType()) return c.ShouldBindWith(obj, b) } func WrapNo(f NoHandler) gin.HandlerFunc { return func(c *gin.Context) { err := f(c) if err != nil { _ = c.Error(err) c.JSON(200, gin.H{"result": false, "code": 400, "msg": err.Error()}) return } c.JSON(200, gin.H{"result": true, "msg": "ok"}) } }