2019-02-02 16:18:25 +08:00
|
|
|
|
// 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,
|
2019-02-02 16:18:25 +08:00
|
|
|
|
// 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"
|
2019-07-03 22:09:35 +08:00
|
|
|
|
|
2019-09-18 23:20:45 +08:00
|
|
|
|
"github.com/gogf/gf/text/gstr"
|
|
|
|
|
|
2019-07-29 21:01:19 +08:00
|
|
|
|
"github.com/gogf/gf/container/gvar"
|
2019-07-16 16:23:03 +08:00
|
|
|
|
|
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 {
|
2019-09-18 23:20:45 +08:00
|
|
|
|
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 {
|
2019-09-18 23:20:45 +08:00
|
|
|
|
r.getMap, _ = gstr.Parse(raw)
|
2019-06-19 09:06:52 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-07-29 22:01:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-09-18 23:20:45 +08:00
|
|
|
|
// 设置当前请求的GET参数
|
|
|
|
|
func (r *Request) SetQuery(key string, value interface{}) {
|
2019-06-19 09:06:52 +08:00
|
|
|
|
r.initGet()
|
2019-09-18 23:20:45 +08:00
|
|
|
|
r.getMap[key] = value
|
2018-07-31 14:28:41 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-07-29 22:01:29 +08:00
|
|
|
|
// 获得指定名称的get参数列表
|
2019-09-18 23:20:45 +08:00
|
|
|
|
func (r *Request) GetQuery(key string, def ...interface{}) interface{} {
|
2019-06-19 09:06:52 +08:00
|
|
|
|
r.initGet()
|
2019-09-18 23:20:45 +08:00
|
|
|
|
if v, ok := r.getMap[key]; ok {
|
2019-06-19 09:06:52 +08:00
|
|
|
|
return v
|
|
|
|
|
}
|
|
|
|
|
if len(def) > 0 {
|
2019-09-18 23:20:45 +08:00
|
|
|
|
return def[0]
|
2019-06-19 09:06:52 +08:00
|
|
|
|
}
|
|
|
|
|
return nil
|
2018-07-29 22:01:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-16 16:23:03 +08:00
|
|
|
|
func (r *Request) GetQueryVar(key string, def ...interface{}) *gvar.Var {
|
2019-09-18 23:20:45 +08:00
|
|
|
|
return gvar.New(r.GetQuery(key, def...))
|
2019-07-16 16:23:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-19 09:06:52 +08:00
|
|
|
|
func (r *Request) GetQueryString(key string, def ...interface{}) string {
|
2019-09-18 23:20:45 +08:00
|
|
|
|
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 {
|
2019-09-18 23:20:45 +08:00
|
|
|
|
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 {
|
2019-09-18 23:20:45 +08:00
|
|
|
|
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 {
|
2019-09-18 23:20:45 +08:00
|
|
|
|
return r.GetQueryVar(key, def...).Ints()
|
2018-10-09 10:05:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-19 09:06:52 +08:00
|
|
|
|
func (r *Request) GetQueryUint(key string, def ...interface{}) uint {
|
2019-09-18 23:20:45 +08:00
|
|
|
|
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 {
|
2019-09-18 23:20:45 +08:00
|
|
|
|
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 {
|
2019-09-18 23:20:45 +08:00
|
|
|
|
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 {
|
2019-09-18 23:20:45 +08:00
|
|
|
|
return r.GetQueryVar(key, def...).Floats()
|
2018-10-09 10:05:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-19 09:06:52 +08:00
|
|
|
|
func (r *Request) GetQueryArray(key string, def ...interface{}) []string {
|
2019-09-18 23:20:45 +08:00
|
|
|
|
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 {
|
2019-09-18 23:20:45 +08:00
|
|
|
|
return r.GetQueryVar(key, def...).Strings()
|
2018-10-09 10:05:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-19 09:06:52 +08:00
|
|
|
|
func (r *Request) GetQueryInterfaces(key string, def ...interface{}) []interface{} {
|
2019-09-18 23:20:45 +08:00
|
|
|
|
return r.GetQueryVar(key, def...).Interfaces()
|
2018-10-09 10:05:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-09-18 23:20:45 +08:00
|
|
|
|
// 获取指定键名的关联数组,并且给定当指定键名不存在时的默认值。
|
|
|
|
|
// 当不指定键值对关联数组时,默认获取GET方式提交的所有的提交键值对数据。
|
|
|
|
|
func (r *Request) GetQueryMap(kvMap ...map[string]interface{}) map[string]interface{} {
|
2019-06-19 09:06:52 +08:00
|
|
|
|
r.initGet()
|
2019-09-18 23:20:45 +08:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
}
|
2019-09-18 23:20:45 +08:00
|
|
|
|
return m
|
|
|
|
|
} else {
|
|
|
|
|
return r.getMap
|
2019-06-19 09:06:52 +08:00
|
|
|
|
}
|
2018-07-29 22:01:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-09-19 23:23:41 +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
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-12 10:50:03 +08:00
|
|
|
|
// 将所有的get参数映射到struct属性上,参数object应当为一个struct对象的指针, mapping为非必需参数,自定义参数与属性的映射关系
|
2019-06-19 09:06:52 +08:00
|
|
|
|
func (r *Request) GetQueryToStruct(pointer interface{}, mapping ...map[string]string) error {
|
2019-09-18 23:20:45 +08:00
|
|
|
|
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] {
|
2019-09-18 23:20:45 +08:00
|
|
|
|
tagMap[k] = v
|
2019-06-19 09:06:52 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-09-18 23:20:45 +08:00
|
|
|
|
return gconv.StructDeep(r.getMap, pointer, tagMap)
|
2019-06-19 09:06:52 +08:00
|
|
|
|
}
|