gf/g/net/ghttp/ghttp_request_query.go

149 lines
3.6 KiB
Go
Raw Normal View History

2018-07-29 22:01:29 +08:00
// Copyright 2017 gf Author(https://gitee.com/johng/gf). All Rights Reserved.
//
// 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://gitee.com/johng/gf.
package ghttp
import (
"gitee.com/johng/gf/g/util/gconv"
)
// 初始化GET请求参数
func (r *Request) initGet() {
if !r.parsedGet.Val() {
r.parsedGet.Set(true)
r.queryVars = r.URL.Query()
2018-07-29 22:01:29 +08:00
}
}
2018-07-31 14:28:41 +08:00
// 设置GET参数仅在ghttp.Server内有效**注意并发安全性**
2018-08-19 22:45:23 +08:00
func (r *Request) SetQuery(key string, value string) {
r.initGet()
2018-08-19 22:45:23 +08:00
r.queryVars[key] = []string{value}
2018-07-31 14:28:41 +08:00
}
// 添加GET参数构成[]string
2018-08-19 22:45:23 +08:00
func (r *Request) AddQuery(key string, value string) {
r.initGet()
2018-08-19 22:45:23 +08:00
r.queryVars[key] = append(r.queryVars[key], value)
2018-07-31 14:28:41 +08:00
}
2018-07-29 22:01:29 +08:00
// 获得指定名称的get参数列表
2018-08-19 22:45:23 +08:00
func (r *Request) GetQuery(key string, def ... []string) []string {
2018-07-29 22:01:29 +08:00
r.initGet()
2018-08-19 22:45:23 +08:00
if v, ok := r.queryVars[key]; ok {
2018-07-29 22:01:29 +08:00
return v
}
2018-08-19 22:45:23 +08:00
if len(def) > 0 {
return def[0]
}
2018-07-29 22:01:29 +08:00
return nil
}
2018-08-19 22:45:23 +08:00
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 ""
2018-07-29 22:01:29 +08:00
}
2018-08-19 22:45:23 +08:00
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
2018-07-29 22:01:29 +08:00
}
2018-08-19 22:45:23 +08:00
func (r *Request) GetQueryInt(key string, def ... int) int {
value := r.GetQueryString(key)
if value != "" {
2018-08-23 10:24:14 +08:00
return gconv.Int(value)
2018-08-19 22:45:23 +08:00
}
if len(def) > 0 {
return def[0]
}
return 0
2018-07-29 22:01:29 +08:00
}
2018-08-19 22:45:23 +08:00
func (r *Request) GetQueryUint(key string, def ... uint) uint {
value := r.GetQueryString(key)
if value != "" {
2018-08-23 10:24:14 +08:00
return gconv.Uint(value)
2018-08-19 22:45:23 +08:00
}
if len(def) > 0 {
return def[0]
}
return 0
2018-07-29 22:01:29 +08:00
}
2018-08-19 22:45:23 +08:00
func (r *Request) GetQueryFloat32(key string, def ... float32) float32 {
value := r.GetQueryString(key)
if value != "" {
2018-08-23 10:24:14 +08:00
return gconv.Float32(value)
2018-08-19 22:45:23 +08:00
}
if len(def) > 0 {
return def[0]
}
return 0
2018-07-29 22:01:29 +08:00
}
2018-08-19 22:45:23 +08:00
func (r *Request) GetQueryFloat64(key string, def ... float64) float64 {
value := r.GetQueryString(key)
if value != "" {
2018-08-23 10:24:14 +08:00
return gconv.Float64(value)
2018-08-19 22:45:23 +08:00
}
if len(def) > 0 {
return def[0]
2018-07-29 22:01:29 +08:00
}
2018-08-19 22:45:23 +08:00
return 0
2018-07-29 22:01:29 +08:00
}
2018-08-19 22:45:23 +08:00
func (r *Request) GetQueryArray(key string, def ... []string) []string {
return r.GetQuery(key, def...)
2018-07-29 22:01:29 +08:00
}
// 获取指定键名的关联数组,并且给定当指定键名不存在时的默认值
2018-08-19 22:45:23 +08:00
func (r *Request) GetQueryMap(def ... map[string]string) map[string]string {
2018-07-29 22:01:29 +08:00
r.initGet()
m := make(map[string]string)
2018-08-19 22:45:23 +08:00
if len(def) == 0 {
2018-07-29 22:01:29 +08:00
for k, v := range r.queryVars {
m[k] = v[0]
}
} else {
2018-08-19 22:45:23 +08:00
for k, v := range def[0] {
2018-07-29 22:01:29 +08:00
v2 := r.GetQueryArray(k)
if v2 == nil {
m[k] = v
} else {
m[k] = v2[0]
}
}
}
return m
}
// 将所有的get参数映射到struct属性上参数object应当为一个struct对象的指针, mapping为非必需参数自定义参数与属性的映射关系
func (r *Request) GetQueryToStruct(object interface{}, mapping...map[string]string) {
tagmap := r.getStructParamsTagMap(object)
if len(mapping) > 0 {
for k, v := range mapping[0] {
tagmap[k] = v
}
}
params := make(map[string]interface{})
for k, v := range r.GetQueryMap() {
params[k] = v
}
gconv.Struct(params, object, tagmap)
}