gf/net/ghttp/ghttp_request_query.go

143 lines
3.8 KiB
Go
Raw Normal View History

// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
2018-07-29 22:01:29 +08:00
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
2018-07-29 22:01:29 +08:00
package ghttp
import (
2019-06-19 09:06:52 +08:00
"strings"
"github.com/gogf/gf/text/gstr"
2019-07-29 21:01:19 +08:00
"github.com/gogf/gf/container/gvar"
2019-07-29 21:01:19 +08:00
"github.com/gogf/gf/internal/structs"
"github.com/gogf/gf/util/gconv"
2018-07-29 22:01:29 +08:00
)
// 初始化GET请求参数
func (r *Request) initGet() {
2019-06-19 09:06:52 +08:00
if !r.parsedGet {
r.parsedGet = true
if r.URL.RawQuery != "" {
r.getMap, _ = gstr.Parse(r.URL.RawQuery)
} else if strings.EqualFold(r.Method, "GET") {
r.parsedRaw = true
2019-06-19 09:06:52 +08:00
if raw := r.GetRawString(); len(raw) > 0 {
r.getMap, _ = gstr.Parse(raw)
2019-06-19 09:06:52 +08:00
}
}
}
2018-07-29 22:01:29 +08:00
}
// 设置当前请求的GET参数
func (r *Request) SetQuery(key string, value interface{}) {
2019-06-19 09:06:52 +08:00
r.initGet()
r.getMap[key] = value
2018-07-31 14:28:41 +08:00
}
2018-07-29 22:01:29 +08:00
// 获得指定名称的get参数列表
func (r *Request) GetQuery(key string, def ...interface{}) interface{} {
2019-06-19 09:06:52 +08:00
r.initGet()
if v, ok := r.getMap[key]; ok {
2019-06-19 09:06:52 +08:00
return v
}
if len(def) > 0 {
return def[0]
2019-06-19 09:06:52 +08:00
}
return nil
2018-07-29 22:01:29 +08:00
}
func (r *Request) GetQueryVar(key string, def ...interface{}) *gvar.Var {
return gvar.New(r.GetQuery(key, def...))
}
2019-06-19 09:06:52 +08:00
func (r *Request) GetQueryString(key string, def ...interface{}) string {
return r.GetQueryVar(key, def...).String()
2018-07-29 22:01:29 +08:00
}
2019-06-19 09:06:52 +08:00
func (r *Request) GetQueryBool(key string, def ...interface{}) bool {
return r.GetQueryVar(key, def...).Bool()
2018-07-29 22:01:29 +08:00
}
2019-06-19 09:06:52 +08:00
func (r *Request) GetQueryInt(key string, def ...interface{}) int {
return r.GetQueryVar(key, def...).Int()
2018-07-29 22:01:29 +08:00
}
2019-06-19 09:06:52 +08:00
func (r *Request) GetQueryInts(key string, def ...interface{}) []int {
return r.GetQueryVar(key, def...).Ints()
}
2019-06-19 09:06:52 +08:00
func (r *Request) GetQueryUint(key string, def ...interface{}) uint {
return r.GetQueryVar(key, def...).Uint()
2018-07-29 22:01:29 +08:00
}
2019-06-19 09:06:52 +08:00
func (r *Request) GetQueryFloat32(key string, def ...interface{}) float32 {
return r.GetQueryVar(key, def...).Float32()
2018-07-29 22:01:29 +08:00
}
2019-06-19 09:06:52 +08:00
func (r *Request) GetQueryFloat64(key string, def ...interface{}) float64 {
return r.GetQueryVar(key, def...).Float64()
2018-07-29 22:01:29 +08:00
}
2019-06-19 09:06:52 +08:00
func (r *Request) GetQueryFloats(key string, def ...interface{}) []float64 {
return r.GetQueryVar(key, def...).Floats()
}
2019-06-19 09:06:52 +08:00
func (r *Request) GetQueryArray(key string, def ...interface{}) []string {
return r.GetQueryVar(key, def...).Strings()
2018-07-29 22:01:29 +08:00
}
2019-06-19 09:06:52 +08:00
func (r *Request) GetQueryStrings(key string, def ...interface{}) []string {
return r.GetQueryVar(key, def...).Strings()
}
2019-06-19 09:06:52 +08:00
func (r *Request) GetQueryInterfaces(key string, def ...interface{}) []interface{} {
return r.GetQueryVar(key, def...).Interfaces()
}
// 获取指定键名的关联数组,并且给定当指定键名不存在时的默认值。
// 当不指定键值对关联数组时默认获取GET方式提交的所有的提交键值对数据。
func (r *Request) GetQueryMap(kvMap ...map[string]interface{}) map[string]interface{} {
2019-06-19 09:06:52 +08:00
r.initGet()
if len(kvMap) > 0 {
m := make(map[string]interface{})
for k, defValue := range kvMap[0] {
if queryValue, ok := r.getMap[k]; ok {
m[k] = queryValue
} else {
m[k] = defValue
2019-06-19 09:06:52 +08:00
}
}
return m
} else {
return r.getMap
2019-06-19 09:06:52 +08:00
}
2018-07-29 22:01:29 +08:00
}
func (r *Request) GetQueryMapStrStr(kvMap ...map[string]interface{}) map[string]string {
queryMap := r.GetQueryMap(kvMap...)
if len(queryMap) > 0 {
m := make(map[string]string)
for k, v := range queryMap {
m[k] = gconv.String(v)
}
return m
}
return nil
}
// 将所有的get参数映射到struct属性上参数object应当为一个struct对象的指针, mapping为非必需参数自定义参数与属性的映射关系
2019-06-19 09:06:52 +08:00
func (r *Request) GetQueryToStruct(pointer interface{}, mapping ...map[string]string) error {
r.initGet()
tagMap := structs.TagMapName(pointer, paramTagPriority, true)
2019-06-19 09:06:52 +08:00
if len(mapping) > 0 {
for k, v := range mapping[0] {
tagMap[k] = v
2019-06-19 09:06:52 +08:00
}
}
return gconv.StructDeep(r.getMap, pointer, tagMap)
2019-06-19 09:06:52 +08:00
}