mirror of
https://gitee.com/johng/gf
synced 2026-06-07 10:22:11 +08:00
up
This commit is contained in:
@ -74,8 +74,8 @@ func (r *Request) WebSocket() (*WebSocket, error) {
|
||||
|
||||
// 获得指定名称的参数字符串(Router/GET/POST),同 GetRequestString
|
||||
// 这是常用方法的简化别名
|
||||
func (r *Request) Get(k string) string {
|
||||
return r.GetRequestString(k)
|
||||
func (r *Request) Get(key string, def ... string) string {
|
||||
return r.GetRequestString(key, def...)
|
||||
}
|
||||
|
||||
// 获取原始请求输入字符串,注意:只能获取一次,读完就没了
|
||||
@ -95,32 +95,32 @@ func (r *Request) GetJson() *gjson.Json {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Request) GetString(k string) string {
|
||||
return r.GetRequestString(k)
|
||||
func (r *Request) GetString(key string, def ... string) string {
|
||||
return r.GetRequestString(key, def...)
|
||||
}
|
||||
|
||||
func (r *Request) GetInt(k string) int {
|
||||
return r.GetRequestInt(k)
|
||||
func (r *Request) GetInt(key string, def ... int) int {
|
||||
return r.GetRequestInt(key, def...)
|
||||
}
|
||||
|
||||
func (r *Request) GetUint(k string) uint {
|
||||
return r.GetRequestUint(k)
|
||||
func (r *Request) GetUint(key string, def ... uint) uint {
|
||||
return r.GetRequestUint(key, def...)
|
||||
}
|
||||
|
||||
func (r *Request) GetFloat32(k string) float32 {
|
||||
return r.GetRequestFloat32(k)
|
||||
func (r *Request) GetFloat32(key string, def ... float32) float32 {
|
||||
return r.GetRequestFloat32(key, def...)
|
||||
}
|
||||
|
||||
func (r *Request) GetFloat64(k string) float64 {
|
||||
return r.GetRequestFloat64(k)
|
||||
func (r *Request) GetFloat64(key string, def ... float64) float64 {
|
||||
return r.GetRequestFloat64(key, def...)
|
||||
}
|
||||
|
||||
func (r *Request) GetArray(k string) []string {
|
||||
return r.GetRequestArray(k)
|
||||
func (r *Request) GetArray(key string, def ... []string) []string {
|
||||
return r.GetRequestArray(key, def...)
|
||||
}
|
||||
|
||||
func (r *Request) GetMap(defaultMap...map[string]string) map[string]string {
|
||||
return r.GetRequestMap(defaultMap...)
|
||||
func (r *Request) GetMap(def...map[string]string) map[string]string {
|
||||
return r.GetRequestMap(def...)
|
||||
}
|
||||
|
||||
// 将所有的request参数映射到struct属性上,参数object应当为一个struct对象的指针, mapping为非必需参数,自定义参数与属性的映射关系
|
||||
|
||||
@ -21,69 +21,109 @@ func (r *Request) initPost() {
|
||||
}
|
||||
|
||||
// 设置POST参数,仅在ghttp.Server内有效,**注意并发安全性**
|
||||
func (r *Request) SetPost(k string, v string) {
|
||||
func (r *Request) SetPost(key string, value string) {
|
||||
r.initPost()
|
||||
r.PostForm[k] = []string{v}
|
||||
r.PostForm[key] = []string{value}
|
||||
}
|
||||
|
||||
func (r *Request) AddPost(k string, v string) {
|
||||
func (r *Request) AddPost(key string, value string) {
|
||||
r.initPost()
|
||||
r.PostForm[k] = append(r.PostForm[k], v)
|
||||
r.PostForm[key] = append(r.PostForm[key], value)
|
||||
}
|
||||
|
||||
// 获得post参数
|
||||
func (r *Request) GetPost(k string) []string {
|
||||
func (r *Request) GetPost(key string, def...[]string) []string {
|
||||
r.initPost()
|
||||
if v, ok := r.PostForm[k]; ok {
|
||||
if v, ok := r.PostForm[key]; ok {
|
||||
return v
|
||||
}
|
||||
if len(def) > 0 {
|
||||
return def[0]
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Request) GetPostBool(k string) bool {
|
||||
return gconv.Bool(r.GetPostString(k))
|
||||
}
|
||||
|
||||
func (r *Request) GetPostInt(k string) int {
|
||||
return gconv.Int(r.GetPostString(k))
|
||||
}
|
||||
|
||||
func (r *Request) GetPostUint(k string) uint {
|
||||
return gconv.Uint(r.GetPostString(k))
|
||||
}
|
||||
|
||||
func (r *Request) GetPostFloat32(k string) float32 {
|
||||
return gconv.Float32(r.GetPostString(k))
|
||||
}
|
||||
|
||||
func (r *Request) GetPostFloat64(k string) float64 {
|
||||
return gconv.Float64(r.GetPostString(k))
|
||||
}
|
||||
|
||||
func (r *Request) GetPostString(k string) string {
|
||||
v := r.GetPost(k)
|
||||
if v == nil {
|
||||
return ""
|
||||
} else {
|
||||
return v[0]
|
||||
func (r *Request) GetPostString(key string, def ... string) string {
|
||||
value := r.GetPost(key)
|
||||
if value != nil && value[0] != "" {
|
||||
return value[0]
|
||||
}
|
||||
if len(def) > 0 {
|
||||
return def[0]
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (r *Request) GetPostArray(k string) []string {
|
||||
return r.GetPost(k)
|
||||
func (r *Request) GetPostBool(key string, def ... bool) bool {
|
||||
value := r.GetPostString(key)
|
||||
if value != "" {
|
||||
return gconv.Bool(value)
|
||||
}
|
||||
if len(def) > 0 {
|
||||
return def[0]
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (r *Request) GetPostInt(key string, def ... int) int {
|
||||
value := r.GetPostString(key)
|
||||
if value != "" {
|
||||
return gconv.Int(value[0])
|
||||
}
|
||||
if len(def) > 0 {
|
||||
return def[0]
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (r *Request) GetPostUint(key string, def ... uint) uint {
|
||||
value := r.GetPostString(key)
|
||||
if value != "" {
|
||||
return gconv.Uint(value[0])
|
||||
}
|
||||
if len(def) > 0 {
|
||||
return def[0]
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (r *Request) GetPostFloat32(key string, def ... float32) float32 {
|
||||
value := r.GetPostString(key)
|
||||
if value != "" {
|
||||
return gconv.Float32(value[0])
|
||||
}
|
||||
if len(def) > 0 {
|
||||
return def[0]
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (r *Request) GetPostFloat64(key string, def ... float64) float64 {
|
||||
value := r.GetPostString(key)
|
||||
if value != "" {
|
||||
return gconv.Float64(value[0])
|
||||
}
|
||||
if len(def) > 0 {
|
||||
return def[0]
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (r *Request) GetPostArray(key string, def ... []string) []string {
|
||||
return r.GetPost(key, def...)
|
||||
}
|
||||
|
||||
// 获取指定键名的关联数组,并且给定当指定键名不存在时的默认值
|
||||
// 需要注意的是,如果其中一个字段为数组形式,那么只会返回第一个元素,如果需要获取全部的元素,请使用GetPostArray获取特定字段内容
|
||||
func (r *Request) GetPostMap(defaultMap...map[string]string) map[string]string {
|
||||
func (r *Request) GetPostMap(def...map[string]string) map[string]string {
|
||||
r.initPost()
|
||||
m := make(map[string]string)
|
||||
if len(defaultMap) == 0 {
|
||||
if len(def) == 0 {
|
||||
for k, v := range r.PostForm {
|
||||
m[k] = v[0]
|
||||
}
|
||||
} else {
|
||||
for k, v := range defaultMap[0] {
|
||||
for k, v := range def[0] {
|
||||
if v2, ok := r.PostForm[k]; ok {
|
||||
m[k] = v2[0]
|
||||
} else {
|
||||
|
||||
@ -19,69 +19,109 @@ func (r *Request) initGet() {
|
||||
}
|
||||
|
||||
// 设置GET参数,仅在ghttp.Server内有效,**注意并发安全性**
|
||||
func (r *Request) SetQuery(k string, v string) {
|
||||
func (r *Request) SetQuery(key string, value string) {
|
||||
r.initGet()
|
||||
r.queryVars[k] = []string{v}
|
||||
r.queryVars[key] = []string{value}
|
||||
}
|
||||
|
||||
// 添加GET参数,构成[]string
|
||||
func (r *Request) AddQuery(k string, v string) {
|
||||
func (r *Request) AddQuery(key string, value string) {
|
||||
r.initGet()
|
||||
r.queryVars[k] = append(r.queryVars[k], v)
|
||||
r.queryVars[key] = append(r.queryVars[key], value)
|
||||
}
|
||||
|
||||
// 获得指定名称的get参数列表
|
||||
func (r *Request) GetQuery(k string) []string {
|
||||
func (r *Request) GetQuery(key string, def ... []string) []string {
|
||||
r.initGet()
|
||||
if v, ok := r.queryVars[k]; ok {
|
||||
if v, ok := r.queryVars[key]; ok {
|
||||
return v
|
||||
}
|
||||
if len(def) > 0 {
|
||||
return def[0]
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Request) GetQueryBool(k string) bool {
|
||||
return gconv.Bool(r.Get(k))
|
||||
}
|
||||
|
||||
func (r *Request) GetQueryInt(k string) int {
|
||||
return gconv.Int(r.Get(k))
|
||||
}
|
||||
|
||||
func (r *Request) GetQueryUint(k string) uint {
|
||||
return gconv.Uint(r.Get(k))
|
||||
}
|
||||
|
||||
func (r *Request) GetQueryFloat32(k string) float32 {
|
||||
return gconv.Float32(r.Get(k))
|
||||
}
|
||||
|
||||
func (r *Request) GetQueryFloat64(k string) float64 {
|
||||
return gconv.Float64(r.Get(k))
|
||||
}
|
||||
|
||||
func (r *Request) GetQueryString(k string) string {
|
||||
v := r.GetQuery(k)
|
||||
if v == nil {
|
||||
return ""
|
||||
} else {
|
||||
return v[0]
|
||||
func (r *Request) GetQueryString(key string, def ... string) string {
|
||||
value := r.GetQuery(key)
|
||||
if value != nil && value[0] != "" {
|
||||
return value[0]
|
||||
}
|
||||
if len(def) > 0 {
|
||||
return def[0]
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (r *Request) GetQueryArray(k string) []string {
|
||||
return r.GetQuery(k)
|
||||
func (r *Request) GetQueryBool(key string, def ... bool) bool {
|
||||
value := r.GetQueryString(key)
|
||||
if value != "" {
|
||||
return gconv.Bool(value)
|
||||
}
|
||||
if len(def) > 0 {
|
||||
return def[0]
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (r *Request) GetQueryInt(key string, def ... int) int {
|
||||
value := r.GetQueryString(key)
|
||||
if value != "" {
|
||||
return gconv.Int(value[0])
|
||||
}
|
||||
if len(def) > 0 {
|
||||
return def[0]
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (r *Request) GetQueryUint(key string, def ... uint) uint {
|
||||
value := r.GetQueryString(key)
|
||||
if value != "" {
|
||||
return gconv.Uint(value[0])
|
||||
}
|
||||
if len(def) > 0 {
|
||||
return def[0]
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (r *Request) GetQueryFloat32(key string, def ... float32) float32 {
|
||||
value := r.GetQueryString(key)
|
||||
if value != "" {
|
||||
return gconv.Float32(value[0])
|
||||
}
|
||||
if len(def) > 0 {
|
||||
return def[0]
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (r *Request) GetQueryFloat64(key string, def ... float64) float64 {
|
||||
value := r.GetQueryString(key)
|
||||
if value != "" {
|
||||
return gconv.Float64(value[0])
|
||||
}
|
||||
if len(def) > 0 {
|
||||
return def[0]
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (r *Request) GetQueryArray(key string, def ... []string) []string {
|
||||
return r.GetQuery(key, def...)
|
||||
}
|
||||
|
||||
// 获取指定键名的关联数组,并且给定当指定键名不存在时的默认值
|
||||
func (r *Request) GetQueryMap(defaultMap...map[string]string) map[string]string {
|
||||
func (r *Request) GetQueryMap(def ... map[string]string) map[string]string {
|
||||
r.initGet()
|
||||
m := make(map[string]string)
|
||||
if len(defaultMap) == 0 {
|
||||
if len(def) == 0 {
|
||||
for k, v := range r.queryVars {
|
||||
m[k] = v[0]
|
||||
}
|
||||
} else {
|
||||
for k, v := range defaultMap[0] {
|
||||
for k, v := range def[0] {
|
||||
v2 := r.GetQueryArray(k)
|
||||
if v2 == nil {
|
||||
m[k] = v
|
||||
|
||||
@ -11,62 +11,102 @@ import (
|
||||
)
|
||||
|
||||
// 获得router、post或者get提交的参数,如果有同名参数,那么按照router->get->post优先级进行覆盖
|
||||
func (r *Request) GetRequest(k string) []string {
|
||||
v := r.GetRouterArray(k)
|
||||
func (r *Request) GetRequest(key string, def ... []string) []string {
|
||||
v := r.GetRouterArray(key)
|
||||
if v == nil {
|
||||
v = r.GetQuery(k)
|
||||
v = r.GetQuery(key)
|
||||
}
|
||||
if v == nil {
|
||||
v = r.GetPost(k)
|
||||
v = r.GetPost(key)
|
||||
}
|
||||
if v == nil && len(def) > 0 {
|
||||
return def[0]
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
func (r *Request) GetRequestString(k string) string {
|
||||
v := r.GetRequest(k)
|
||||
if v == nil {
|
||||
return ""
|
||||
} else {
|
||||
return v[0]
|
||||
func (r *Request) GetRequestString(key string, def ... string) string {
|
||||
value := r.GetRequest(key)
|
||||
if value != nil && value[0] != "" {
|
||||
return value[0]
|
||||
}
|
||||
if len(def) > 0 {
|
||||
return def[0]
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (r *Request) GetRequestBool(k string) bool {
|
||||
return gconv.Bool(r.GetRequestString(k))
|
||||
func (r *Request) GetRequestBool(key string, def ... bool) bool {
|
||||
value := r.GetRequestString(key)
|
||||
if value != "" {
|
||||
return gconv.Bool(value[0])
|
||||
}
|
||||
if len(def) > 0 {
|
||||
return def[0]
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (r *Request) GetRequestInt(k string) int {
|
||||
return gconv.Int(r.GetRequestString(k))
|
||||
func (r *Request) GetRequestInt(key string, def ... int) int {
|
||||
value := r.GetRequestString(key)
|
||||
if value != "" {
|
||||
return gconv.Int(value[0])
|
||||
}
|
||||
if len(def) > 0 {
|
||||
return def[0]
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (r *Request) GetRequestUint(k string) uint {
|
||||
return gconv.Uint(r.GetRequestString(k))
|
||||
func (r *Request) GetRequestUint(key string, def ... uint) uint {
|
||||
value := r.GetRequestString(key)
|
||||
if value != "" {
|
||||
return gconv.Uint(value[0])
|
||||
}
|
||||
if len(def) > 0 {
|
||||
return def[0]
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (r *Request) GetRequestFloat32(k string) float32 {
|
||||
return gconv.Float32(r.GetRequestString(k))
|
||||
func (r *Request) GetRequestFloat32(key string, def ... float32) float32 {
|
||||
value := r.GetRequestString(key)
|
||||
if value != "" {
|
||||
return gconv.Float32(value[0])
|
||||
}
|
||||
if len(def) > 0 {
|
||||
return def[0]
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (r *Request) GetRequestFloat64(k string) float64 {
|
||||
return gconv.Float64(r.GetRequestString(k))
|
||||
func (r *Request) GetRequestFloat64(key string, def ... float64) float64 {
|
||||
value := r.GetRequestString(key)
|
||||
if value != "" {
|
||||
return gconv.Float64(value[0])
|
||||
}
|
||||
if len(def) > 0 {
|
||||
return def[0]
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (r *Request) GetRequestArray(k string) []string {
|
||||
return r.GetRequest(k)
|
||||
func (r *Request) GetRequestArray(key string, def ... []string) []string {
|
||||
return r.GetRequest(key, def...)
|
||||
}
|
||||
|
||||
// 获取指定键名的关联数组,并且给定当指定键名不存在时的默认值
|
||||
// 需要注意的是,如果其中一个字段为数组形式,那么只会返回第一个元素,如果需要获取全部的元素,请使用GetRequestArray获取特定字段内容
|
||||
func (r *Request) GetRequestMap(defaultMap...map[string]string) map[string]string {
|
||||
func (r *Request) GetRequestMap(def...map[string]string) map[string]string {
|
||||
m := r.GetQueryMap()
|
||||
if len(defaultMap) == 0 {
|
||||
if len(def) == 0 {
|
||||
for k, v := range r.GetPostMap() {
|
||||
if _, ok := m[k]; !ok {
|
||||
m[k] = v
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for k, v := range defaultMap[0] {
|
||||
for k, v := range def[0] {
|
||||
v2 := r.GetRequest(k)
|
||||
if v2 != nil {
|
||||
m[k] = v2[0]
|
||||
|
||||
@ -6,25 +6,25 @@
|
||||
|
||||
package ghttp
|
||||
|
||||
func (r *Request) SetRouterString(k, v string) {
|
||||
r.routerVars[k] = []string{v}
|
||||
func (r *Request) SetRouterString(key, value string) {
|
||||
r.routerVars[key] = []string{value}
|
||||
}
|
||||
|
||||
func (r *Request) AddRouterString(k, v string) {
|
||||
r.routerVars[k] = append(r.routerVars[k], v)
|
||||
func (r *Request) AddRouterString(key, value string) {
|
||||
r.routerVars[key] = append(r.routerVars[key], value)
|
||||
}
|
||||
|
||||
// 获得路由解析参数
|
||||
func (r *Request) GetRouterString(k string) string {
|
||||
if v := r.GetRouterArray(k); v != nil {
|
||||
func (r *Request) GetRouterString(key string) string {
|
||||
if v := r.GetRouterArray(key); v != nil {
|
||||
return v[0]
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// 获得路由解析参数
|
||||
func (r *Request) GetRouterArray(k string) []string {
|
||||
if v, ok := r.routerVars[k]; ok {
|
||||
func (r *Request) GetRouterArray(key string) []string {
|
||||
if v, ok := r.routerVars[key]; ok {
|
||||
return v
|
||||
}
|
||||
return nil
|
||||
|
||||
Reference in New Issue
Block a user