-
Notifications
You must be signed in to change notification settings - Fork 0
/
request_post.go
124 lines (96 loc) · 2.09 KB
/
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
package gintool
import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/lucky-lee/gutil/gStr"
"net/http"
"time"
)
//param uint8 value
func PostUint8(c *gin.Context, key string) uint8 {
val := c.PostForm(key)
toVal := gStr.ToUint8(val)
if toVal == 0 {
renderAbort(c, key)
}
return toVal
}
//param uint8 value and have default value
func PostUint8Def(c *gin.Context, key string, defVal uint8) uint8 {
val := c.PostForm(key)
toVal := gStr.ToUint8(val)
if toVal == 0 {
toVal = defVal
}
return toVal
}
//param int value
func PostInt(c *gin.Context, key string) int {
val := c.PostForm(key)
toVal := gStr.ToInt(val)
if toVal == 0 {
renderAbort(c, key)
}
return toVal
}
//param int value and have default value
func PostIntDef(c *gin.Context, key string, defVal int) int {
val := c.PostForm(key)
toVal := gStr.ToInt(val)
if toVal == 0 {
toVal = defVal
}
return toVal
}
//param int64 value
func PostInt64(c *gin.Context, key string) int64 {
val := c.PostForm(key)
toVal := gStr.ToInt64(val)
if toVal == 0 {
renderAbort(c, key)
}
return toVal
}
//param int64 value and have default value
func PostInt64Def(c *gin.Context, key string, defVal int64) int64 {
val := c.PostForm(key)
toVal := gStr.ToInt64(val)
if toVal == 0 {
toVal = defVal
}
return toVal
}
//param float value
func PostFloat64(c *gin.Context, key string) float64 {
val := c.PostForm(key)
if val == "" {
renderAbort(c, key)
return 0
}
toVal := gStr.ToFloat64(val)
return toVal
}
//param string value
func PostStr(c *gin.Context, key string) string {
val := c.PostForm(key)
if val == "" {
renderAbort(c, key)
}
return val
}
//param string value and have default value
func PostStrDef(c *gin.Context, key string, defVal string) string {
val := c.PostForm(key)
if val == "" {
val = defVal
}
return val
}
//输出 abort信息
func renderAbort(c *gin.Context, key string) {
if !c.IsAborted() {
res := RetStruct(http.StatusForbidden, "param err", nil)
c.AbortWithStatusJSON(http.StatusOK, res)
}
fmt.Println("time:", time.Now().Format("2006-01-02 15:04:05"), " param:", key, "no exist")
}