From 0e14eb64d1a7fe448bb24fdcac2c283d717a2d38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Sun, 3 Jul 2022 00:56:37 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81gin=E8=AF=B7=E6=B1=82?= =?UTF-8?q?=E8=A1=A8=E5=8D=95=E8=A7=A3=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- request/content_type.go | 54 ++++++++++++++++++++++++++++++++++++++++ request/form.go | 55 +++++++++++++++++++++++++++++++++++++++++ request/init.go | 20 +++++++++++++++ 3 files changed, 129 insertions(+) create mode 100644 request/content_type.go create mode 100644 request/form.go create mode 100644 request/init.go diff --git a/request/content_type.go b/request/content_type.go new file mode 100644 index 0000000..8f10399 --- /dev/null +++ b/request/content_type.go @@ -0,0 +1,54 @@ +// Package request ... +// +// Description : request ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2022-07-03 00:52 +package request + +import ( + "strings" + + "github.com/gin-gonic/gin" +) + +// contentType 请求类型相关处理 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 00:53 2022/7/3 +type contentType struct { +} + +// IsJson 请求方式是否为JSON +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 00:35 2022/7/3 +func (ct *contentType) IsJson(ctx *gin.Context) bool { + return strings.Contains(ct.Get(ctx), "json") +} + +// IsFormURLEncoded 请求方式是否为 x-www-form-urlencoded +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 00:36 2022/7/3 +func (ct *contentType) IsFormURLEncoded(ctx *gin.Context) bool { + return strings.Contains(ct.Get(ctx), "x-www-form-urlencoded") +} + +// Get 获取请求类型 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 00:39 2022/7/3 +func (ct *contentType) Get(ctx *gin.Context) string { + contentType := strings.ToLower(ctx.ContentType()) + if len(contentType) == 0 { + // 请求不传type默认为 x-www-form-urlencoded + contentType = "application/x-www-form-urlencoded" + } + return contentType +} diff --git a/request/form.go b/request/form.go new file mode 100644 index 0000000..630b3dc --- /dev/null +++ b/request/form.go @@ -0,0 +1,55 @@ +// Package request ... +// +// Description : request ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2022-07-03 00:33 +package request + +import ( + "errors" + "net/http" + "strings" + + "github.com/gin-gonic/gin" +) + +// form 表单相关操作 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 00:51 2022/7/3 +type form struct { +} + +// ParseRequestForm 解析请求表单 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 00:34 2022/7/3 +func (f *form) ParseRequestForm(ctx *gin.Context, receiver interface{}) error { + method := strings.ToUpper(ctx.Request.Method) + if method == http.MethodGet || + method == http.MethodPatch || + method == http.MethodTrace || + method == http.MethodConnect || + method == http.MethodOptions { + return ctx.ShouldBindQuery(receiver) + } + + if method == http.MethodPost || + method == http.MethodPut || + method == http.MethodDelete { + if ContentType.IsJson(ctx) { + return ctx.ShouldBindJSON(receiver) + } + + if ContentType.IsFormURLEncoded(ctx) { + return ctx.ShouldBind(receiver) + } + + return errors.New(ctx.ContentType() + " is not support") + } + return errors.New(method + " : request method is not support") +} diff --git a/request/init.go b/request/init.go new file mode 100644 index 0000000..78b08c0 --- /dev/null +++ b/request/init.go @@ -0,0 +1,20 @@ +// Package request ... +// +// Description : request ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2022-07-03 00:52 +package request + +var ( + // Form 表单相关操作 + Form *form + // ContentType 请求类型 + ContentType *contentType +) + +func init() { + Form = &form{} + ContentType = &contentType{} +}