github.com/zhongdalu/gf@v1.0.0/g/net/ghttp/ghttp_request_query.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  	"strings"
    11  
    12  	"github.com/zhongdalu/gf/g/container/gvar"
    13  
    14  	"github.com/zhongdalu/gf/g/internal/structs"
    15  	"github.com/zhongdalu/gf/g/util/gconv"
    16  )
    17  
    18  // 初始化GET请求参数
    19  func (r *Request) initGet() {
    20  	if !r.parsedGet {
    21  		r.queryVars = r.URL.Query()
    22  		if strings.EqualFold(r.Method, "GET") {
    23  			if raw := r.GetRawString(); len(raw) > 0 {
    24  				var array []string
    25  				for _, item := range strings.Split(raw, "&") {
    26  					array = strings.Split(item, "=")
    27  					r.queryVars[array[0]] = append(r.queryVars[array[0]], array[1])
    28  				}
    29  			}
    30  		}
    31  		r.parsedGet = true
    32  	}
    33  }
    34  
    35  // 设置GET参数,仅在ghttp.Server内有效,**注意并发安全性**
    36  func (r *Request) SetQuery(key string, value string) {
    37  	r.initGet()
    38  	r.queryVars[key] = []string{value}
    39  }
    40  
    41  // 添加GET参数,构成[]string
    42  func (r *Request) AddQuery(key string, value string) {
    43  	r.initGet()
    44  	r.queryVars[key] = append(r.queryVars[key], value)
    45  }
    46  
    47  // 获得指定名称的get参数列表
    48  func (r *Request) GetQuery(key string, def ...interface{}) []string {
    49  	r.initGet()
    50  	if v, ok := r.queryVars[key]; ok {
    51  		return v
    52  	}
    53  	if len(def) > 0 {
    54  		return gconv.Strings(def[0])
    55  	}
    56  	return nil
    57  }
    58  
    59  func (r *Request) GetQueryVar(key string, def ...interface{}) *gvar.Var {
    60  	return gvar.New(r.GetQueryString(key, def...), true)
    61  }
    62  
    63  func (r *Request) GetQueryString(key string, def ...interface{}) string {
    64  	value := r.GetQuery(key, def...)
    65  	if value != nil && value[0] != "" {
    66  		return value[0]
    67  	}
    68  	return ""
    69  }
    70  
    71  func (r *Request) GetQueryBool(key string, def ...interface{}) bool {
    72  	value := r.GetQueryString(key, def...)
    73  	if value != "" {
    74  		return gconv.Bool(value)
    75  	}
    76  	return false
    77  }
    78  
    79  func (r *Request) GetQueryInt(key string, def ...interface{}) int {
    80  	value := r.GetQueryString(key, def...)
    81  	if value != "" {
    82  		return gconv.Int(value)
    83  	}
    84  	return 0
    85  }
    86  
    87  func (r *Request) GetQueryInts(key string, def ...interface{}) []int {
    88  	value := r.GetQuery(key, def...)
    89  	if value != nil {
    90  		return gconv.Ints(value)
    91  	}
    92  	return nil
    93  }
    94  
    95  func (r *Request) GetQueryUint(key string, def ...interface{}) uint {
    96  	value := r.GetQueryString(key, def...)
    97  	if value != "" {
    98  		return gconv.Uint(value)
    99  	}
   100  	return 0
   101  }
   102  
   103  func (r *Request) GetQueryFloat32(key string, def ...interface{}) float32 {
   104  	value := r.GetQueryString(key, def...)
   105  	if value != "" {
   106  		return gconv.Float32(value)
   107  	}
   108  	return 0
   109  }
   110  
   111  func (r *Request) GetQueryFloat64(key string, def ...interface{}) float64 {
   112  	value := r.GetQueryString(key, def...)
   113  	if value != "" {
   114  		return gconv.Float64(value)
   115  	}
   116  	return 0
   117  }
   118  
   119  func (r *Request) GetQueryFloats(key string, def ...interface{}) []float64 {
   120  	value := r.GetQuery(key, def...)
   121  	if value != nil {
   122  		return gconv.Floats(value)
   123  	}
   124  	return nil
   125  }
   126  
   127  func (r *Request) GetQueryArray(key string, def ...interface{}) []string {
   128  	return r.GetQuery(key, def...)
   129  }
   130  
   131  func (r *Request) GetQueryStrings(key string, def ...interface{}) []string {
   132  	return r.GetQuery(key, def...)
   133  }
   134  
   135  func (r *Request) GetQueryInterfaces(key string, def ...interface{}) []interface{} {
   136  	value := r.GetQuery(key, def...)
   137  	if value != nil {
   138  		return gconv.Interfaces(value)
   139  	}
   140  	return nil
   141  }
   142  
   143  // 获取指定键名的关联数组,并且给定当指定键名不存在时的默认值
   144  func (r *Request) GetQueryMap(def ...map[string]string) map[string]string {
   145  	r.initGet()
   146  	m := make(map[string]string)
   147  	for k, v := range r.queryVars {
   148  		m[k] = v[0]
   149  	}
   150  	if len(def) > 0 {
   151  		for k, v := range def[0] {
   152  			if _, ok := m[k]; !ok {
   153  				m[k] = v
   154  			}
   155  		}
   156  	}
   157  	return m
   158  }
   159  
   160  // 将所有的get参数映射到struct属性上,参数object应当为一个struct对象的指针, mapping为非必需参数,自定义参数与属性的映射关系
   161  func (r *Request) GetQueryToStruct(pointer interface{}, mapping ...map[string]string) error {
   162  	tagmap := structs.TagMapName(pointer, paramTagPriority, true)
   163  	if len(mapping) > 0 {
   164  		for k, v := range mapping[0] {
   165  			tagmap[k] = v
   166  		}
   167  	}
   168  	params := make(map[string]interface{})
   169  	for k, v := range r.GetQueryMap() {
   170  		params[k] = v
   171  	}
   172  	return gconv.StructDeep(params, pointer, tagmap)
   173  }