包装gin上下文数据获取

This commit is contained in:
白茶清欢 2023-12-29 15:29:37 +08:00
parent 782c3afffe
commit b7abf88670
2 changed files with 77 additions and 16 deletions

View File

@ -27,7 +27,7 @@ type contentType struct {
// //
// Date : 00:35 2022/7/3 // Date : 00:35 2022/7/3
func (ct *contentType) IsJson(ctx *gin.Context) bool { 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 // IsFormURLEncoded 请求方式是否为 x-www-form-urlencoded
@ -36,19 +36,5 @@ func (ct *contentType) IsJson(ctx *gin.Context) bool {
// //
// Date : 00:36 2022/7/3 // Date : 00:36 2022/7/3
func (ct *contentType) IsFormURLEncoded(ctx *gin.Context) bool { func (ct *contentType) IsFormURLEncoded(ctx *gin.Context) bool {
return strings.Contains(ct.Get(ctx), "x-www-form-urlencoded") return strings.Contains(WrapperHandle.GetContentType(ctx, "application/x-www-form-urlencoded"), "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
} }

75
request/wrapper.go Normal file
View File

@ -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()
}