feat ✨ 增加bind通用方法
This commit is contained in:
1 parent
13ff9d9e63
commit
b0225d9bab
1 file changed
+36
@@ -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