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