github.com/zhongdalu/gf@v1.0.0/g/net/ghttp/ghttp_request_request.go (about)

     1  // Copyright 2017 gf Author(https://github.com/zhongdalu/gf). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/zhongdalu/gf.
     6  
     7  package ghttp
     8  
     9  import (
    10  	"github.com/zhongdalu/gf/g/container/gvar"
    11  	"github.com/zhongdalu/gf/g/internal/structs"
    12  	"github.com/zhongdalu/gf/g/util/gconv"
    13  )
    14  
    15  // 获得router、post或者get提交的参数,如果有同名参数,那么按照router->get->post优先级进行覆盖
    16  func (r *Request) GetRequest(key string, def ...interface{}) []string {
    17  	v := r.GetRouterArray(key)
    18  	if v == nil {
    19  		v = r.GetQuery(key)
    20  	}
    21  	if v == nil {
    22  		v = r.GetPost(key)
    23  	}
    24  	if v == nil && len(def) > 0 {
    25  		return gconv.Strings(def[0])
    26  	}
    27  	return v
    28  }
    29  
    30  func (r *Request) GetRequestVar(key string, def ...interface{}) *gvar.Var {
    31  	value := r.GetRequest(key, def...)
    32  	if value != nil {
    33  		return gvar.New(value[0], true)
    34  	}
    35  	return gvar.New(nil, true)
    36  }
    37  
    38  func (r *Request) GetRequestString(key string, def ...interface{}) string {
    39  	value := r.GetRequest(key, def...)
    40  	if value != nil && value[0] != "" {
    41  		return value[0]
    42  	}
    43  	return ""
    44  }
    45  
    46  func (r *Request) GetRequestBool(key string, def ...interface{}) bool {
    47  	value := r.GetRequestString(key, def...)
    48  	if value != "" {
    49  		return gconv.Bool(value)
    50  	}
    51  	return false
    52  }
    53  
    54  func (r *Request) GetRequestInt(key string, def ...interface{}) int {
    55  	value := r.GetRequestString(key, def...)
    56  	if value != "" {
    57  		return gconv.Int(value)
    58  	}
    59  	return 0
    60  }
    61  
    62  func (r *Request) GetRequestInts(key string, def ...interface{}) []int {
    63  	value := r.GetRequest(key, def...)
    64  	if value != nil {
    65  		return gconv.Ints(value)
    66  	}
    67  	return nil
    68  }
    69  
    70  func (r *Request) GetRequestUint(key string, def ...interface{}) uint {
    71  	value := r.GetRequestString(key, def...)
    72  	if value != "" {
    73  		return gconv.Uint(value)
    74  	}
    75  	return 0
    76  }
    77  
    78  func (r *Request) GetRequestFloat32(key string, def ...interface{}) float32 {
    79  	value := r.GetRequestString(key, def...)
    80  	if value != "" {
    81  		return gconv.Float32(value)
    82  	}
    83  	return 0
    84  }
    85  
    86  func (r *Request) GetRequestFloat64(key string, def ...interface{}) float64 {
    87  	value := r.GetRequestString(key, def...)
    88  	if value != "" {
    89  		return gconv.Float64(value)
    90  	}
    91  	return 0
    92  }
    93  
    94  func (r *Request) GetRequestFloats(key string, def ...interface{}) []float64 {
    95  	value := r.GetRequest(key, def...)
    96  	if value != nil {
    97  		return gconv.Floats(value)
    98  	}
    99  	return nil
   100  }
   101  
   102  func (r *Request) GetRequestArray(key string, def ...interface{}) []string {
   103  	return r.GetRequest(key, def...)
   104  }
   105  
   106  func (r *Request) GetRequestStrings(key string, def ...interface{}) []string {
   107  	return r.GetRequest(key, def...)
   108  }
   109  
   110  func (r *Request) GetRequestInterfaces(key string, def ...interface{}) []interface{} {
   111  	value := r.GetRequest(key, def...)
   112  	if value != nil {
   113  		return gconv.Interfaces(value)
   114  	}
   115  	return nil
   116  }
   117  
   118  // 获取指定键名的关联数组,并且给定当指定键名不存在时的默认值
   119  // 需要注意的是,如果其中一个字段为数组形式,那么只会返回第一个元素,如果需要获取全部的元素,请使用GetRequestArray获取特定字段内容
   120  func (r *Request) GetRequestMap(def ...map[string]string) map[string]string {
   121  	m := r.GetQueryMap()
   122  	if len(m) == 0 {
   123  		m = r.GetPostMap()
   124  	}
   125  	if len(def) > 0 {
   126  		for k, v := range def[0] {
   127  			if _, ok := m[k]; !ok {
   128  				m[k] = v
   129  			}
   130  		}
   131  	}
   132  	return m
   133  }
   134  
   135  // 将所有的request参数映射到struct属性上,参数object应当为一个struct对象的指针, mapping为非必需参数,自定义参数与属性的映射关系
   136  func (r *Request) GetRequestToStruct(pointer interface{}, mapping ...map[string]string) error {
   137  	tagMap := structs.TagMapName(pointer, paramTagPriority, true)
   138  	if len(mapping) > 0 {
   139  		for k, v := range mapping[0] {
   140  			tagMap[k] = v
   141  		}
   142  	}
   143  	params := make(map[string]interface{})
   144  	for k, v := range r.GetRequestMap() {
   145  		params[k] = v
   146  	}
   147  	if len(params) == 0 {
   148  		if j := r.GetJson(); j != nil {
   149  			params = j.ToMap()
   150  		}
   151  	}
   152  	return gconv.StructDeep(params, pointer, tagMap)
   153  }