github.com/zhongdalu/gf@v1.0.0/g/net/ghttp/ghttp_request_post.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 // 初始化POST请求参数 16 func (r *Request) initPost() { 17 if !r.parsedPost { 18 // MultiMedia表单请求解析允许最大使用内存:1GB 19 if r.ParseMultipartForm(1024*1024*1024) == nil { 20 r.parsedPost = true 21 } 22 } 23 } 24 25 // 设置POST参数,仅在ghttp.Server内有效,**注意并发安全性** 26 func (r *Request) SetPost(key string, value string) { 27 r.initPost() 28 r.PostForm[key] = []string{value} 29 } 30 31 func (r *Request) AddPost(key string, value string) { 32 r.initPost() 33 r.PostForm[key] = append(r.PostForm[key], value) 34 } 35 36 func (r *Request) GetPost(key string, def ...interface{}) []string { 37 r.initPost() 38 if v, ok := r.PostForm[key]; ok { 39 return v 40 } 41 if len(def) > 0 { 42 return gconv.Strings(def[0]) 43 } 44 return nil 45 } 46 47 func (r *Request) GetPostVar(key string, def ...interface{}) *gvar.Var { 48 return gvar.New(r.GetPostString(key, def...), true) 49 } 50 51 func (r *Request) GetPostString(key string, def ...interface{}) string { 52 value := r.GetPost(key, def...) 53 if value != nil && value[0] != "" { 54 return value[0] 55 } 56 return "" 57 } 58 59 func (r *Request) GetPostBool(key string, def ...interface{}) bool { 60 value := r.GetPostString(key, def...) 61 if value != "" { 62 return gconv.Bool(value) 63 } 64 return false 65 } 66 67 func (r *Request) GetPostInt(key string, def ...interface{}) int { 68 value := r.GetPostString(key, def...) 69 if value != "" { 70 return gconv.Int(value) 71 } 72 return 0 73 } 74 75 func (r *Request) GetPostInts(key string, def ...interface{}) []int { 76 value := r.GetPost(key, def...) 77 if value != nil { 78 return gconv.Ints(value) 79 } 80 return nil 81 } 82 83 func (r *Request) GetPostUint(key string, def ...interface{}) uint { 84 value := r.GetPostString(key, def...) 85 if value != "" { 86 return gconv.Uint(value) 87 } 88 return 0 89 } 90 91 func (r *Request) GetPostFloat32(key string, def ...interface{}) float32 { 92 value := r.GetPostString(key, def...) 93 if value != "" { 94 return gconv.Float32(value) 95 } 96 return 0 97 } 98 99 func (r *Request) GetPostFloat64(key string, def ...interface{}) float64 { 100 value := r.GetPostString(key, def...) 101 if value != "" { 102 return gconv.Float64(value) 103 } 104 return 0 105 } 106 107 func (r *Request) GetPostFloats(key string, def ...interface{}) []float64 { 108 value := r.GetPost(key, def...) 109 if value != nil { 110 return gconv.Floats(value) 111 } 112 return nil 113 } 114 115 func (r *Request) GetPostArray(key string, def ...interface{}) []string { 116 return r.GetPost(key, def...) 117 } 118 119 func (r *Request) GetPostStrings(key string, def ...interface{}) []string { 120 return r.GetPost(key, def...) 121 } 122 123 func (r *Request) GetPostInterfaces(key string, def ...interface{}) []interface{} { 124 value := r.GetPost(key, def...) 125 if value != nil { 126 return gconv.Interfaces(value) 127 } 128 return nil 129 } 130 131 // 获取指定键名的关联数组,并且给定当指定键名不存在时的默认值 132 // 需要注意的是,如果其中一个字段为数组形式,那么只会返回第一个元素,如果需要获取全部的元素,请使用GetPostArray获取特定字段内容 133 func (r *Request) GetPostMap(def ...map[string]string) map[string]string { 134 r.initPost() 135 m := make(map[string]string) 136 for k, v := range r.PostForm { 137 m[k] = v[0] 138 } 139 if len(def) > 0 { 140 for k, v := range def[0] { 141 if _, ok := m[k]; !ok { 142 m[k] = v 143 } 144 } 145 } 146 return m 147 } 148 149 // 将所有的request参数映射到struct属性上,参数object应当为一个struct对象的指针, mapping为非必需参数,自定义参数与属性的映射关系 150 func (r *Request) GetPostToStruct(pointer interface{}, mapping ...map[string]string) error { 151 tagMap := structs.TagMapName(pointer, paramTagPriority, true) 152 if len(mapping) > 0 { 153 for k, v := range mapping[0] { 154 tagMap[k] = v 155 } 156 } 157 params := make(map[string]interface{}) 158 for k, v := range r.GetPostMap() { 159 params[k] = v 160 } 161 return gconv.StructDeep(params, pointer, tagMap) 162 }