2024-10-22 16:49:45 +08:00
|
|
|
// Package parse_body ...
|
|
|
|
//
|
|
|
|
// Description : parse_body ...
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 2024-10-22 16:43
|
|
|
|
package parse_body
|
2024-10-22 17:20:30 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"git.zhangdeman.cn/zhangdeman/consts"
|
|
|
|
"git.zhangdeman.cn/zhangdeman/serialize"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
)
|
|
|
|
|
|
|
|
type FormUrlEncode struct {
|
|
|
|
base
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f FormUrlEncode) Parse(ctx *gin.Context, receiver any) ([]byte, error) {
|
|
|
|
if err := ctx.Request.ParseForm(); nil != err {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
body := map[string]string{}
|
|
|
|
for paramName, itemParam := range ctx.Request.PostForm {
|
|
|
|
if len(itemParam) > 0 {
|
|
|
|
body[paramName] = itemParam[0]
|
|
|
|
} else {
|
|
|
|
body[paramName] = ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
byteData, _ := json.Marshal(body)
|
|
|
|
if nil != receiver {
|
|
|
|
if err := serialize.JSON.UnmarshalWithNumber(byteData, receiver); nil != receiver {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return byteData, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f FormUrlEncode) ContentType() string {
|
|
|
|
return consts.MimeTypeXWWWFormUrlencoded
|
|
|
|
}
|