mirror of
https://github.com/go-admin-team/go-admin.git
synced 2026-09-21 18:20:50 +00:00
feat ✨ 增加bind通用方法
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package apis
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin/binding"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -49,3 +50,38 @@ func (e Api) PageOK(result interface{}, count int, pageIndex int, pageSize int,
|
||||
func (e Api) Custom(data gin.H) {
|
||||
response.Custum(e.Context, data)
|
||||
}
|
||||
|
||||
// Bind 绑定参数,⚠️注意:uri绑定,请传nil
|
||||
func (e Api) Bind(c interface{}, bindings ...binding.Binding) error {
|
||||
var err error
|
||||
for i := range bindings {
|
||||
switch bindings[i] {
|
||||
case binding.JSON:
|
||||
err = e.Context.ShouldBindWith(c, binding.JSON)
|
||||
case binding.XML:
|
||||
err = e.Context.ShouldBindWith(c, binding.XML)
|
||||
case binding.Form:
|
||||
err = e.Context.ShouldBindWith(c, binding.Form)
|
||||
case binding.Query:
|
||||
err = e.Context.ShouldBindWith(c, binding.Query)
|
||||
case binding.FormPost:
|
||||
err = e.Context.ShouldBindWith(c, binding.FormPost)
|
||||
case binding.FormMultipart:
|
||||
err = e.Context.ShouldBindWith(c, binding.FormMultipart)
|
||||
case binding.ProtoBuf:
|
||||
err = e.Context.ShouldBindWith(c, binding.ProtoBuf)
|
||||
case binding.MsgPack:
|
||||
err = e.Context.ShouldBindWith(c, binding.MsgPack)
|
||||
case binding.YAML:
|
||||
err = e.Context.ShouldBindWith(c, binding.YAML)
|
||||
case binding.Header:
|
||||
err = e.Context.ShouldBindWith(c, binding.Header)
|
||||
default:
|
||||
err = e.Context.ShouldBindUri(c)
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user