2021-01-17 21:46:25 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). 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 (
|
2021-10-11 21:41:56 +08:00
|
|
|
"github.com/gogf/gf/v2/container/gvar"
|
|
|
|
"github.com/gogf/gf/v2/util/gconv"
|
2018-07-29 22:01:29 +08:00
|
|
|
)
|
|
|
|
|
2022-03-19 17:58:21 +08:00
|
|
|
// SetQuery sets custom query value with key-value pairs.
|
2019-09-18 23:20:45 +08:00
|
|
|
func (r *Request) SetQuery(key string, value interface{}) {
|
2020-01-01 14:18:00 +08:00
|
|
|
r.parseQuery()
|
2019-11-29 22:02:19 +08:00
|
|
|
if r.queryMap == nil {
|
|
|
|
r.queryMap = make(map[string]interface{})
|
|
|
|
}
|
|
|
|
r.queryMap[key] = value
|
2018-07-31 14:28:41 +08:00
|
|
|
}
|
|
|
|
|
2022-03-19 17:58:21 +08:00
|
|
|
// GetQuery retrieves and return parameter with the given name `key` from query string
|
2021-10-21 18:22:47 +08:00
|
|
|
// and request body. It returns `def` if `key` does not exist in the query and `def` is given,
|
2020-04-06 22:31:45 +08:00
|
|
|
// or else it returns nil.
|
2019-12-01 14:07:36 +08:00
|
|
|
//
|
2021-09-19 23:13:53 +08:00
|
|
|
// Note that if there are multiple parameters with the same name, the parameters are retrieved
|
2020-04-06 22:31:45 +08:00
|
|
|
// and overwrote in order of priority: query > body.
|
2021-09-27 21:27:24 +08:00
|
|
|
func (r *Request) GetQuery(key string, def ...interface{}) *gvar.Var {
|
2020-01-01 14:18:00 +08:00
|
|
|
r.parseQuery()
|
2019-12-01 21:41:29 +08:00
|
|
|
if len(r.queryMap) > 0 {
|
2022-03-01 22:53:19 +08:00
|
|
|
if value, ok := r.queryMap[key]; ok {
|
|
|
|
return gvar.New(value)
|
2019-11-29 22:02:19 +08:00
|
|
|
}
|
|
|
|
}
|
2020-10-22 15:16:31 +08:00
|
|
|
if r.Method == "GET" {
|
|
|
|
r.parseBody()
|
|
|
|
}
|
2019-12-01 21:41:29 +08:00
|
|
|
if len(r.bodyMap) > 0 {
|
|
|
|
if v, ok := r.bodyMap[key]; ok {
|
2021-09-27 21:27:24 +08:00
|
|
|
return gvar.New(v)
|
2019-11-29 22:02:19 +08:00
|
|
|
}
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
|
|
|
if len(def) > 0 {
|
2021-09-27 21:27:24 +08:00
|
|
|
return gvar.New(def[0])
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
|
|
|
return nil
|
2018-07-29 22:01:29 +08:00
|
|
|
}
|
|
|
|
|
2022-03-19 17:58:21 +08:00
|
|
|
// GetQueryMap retrieves and returns all parameters passed from the client using HTTP GET method
|
|
|
|
// as the map. The parameter `kvMap` specifies the keys retrieving from client parameters,
|
2019-11-29 22:02:19 +08:00
|
|
|
// the associated values are the default values if the client does not pass.
|
2019-12-01 14:07:36 +08:00
|
|
|
//
|
2021-09-19 23:13:53 +08:00
|
|
|
// Note that if there are multiple parameters with the same name, the parameters are retrieved and overwrote
|
2019-12-01 21:41:29 +08:00
|
|
|
// in order of priority: query > body.
|
2019-09-18 23:20:45 +08:00
|
|
|
func (r *Request) GetQueryMap(kvMap ...map[string]interface{}) map[string]interface{} {
|
2020-01-01 14:18:00 +08:00
|
|
|
r.parseQuery()
|
2020-10-22 15:16:31 +08:00
|
|
|
if r.Method == "GET" {
|
|
|
|
r.parseBody()
|
|
|
|
}
|
2019-12-01 14:07:36 +08:00
|
|
|
var m map[string]interface{}
|
|
|
|
if len(kvMap) > 0 && kvMap[0] != nil {
|
2019-11-29 22:02:19 +08:00
|
|
|
if len(r.queryMap) == 0 && len(r.bodyMap) == 0 {
|
|
|
|
return kvMap[0]
|
|
|
|
}
|
2019-12-01 14:07:36 +08:00
|
|
|
m = make(map[string]interface{}, len(kvMap[0]))
|
2019-12-01 21:41:29 +08:00
|
|
|
if len(r.bodyMap) > 0 {
|
2019-11-29 22:02:19 +08:00
|
|
|
for k, v := range kvMap[0] {
|
2019-12-01 21:41:29 +08:00
|
|
|
if postValue, ok := r.bodyMap[k]; ok {
|
2019-11-29 22:02:19 +08:00
|
|
|
m[k] = postValue
|
|
|
|
} else {
|
|
|
|
m[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-12-01 21:41:29 +08:00
|
|
|
if len(r.queryMap) > 0 {
|
2019-11-29 22:02:19 +08:00
|
|
|
for k, v := range kvMap[0] {
|
2019-12-01 21:41:29 +08:00
|
|
|
if postValue, ok := r.queryMap[k]; ok {
|
2019-11-29 22:02:19 +08:00
|
|
|
m[k] = postValue
|
|
|
|
} else {
|
|
|
|
m[k] = v
|
|
|
|
}
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
|
|
|
}
|
2019-09-18 23:20:45 +08:00
|
|
|
} else {
|
2019-12-01 14:07:36 +08:00
|
|
|
m = make(map[string]interface{}, len(r.queryMap)+len(r.bodyMap))
|
2019-12-01 21:41:29 +08:00
|
|
|
for k, v := range r.bodyMap {
|
2019-11-29 22:02:19 +08:00
|
|
|
m[k] = v
|
|
|
|
}
|
2019-12-01 21:41:29 +08:00
|
|
|
for k, v := range r.queryMap {
|
2019-11-29 22:02:19 +08:00
|
|
|
m[k] = v
|
|
|
|
}
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
2019-11-29 22:02:19 +08:00
|
|
|
return m
|
2018-07-29 22:01:29 +08:00
|
|
|
}
|
|
|
|
|
2022-03-19 17:58:21 +08:00
|
|
|
// GetQueryMapStrStr retrieves and returns all parameters passed from the client using the HTTP GET method as a
|
|
|
|
// map[string]string. The parameter `kvMap` specifies the keys
|
2019-11-29 22:02:19 +08:00
|
|
|
// retrieving from client parameters, the associated values are the default values if the client
|
|
|
|
// does not pass.
|
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 {
|
2019-10-30 23:26:57 +08:00
|
|
|
m := make(map[string]string, len(queryMap))
|
2019-09-19 23:23:41 +08:00
|
|
|
for k, v := range queryMap {
|
|
|
|
m[k] = gconv.String(v)
|
|
|
|
}
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-19 17:58:21 +08:00
|
|
|
// GetQueryMapStrVar retrieves and returns all parameters passed from the client using the HTTP GET method
|
2021-10-21 18:22:47 +08:00
|
|
|
// as map[string]*gvar.Var. The parameter `kvMap` specifies the keys
|
2019-11-29 22:02:19 +08:00
|
|
|
// retrieving from client parameters, the associated values are the default values if the client
|
|
|
|
// does not pass.
|
2020-06-29 13:40:19 +08:00
|
|
|
func (r *Request) GetQueryMapStrVar(kvMap ...map[string]interface{}) map[string]*gvar.Var {
|
2019-09-23 16:21:19 +08:00
|
|
|
queryMap := r.GetQueryMap(kvMap...)
|
|
|
|
if len(queryMap) > 0 {
|
2020-06-29 13:40:19 +08:00
|
|
|
m := make(map[string]*gvar.Var, len(queryMap))
|
2019-09-23 16:21:19 +08:00
|
|
|
for k, v := range queryMap {
|
|
|
|
m[k] = gvar.New(v)
|
|
|
|
}
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-19 17:58:21 +08:00
|
|
|
// GetQueryStruct retrieves all parameters passed from the client using the HTTP GET method
|
|
|
|
// and converts them to a given struct object. Note that the parameter `pointer` is a pointer
|
2021-10-21 18:22:47 +08:00
|
|
|
// to the struct object. The optional parameter `mapping` is used to specify the key to
|
2019-11-29 22:02:19 +08:00
|
|
|
// attribute mapping.
|
2019-12-19 15:14:05 +08:00
|
|
|
func (r *Request) GetQueryStruct(pointer interface{}, mapping ...map[string]string) error {
|
2021-05-12 00:01:52 +08:00
|
|
|
_, err := r.doGetQueryStruct(pointer, mapping...)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Request) doGetQueryStruct(pointer interface{}, mapping ...map[string]string) (data map[string]interface{}, err error) {
|
2020-01-01 14:18:00 +08:00
|
|
|
r.parseQuery()
|
2021-05-12 00:01:52 +08:00
|
|
|
data = r.GetQueryMap()
|
2020-11-08 15:44:04 +08:00
|
|
|
if data == nil {
|
|
|
|
data = map[string]interface{}{}
|
2020-08-13 18:51:59 +08:00
|
|
|
}
|
2021-11-01 19:46:39 +08:00
|
|
|
if err = r.mergeDefaultStructValue(data, pointer); err != nil {
|
2021-05-12 00:01:52 +08:00
|
|
|
return data, nil
|
2020-11-08 15:44:04 +08:00
|
|
|
}
|
2021-05-12 00:01:52 +08:00
|
|
|
return data, gconv.Struct(data, pointer, mapping...)
|
2019-12-19 15:14:05 +08:00
|
|
|
}
|