升级参数解析
This commit is contained in:
parent
a6a12b1b55
commit
d261c4eb1a
@ -15,6 +15,9 @@ import (
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"git.zhangdeman.cn/zhangdeman/gopkg/easymap"
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
@ -23,7 +26,7 @@ import (
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 8:52 下午 2021/9/14
|
||||
func ParseRequestParam(ctx *gin.Context) (map[string]interface{}, error) {
|
||||
func ParseRequestParam(ctx *gin.Context) (easymap.EasyMap, error) {
|
||||
switch ctx.Request.Method {
|
||||
case http.MethodPost:
|
||||
return ParsePostRequestBody(ctx)
|
||||
@ -75,8 +78,8 @@ func ParseGetRequestURI(url *url.URL) map[string][]string {
|
||||
// Author : zhangdeman001@ke.com<白茶清欢>
|
||||
//
|
||||
// Date : 9:07 下午 2021/7/24
|
||||
func ParseGetRequestBody(ctx *gin.Context) (map[string]interface{}, error) {
|
||||
result := make(map[string]interface{})
|
||||
func ParseGetRequestBody(ctx *gin.Context) (easymap.EasyMap, error) {
|
||||
result := easymap.NewNormal(true)
|
||||
urlRaw := strings.TrimLeft(ctx.Request.URL.RawQuery, "/")
|
||||
queryArr := strings.Split(urlRaw, "&")
|
||||
for _, itemPair := range queryArr {
|
||||
@ -85,9 +88,9 @@ func ParseGetRequestBody(ctx *gin.Context) (map[string]interface{}, error) {
|
||||
continue
|
||||
}
|
||||
if len(itemPairArr) == 1 {
|
||||
result[itemPairArr[0]] = ""
|
||||
result.Set(itemPairArr[0], "")
|
||||
} else {
|
||||
result[itemPairArr[0]] = strings.Join(itemPairArr[1:], "=")
|
||||
result.Set(itemPairArr[0], strings.Join(itemPairArr[1:], "="))
|
||||
}
|
||||
}
|
||||
return result, nil
|
||||
@ -98,7 +101,7 @@ func ParseGetRequestBody(ctx *gin.Context) (map[string]interface{}, error) {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 7:38 下午 2021/9/14
|
||||
func ParsePostRequestBody(ctx *gin.Context) (map[string]interface{}, error) {
|
||||
func ParsePostRequestBody(ctx *gin.Context) (easymap.EasyMap, error) {
|
||||
contentType := strings.ToLower(ctx.ContentType())
|
||||
if strings.Contains(contentType, "application/json") {
|
||||
// application/json 请求参数
|
||||
@ -114,7 +117,7 @@ func ParsePostRequestBody(ctx *gin.Context) (map[string]interface{}, error) {
|
||||
// multipart/form-data 请求方法
|
||||
return ParsePostRequestForFormData(ctx)
|
||||
}
|
||||
return make(map[string]interface{}), nil
|
||||
return easymap.NewNormal(true), errors.New("content-type : " + contentType + " is not support")
|
||||
}
|
||||
|
||||
// ParsePostRequestForApplicationJSON 解析application/json请求
|
||||
@ -122,17 +125,21 @@ func ParsePostRequestBody(ctx *gin.Context) (map[string]interface{}, error) {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 8:00 下午 2021/9/14
|
||||
func ParsePostRequestForApplicationJSON(ctx *gin.Context) (map[string]interface{}, error) {
|
||||
func ParsePostRequestForApplicationJSON(ctx *gin.Context) (easymap.EasyMap, error) {
|
||||
var (
|
||||
err error
|
||||
result map[string]interface{}
|
||||
)
|
||||
finalResult := easymap.NewNormal(true)
|
||||
decoder := json.NewDecoder(ctx.Request.Body)
|
||||
decoder.UseNumber()
|
||||
if err = decoder.Decode(&result); nil != err {
|
||||
return make(map[string]interface{}), err
|
||||
return finalResult, err
|
||||
}
|
||||
return result, nil
|
||||
for k, v := range result {
|
||||
finalResult.Set(k, v)
|
||||
}
|
||||
return finalResult, nil
|
||||
}
|
||||
|
||||
// ParsePostRequestForApplicationFormURLEncoded 解析 application/x-www-form-urlencoded 表单
|
||||
@ -140,15 +147,16 @@ func ParsePostRequestForApplicationJSON(ctx *gin.Context) (map[string]interface{
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 8:11 下午 2021/9/14
|
||||
func ParsePostRequestForApplicationFormURLEncoded(ctx *gin.Context) (map[string]interface{}, error) {
|
||||
func ParsePostRequestForApplicationFormURLEncoded(ctx *gin.Context) (easymap.EasyMap, error) {
|
||||
var (
|
||||
err error
|
||||
result map[string]interface{}
|
||||
result easymap.EasyMap
|
||||
byteData []byte
|
||||
)
|
||||
|
||||
result = easymap.NewNormal(true)
|
||||
if byteData, err = io.ReadAll(ctx.Request.Body); nil != err {
|
||||
return make(map[string]interface{}), err
|
||||
return result, err
|
||||
}
|
||||
|
||||
tmpResult := make(map[string][]string)
|
||||
@ -169,9 +177,8 @@ func ParsePostRequestForApplicationFormURLEncoded(ctx *gin.Context) (map[string]
|
||||
}
|
||||
}
|
||||
|
||||
result = make(map[string]interface{})
|
||||
for k, v := range tmpResult {
|
||||
result[k] = v
|
||||
result.Set(k, v)
|
||||
}
|
||||
|
||||
return result, err
|
||||
@ -182,20 +189,20 @@ func ParsePostRequestForApplicationFormURLEncoded(ctx *gin.Context) (map[string]
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 8:17 下午 2021/9/14
|
||||
func ParsePostRequestForFormData(ctx *gin.Context) (map[string]interface{}, error) {
|
||||
func ParsePostRequestForFormData(ctx *gin.Context) (easymap.EasyMap, error) {
|
||||
var (
|
||||
err error
|
||||
result map[string]interface{}
|
||||
result easymap.EasyMap
|
||||
tmp *multipart.Form
|
||||
)
|
||||
|
||||
result = easymap.NewNormal(true)
|
||||
if tmp, err = ctx.MultipartForm(); nil != err {
|
||||
return make(map[string]interface{}), err
|
||||
return result, err
|
||||
}
|
||||
|
||||
result = make(map[string]interface{})
|
||||
for k, v := range tmp.Value {
|
||||
result[k] = v
|
||||
result.Set(k, v)
|
||||
}
|
||||
|
||||
return result, nil
|
||||
|
Loading…
Reference in New Issue
Block a user