fix both modify
This commit is contained in:
@ -36,10 +36,10 @@ type form struct {
|
||||
//
|
||||
// Date : 00:34 2022/7/3
|
||||
func (f *form) Parse(ctx *gin.Context, receiver interface{}) error {
|
||||
|
||||
handleConfig := define.GetHttpHandleConfig()
|
||||
requestBody, _ := io.ReadAll(ctx.Request.Body)
|
||||
// 请求信息写入上下文
|
||||
ctx.Set(define.RecordRequestDataField, string(requestBody))
|
||||
ctx.Set(handleConfig.RecordRequestDataField, string(requestBody))
|
||||
// 因为请求体被读一遍之后就没了,重新赋值 requestBody
|
||||
ctx.Request.Body = io.NopCloser(bytes.NewReader(requestBody))
|
||||
method := strings.ToUpper(ctx.Request.Method)
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Package gin ...
|
||||
// Package request ...
|
||||
//
|
||||
// Description : gin ...
|
||||
//
|
||||
@ -8,9 +8,11 @@
|
||||
package request
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"git.zhangdeman.cn/zhangdeman/consts"
|
||||
"git.zhangdeman.cn/zhangdeman/wrapper"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/url"
|
||||
"io"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@ -79,11 +81,19 @@ func (wh *wrapperHandle) GetScheme(ctx *gin.Context, defaultVal string) string {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 16:03 2024/1/2
|
||||
func (wh *wrapperHandle) GetQuery(ctx *gin.Context) url.Values {
|
||||
func (wh *wrapperHandle) GetQuery(ctx *gin.Context) map[string]string {
|
||||
query := make(map[string]string)
|
||||
if nil != ctx && nil != ctx.Request && nil != ctx.Request.URL {
|
||||
return ctx.Request.URL.Query()
|
||||
for paramName, valueList := range ctx.Request.URL.Query() {
|
||||
if len(valueList) == 0 {
|
||||
query[paramName] = ""
|
||||
} else {
|
||||
query[paramName] = valueList[0]
|
||||
}
|
||||
}
|
||||
return query
|
||||
}
|
||||
return make(url.Values)
|
||||
return query
|
||||
}
|
||||
|
||||
// GetMethod 获取请求方法
|
||||
@ -111,11 +121,124 @@ func (wh *wrapperHandle) GetContentType(ctx *gin.Context, defaultVal string) str
|
||||
return wrapper.TernaryOperator.String(len(contentType) > 0, wrapper.String(contentType), wrapper.String(defaultVal)).Value()
|
||||
}
|
||||
|
||||
// GetRequestBody 获取请求body
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 11:25 2024/7/26
|
||||
func (wh *wrapperHandle) GetRequestBody(ctx *gin.Context) map[string]any {
|
||||
body := map[string]any{}
|
||||
contentType := wh.GetContentType(ctx, "")
|
||||
if strings.Contains(contentType, consts.MimeTypeXWWWFormUrlencoded) { // form请求
|
||||
if err := ctx.Request.ParseForm(); nil != err {
|
||||
return body
|
||||
}
|
||||
for paramName, itemParam := range ctx.Request.PostForm {
|
||||
if len(itemParam) > 0 {
|
||||
body[paramName] = itemParam[0]
|
||||
} else {
|
||||
body[paramName] = ""
|
||||
}
|
||||
}
|
||||
return body
|
||||
}
|
||||
|
||||
if strings.Contains(contentType, consts.MimeTypeJson) || nil != ctx.Request.Body { // json请求 或者非 json请求 存在 body
|
||||
data, err := io.ReadAll(ctx.Request.Body)
|
||||
if nil != err {
|
||||
return body
|
||||
}
|
||||
ctx.Request.Body = io.NopCloser(bytes.NewBuffer(data))
|
||||
return body
|
||||
}
|
||||
|
||||
return body
|
||||
}
|
||||
|
||||
// GetResponseBody 获取响应body
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 15:18 2024/1/2
|
||||
func (wh *wrapperHandle) GetResponseBody(ctx *gin.Context, defaultVal string) string {
|
||||
return ""
|
||||
func (wh *wrapperHandle) GetResponseBody(ctx *gin.Context, key string, defaultVal any) any {
|
||||
if nil == ctx {
|
||||
return defaultVal
|
||||
}
|
||||
if val, exist := ctx.Get(key); !exist || nil == val {
|
||||
return defaultVal
|
||||
} else {
|
||||
return val
|
||||
}
|
||||
}
|
||||
|
||||
// GetClientIp 获取请求客户端IP
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 16:31 2024/7/23
|
||||
func (wh *wrapperHandle) GetClientIp(ctx *gin.Context, defaultVal string) string {
|
||||
if nil == ctx {
|
||||
return defaultVal
|
||||
}
|
||||
return ctx.ClientIP()
|
||||
}
|
||||
|
||||
// GetUserAgent 获取user_agent
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 16:32 2024/7/23
|
||||
func (wh *wrapperHandle) GetUserAgent(ctx *gin.Context, defaultVal string) string {
|
||||
if nil == ctx {
|
||||
return defaultVal
|
||||
}
|
||||
return ctx.Request.UserAgent()
|
||||
}
|
||||
|
||||
// GetCtxData 获取请求上下文数据
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 16:35 2024/7/23
|
||||
func (wh *wrapperHandle) GetCtxData(ctx *gin.Context, key string, defaultVal any) any {
|
||||
if nil == ctx {
|
||||
return defaultVal
|
||||
}
|
||||
if val, exist := ctx.Get(key); !exist || nil == val {
|
||||
return defaultVal
|
||||
} else {
|
||||
return val
|
||||
}
|
||||
}
|
||||
|
||||
// GetCtxStringData 获取字符串数据
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 16:37 2024/7/23
|
||||
func (wh *wrapperHandle) GetCtxStringData(ctx *gin.Context, key string, defaultVal string) string {
|
||||
if nil == ctx {
|
||||
return defaultVal
|
||||
}
|
||||
val := ctx.GetString(key)
|
||||
if len(val) == 0 {
|
||||
return defaultVal
|
||||
}
|
||||
return val
|
||||
}
|
||||
|
||||
// GetCtxIntData 获取int数据
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 16:39 2024/7/23
|
||||
func (wh *wrapperHandle) GetCtxIntData(ctx *gin.Context, key string, defaultVal int64) int64 {
|
||||
if nil == ctx {
|
||||
return defaultVal
|
||||
}
|
||||
val := wh.GetCtxData(ctx, key, nil)
|
||||
if nil == val {
|
||||
return defaultVal
|
||||
}
|
||||
return ctx.GetInt64(key)
|
||||
}
|
||||
|
Reference in New Issue
Block a user