升级参数解析

This commit is contained in:
白茶清欢 2021-09-15 21:29:47 +08:00
parent a6a12b1b55
commit d261c4eb1a

View File

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