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-07-29 21:01:19 +08:00
|
|
|
|
"github.com/gogf/gf/container/gvar"
|
|
|
|
|
|
"github.com/gogf/gf/internal/structs"
|
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/util/gconv"
|
2018-07-29 22:01:29 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
2019-09-18 23:20:45 +08:00
|
|
|
|
// 初始化RAW请求参数
|
|
|
|
|
|
func (r *Request) initRaw() {
|
|
|
|
|
|
if !r.parsedRaw {
|
|
|
|
|
|
r.parsedRaw = true
|
|
|
|
|
|
if raw := r.GetRawString(); len(raw) > 0 {
|
|
|
|
|
|
r.rawVarMap, _ = gstr.Parse(raw)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-07-29 22:01:29 +08:00
|
|
|
|
// 获得router、post或者get提交的参数,如果有同名参数,那么按照router->get->post优先级进行覆盖
|
2019-09-18 23:20:45 +08:00
|
|
|
|
func (r *Request) GetRequest(key string, def ...interface{}) interface{} {
|
|
|
|
|
|
v := r.GetRouterValue(key)
|
2019-06-19 09:06:52 +08:00
|
|
|
|
if v == nil {
|
|
|
|
|
|
v = r.GetQuery(key)
|
|
|
|
|
|
}
|
|
|
|
|
|
if v == nil {
|
|
|
|
|
|
v = r.GetPost(key)
|
|
|
|
|
|
}
|
2019-09-18 23:20:45 +08:00
|
|
|
|
if v != nil {
|
|
|
|
|
|
return v
|
|
|
|
|
|
}
|
|
|
|
|
|
r.initRaw()
|
|
|
|
|
|
v = r.rawVarMap[key]
|
2019-06-19 09:06:52 +08:00
|
|
|
|
if v == nil && len(def) > 0 {
|
2019-09-18 23:20:45 +08:00
|
|
|
|
return def[0]
|
2019-06-19 09:06:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
return v
|
2018-07-29 22:01:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-06-19 09:06:52 +08:00
|
|
|
|
func (r *Request) GetRequestVar(key string, def ...interface{}) *gvar.Var {
|
2019-09-18 23:20:45 +08:00
|
|
|
|
return gvar.New(r.GetRequest(key, def...))
|
2018-10-09 13:33:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-06-19 09:06:52 +08:00
|
|
|
|
func (r *Request) GetRequestString(key string, def ...interface{}) string {
|
2019-09-18 23:20:45 +08:00
|
|
|
|
return r.GetRequestVar(key, def...).String()
|
2018-07-29 22:01:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-06-19 09:06:52 +08:00
|
|
|
|
func (r *Request) GetRequestBool(key string, def ...interface{}) bool {
|
2019-09-18 23:20:45 +08:00
|
|
|
|
return r.GetRequestVar(key, def...).Bool()
|
2018-07-29 22:01:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-06-19 09:06:52 +08:00
|
|
|
|
func (r *Request) GetRequestInt(key string, def ...interface{}) int {
|
2019-09-18 23:20:45 +08:00
|
|
|
|
return r.GetRequestVar(key, def...).Int()
|
2018-07-29 22:01:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-06-19 09:06:52 +08:00
|
|
|
|
func (r *Request) GetRequestInts(key string, def ...interface{}) []int {
|
2019-09-18 23:20:45 +08:00
|
|
|
|
return r.GetRequestVar(key, def...).Ints()
|
2018-10-09 10:05:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-06-19 09:06:52 +08:00
|
|
|
|
func (r *Request) GetRequestUint(key string, def ...interface{}) uint {
|
2019-09-18 23:20:45 +08:00
|
|
|
|
return r.GetRequestVar(key, def...).Uint()
|
2018-07-29 22:01:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-06-19 09:06:52 +08:00
|
|
|
|
func (r *Request) GetRequestFloat32(key string, def ...interface{}) float32 {
|
2019-09-18 23:20:45 +08:00
|
|
|
|
return r.GetRequestVar(key, def...).Float32()
|
2018-07-29 22:01:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-06-19 09:06:52 +08:00
|
|
|
|
func (r *Request) GetRequestFloat64(key string, def ...interface{}) float64 {
|
2019-09-18 23:20:45 +08:00
|
|
|
|
return r.GetRequestVar(key, def...).Float64()
|
2018-07-29 22:01:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-06-19 09:06:52 +08:00
|
|
|
|
func (r *Request) GetRequestFloats(key string, def ...interface{}) []float64 {
|
2019-09-18 23:20:45 +08:00
|
|
|
|
return r.GetRequestVar(key, def...).Floats()
|
2018-10-09 10:05:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-06-19 09:06:52 +08:00
|
|
|
|
func (r *Request) GetRequestArray(key string, def ...interface{}) []string {
|
2019-09-18 23:20:45 +08:00
|
|
|
|
return r.GetRequestVar(key, def...).Strings()
|
2018-07-29 22:01:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-06-19 09:06:52 +08:00
|
|
|
|
func (r *Request) GetRequestStrings(key string, def ...interface{}) []string {
|
2019-09-18 23:20:45 +08:00
|
|
|
|
return r.GetRequestVar(key, def...).Strings()
|
2018-10-09 10:05:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-06-19 09:06:52 +08:00
|
|
|
|
func (r *Request) GetRequestInterfaces(key string, def ...interface{}) []interface{} {
|
2019-09-18 23:20:45 +08:00
|
|
|
|
return r.GetRequestVar(key, def...).Interfaces()
|
2018-10-09 10:05:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-07-29 22:01:29 +08:00
|
|
|
|
// 获取指定键名的关联数组,并且给定当指定键名不存在时的默认值
|
|
|
|
|
|
// 需要注意的是,如果其中一个字段为数组形式,那么只会返回第一个元素,如果需要获取全部的元素,请使用GetRequestArray获取特定字段内容
|
2019-09-18 23:20:45 +08:00
|
|
|
|
func (r *Request) GetRequestMap(kvMap ...map[string]interface{}) map[string]interface{} {
|
|
|
|
|
|
r.initRaw()
|
|
|
|
|
|
m := r.rawVarMap
|
|
|
|
|
|
if len(kvMap) > 0 {
|
|
|
|
|
|
m = make(map[string]interface{})
|
|
|
|
|
|
for k, defValue := range kvMap[0] {
|
|
|
|
|
|
if rawValue, ok := r.rawVarMap[k]; ok {
|
|
|
|
|
|
m[k] = rawValue
|
|
|
|
|
|
} else {
|
|
|
|
|
|
m[k] = defValue
|
2019-06-19 09:06:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2019-09-18 23:20:45 +08:00
|
|
|
|
if m == nil {
|
|
|
|
|
|
m = make(map[string]interface{})
|
|
|
|
|
|
}
|
|
|
|
|
|
for k, v := range r.GetPostMap(kvMap...) {
|
|
|
|
|
|
m[k] = v
|
|
|
|
|
|
}
|
|
|
|
|
|
for k, v := range r.GetQueryMap(kvMap...) {
|
|
|
|
|
|
m[k] = v
|
|
|
|
|
|
}
|
2019-06-19 09:06:52 +08:00
|
|
|
|
return m
|
2018-07-29 22:01:29 +08:00
|
|
|
|
}
|
2018-08-12 10:50:03 +08:00
|
|
|
|
|
2019-09-19 23:23:41 +08:00
|
|
|
|
func (r *Request) GetRequestMapStrStr(kvMap ...map[string]interface{}) map[string]string {
|
|
|
|
|
|
requestMap := r.GetRequestMap(kvMap...)
|
|
|
|
|
|
if len(requestMap) > 0 {
|
|
|
|
|
|
m := make(map[string]string)
|
|
|
|
|
|
for k, v := range requestMap {
|
|
|
|
|
|
m[k] = gconv.String(v)
|
|
|
|
|
|
}
|
|
|
|
|
|
return m
|
|
|
|
|
|
}
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-09-23 16:21:19 +08:00
|
|
|
|
func (r *Request) GetRequestMapStrVar(kvMap ...map[string]interface{}) map[string]*gvar.Var {
|
|
|
|
|
|
requestMap := r.GetRequestMap(kvMap...)
|
|
|
|
|
|
if len(requestMap) > 0 {
|
|
|
|
|
|
m := make(map[string]*gvar.Var)
|
|
|
|
|
|
for k, v := range requestMap {
|
|
|
|
|
|
m[k] = gvar.New(v)
|
|
|
|
|
|
}
|
|
|
|
|
|
return m
|
|
|
|
|
|
}
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-08-12 10:50:03 +08:00
|
|
|
|
// 将所有的request参数映射到struct属性上,参数object应当为一个struct对象的指针, mapping为非必需参数,自定义参数与属性的映射关系
|
2019-06-19 09:06:52 +08:00
|
|
|
|
func (r *Request) GetRequestToStruct(pointer interface{}, mapping ...map[string]string) error {
|
2019-07-04 11:11:41 +08:00
|
|
|
|
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-09-18 23:20:45 +08:00
|
|
|
|
return gconv.StructDeep(r.GetRequestMap(), pointer, tagMap)
|
2018-08-12 10:50:03 +08:00
|
|
|
|
}
|