From d261c4eb1a101f3e74d871ec003dd109bfccb160 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Wed, 15 Sep 2021 21:29:47 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8D=87=E7=BA=A7=E5=8F=82=E6=95=B0=E8=A7=A3?= =?UTF-8?q?=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gin/request/util.go | 47 ++++++++++++++++++++++++++------------------- 1 file changed, 27 insertions(+), 20 deletions(-) diff --git a/gin/request/util.go b/gin/request/util.go index 48b5f02..d02a92f 100644 --- a/gin/request/util.go +++ b/gin/request/util.go @@ -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