From 545c6ef6a42e0dbc2ff9925f07edcbf81c82b68c Mon Sep 17 00:00:00 2001 From: Jimmy Xue Date: Sun, 7 Dec 2025 01:09:52 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0getuint64/32=E7=9A=84?= =?UTF-8?q?=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/factory.md | 10 ++++++++++ factory/factory.go | 16 ++++++++++++++++ http/request.go | 28 ++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+) diff --git a/docs/factory.md b/docs/factory.md index 74acc83..0645384 100644 --- a/docs/factory.md +++ b/docs/factory.md @@ -288,6 +288,8 @@ fac.ParseJSON(r, &req) // 获取查询参数 id := fac.GetQueryInt64(r, "id", 0) +uid := fac.GetQueryUint64(r, "uid", 0) +userId := fac.GetQueryUint32(r, "user_id", 0) keyword := fac.GetQuery(r, "keyword", "") // 获取时区(需要配合middleware.Timezone使用) @@ -667,6 +669,14 @@ fac.Success(w, user, "获取成功") // 自定义消息 获取查询64位整数参数。 +#### GetQueryUint64(r *http.Request, key string, defaultValue uint64) uint64 + +获取查询64位无符号整数参数。 + +#### GetQueryUint32(r *http.Request, key string, defaultValue uint32) uint32 + +获取查询32位无符号整数参数。 + #### GetFormValue(r *http.Request, key, defaultValue string) string 获取表单值。 diff --git a/factory/factory.go b/factory/factory.go index e6338ef..7296768 100644 --- a/factory/factory.go +++ b/factory/factory.go @@ -984,6 +984,22 @@ func (f *Factory) GetQueryInt64(r *http.Request, key string, defaultValue int64) return commonhttp.GetQueryInt64(r, key, defaultValue) } +// GetQueryUint64 获取uint64查询参数(黑盒模式,推荐使用) +// r: HTTP请求 +// key: 参数名 +// defaultValue: 默认值 +func (f *Factory) GetQueryUint64(r *http.Request, key string, defaultValue uint64) uint64 { + return commonhttp.GetQueryUint64(r, key, defaultValue) +} + +// GetQueryUint32 获取uint32查询参数(黑盒模式,推荐使用) +// r: HTTP请求 +// key: 参数名 +// defaultValue: 默认值 +func (f *Factory) GetQueryUint32(r *http.Request, key string, defaultValue uint32) uint32 { + return commonhttp.GetQueryUint32(r, key, defaultValue) +} + // GetQueryBool 获取布尔查询参数(黑盒模式,推荐使用) // r: HTTP请求 // key: 参数名 diff --git a/http/request.go b/http/request.go index 39afe54..fd8fe57 100644 --- a/http/request.go +++ b/http/request.go @@ -223,6 +223,34 @@ func GetQueryFloat64(r *http.Request, key string, defaultValue float64) float64 return floatValue } +func GetQueryUint64(r *http.Request, key string, defaultValue uint64) uint64 { + value := r.URL.Query().Get(key) + if value == "" { + return defaultValue + } + + uintValue, err := strconv.ParseUint(value, 10, 64) + if err != nil { + return defaultValue + } + + return uintValue +} + +func GetQueryUint32(r *http.Request, key string, defaultValue uint32) uint32 { + value := r.URL.Query().Get(key) + if value == "" { + return defaultValue + } + + uintValue, err := strconv.ParseUint(value, 10, 32) + if err != nil { + return defaultValue + } + + return uint32(uintValue) +} + // GetFormValue 获取表单值(公共方法) // r: HTTP请求 // key: 参数名