From b7abf88670611f87d37abdec6d3c5a51f82b9c46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Fri, 29 Dec 2023 15:29:37 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8C=85=E8=A3=85gin=E4=B8=8A=E4=B8=8B?= =?UTF-8?q?=E6=96=87=E6=95=B0=E6=8D=AE=E8=8E=B7=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- request/content_type.go | 18 ++-------- request/wrapper.go | 75 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 16 deletions(-) create mode 100644 request/wrapper.go diff --git a/request/content_type.go b/request/content_type.go index 8f10399..774ba4f 100644 --- a/request/content_type.go +++ b/request/content_type.go @@ -27,7 +27,7 @@ type contentType struct { // // Date : 00:35 2022/7/3 func (ct *contentType) IsJson(ctx *gin.Context) bool { - return strings.Contains(ct.Get(ctx), "json") + return strings.Contains(WrapperHandle.GetContentType(ctx, "application/x-www-form-urlencoded"), "json") } // IsFormURLEncoded 请求方式是否为 x-www-form-urlencoded @@ -36,19 +36,5 @@ func (ct *contentType) IsJson(ctx *gin.Context) bool { // // 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 + return strings.Contains(WrapperHandle.GetContentType(ctx, "application/x-www-form-urlencoded"), "x-www-form-urlencoded") } diff --git a/request/wrapper.go b/request/wrapper.go new file mode 100644 index 0000000..6a05726 --- /dev/null +++ b/request/wrapper.go @@ -0,0 +1,75 @@ +// Package gin ... +// +// Description : gin ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2023-12-29 15:10 +package request + +import ( + "git.zhangdeman.cn/zhangdeman/wrapper" + "github.com/gin-gonic/gin" + "strings" +) + +var ( + WrapperHandle = &wrapperHandle{} +) + +type wrapperHandle struct { +} + +// GetHeader 读取header +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 21:22 2023/12/25 +func (wh *wrapperHandle) GetHeader(ctx *gin.Context, headerKey string, defaultVal string) string { + if nil == ctx { + return defaultVal + } + val := ctx.GetHeader(headerKey) + return wrapper.TernaryOperator.String(len(val) > 0, wrapper.String(val), wrapper.String(defaultVal)).Value() +} + +// GetCookie 读取cookie +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 21:23 2023/12/25 +func (wh *wrapperHandle) GetCookie(ctx *gin.Context, cookieName string, defaultVal string) string { + if nil == ctx { + return "" + } + val, err := ctx.Cookie(cookieName) + if nil != err { + return defaultVal + } + return wrapper.TernaryOperator.String(len(val) > 0, wrapper.String(val), wrapper.String(defaultVal)).Value() +} + +// GetUri 获取请求URI +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 15:13 2023/12/29 +func (wh *wrapperHandle) GetUri(ctx *gin.Context, defaultVal string) string { + if nil != ctx && nil != ctx.Request && nil != ctx.Request.URL { + return ctx.Request.URL.Path + } + return defaultVal +} + +// GetContentType 获取请求方式 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 15:24 2023/12/29 +func (wh *wrapperHandle) GetContentType(ctx *gin.Context, defaultVal string) string { + if nil == ctx { + return defaultVal + } + contentType := strings.ToLower(ctx.ContentType()) + return wrapper.TernaryOperator.String(len(contentType) > 0, wrapper.String(contentType), wrapper.String(defaultVal)).Value() +}