-
Notifications
You must be signed in to change notification settings - Fork 3
/
param.go
87 lines (74 loc) · 2.15 KB
/
param.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
package ginKit
import (
"github.com/gin-gonic/gin"
"github.com/richelieu-yang/chimera/v2/src/core/boolKit"
"github.com/richelieu-yang/chimera/v2/src/core/floatKit"
"github.com/richelieu-yang/chimera/v2/src/core/intKit"
"github.com/richelieu-yang/chimera/v2/src/core/strKit"
"io"
)
// ObtainGetParam 从url获取参数
func ObtainGetParam(ctx *gin.Context, key string) string {
return ctx.Query(key)
}
// ObtainPostParam
/*
支持的Content-Type: multipart/form-data、x-www-form-urlencoded ...
*/
func ObtainPostParam(ctx *gin.Context, key string) string {
return ctx.PostForm(key)
}
// ObtainParam 获取请求参数(优先级: GET > POST)
func ObtainParam(ctx *gin.Context, key string) string {
// (1) GET
value := ObtainGetParam(ctx, key)
if strKit.IsNotEmpty(value) {
return value
}
// (2) POST
return ObtainPostParam(ctx, key)
}
func ObtainBoolParam(ctx *gin.Context, key string) (bool, error) {
value := ObtainParam(ctx, key)
return boolKit.ToBoolE(value)
}
func ObtainIntParam(ctx *gin.Context, key string) (int, error) {
value := ObtainParam(ctx, key)
return intKit.ToIntE(value)
}
func ObtainInt32Param(ctx *gin.Context, key string) (int32, error) {
value := ObtainParam(ctx, key)
return intKit.ToInt32E(value)
}
func ObtainInt64Param(ctx *gin.Context, key string) (int64, error) {
value := ObtainParam(ctx, key)
return intKit.ToInt64E(value)
}
func ObtainFloat32Param(ctx *gin.Context, key string) (float32, error) {
value := ObtainParam(ctx, key)
return floatKit.ToFloat32E(value)
}
func ObtainFloat64Param(ctx *gin.Context, key string) (float64, error) {
value := ObtainParam(ctx, key)
return floatKit.ToFloat64E(value)
}
// ObtainFormFileContent form请求,根据 传参key 获取文件的字节流.(单文件上传)
/*
@return 文件内容 + 文件名 + 错误
*/
func ObtainFormFileContent(ctx *gin.Context, key string) ([]byte, string, error) {
fileHeader, err := ctx.FormFile(key)
if err != nil {
return nil, "", err
}
file, err := fileHeader.Open()
if err != nil {
return nil, "", err
}
defer file.Close()
content, err := io.ReadAll(file)
if err != nil {
return nil, "", err
}
return content, fileHeader.Filename, nil
}