forked from mlogclub/simple
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparams.go
More file actions
159 lines (141 loc) · 3.18 KB
/
Copy pathparams.go
File metadata and controls
159 lines (141 loc) · 3.18 KB
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
package simple
import (
"errors"
"fmt"
"github.com/MetrodataTeam/simple/date"
"strconv"
"strings"
"time"
"github.com/iris-contrib/schema"
"github.com/kataras/iris/v12"
)
var (
decoder = schema.NewDecoder() // form, url, schema.
)
func init() {
decoder.AddAliasTag("form", "json")
decoder.ZeroEmpty(true)
}
// param error
func paramError(name string) error {
return errors.New(fmt.Sprintf("unable to find param value '%s'", name))
}
// ReadForm read object from FormData
func ReadForm(ctx iris.Context, obj interface{}) error {
values := ctx.FormValues()
if len(values) == 0 {
return nil
}
decoder := schema.NewDecoder()
decoder.ZeroEmpty(true)
return decoder.Decode(obj, values)
}
func FormValue(ctx iris.Context, name string) string {
return ctx.FormValue(name)
}
func FormValueRequired(ctx iris.Context, name string) (string, error) {
str := FormValue(ctx, name)
if len(str) == 0 {
return "", errors.New("参数:" + name + "不能为空")
}
return str, nil
}
func FormValueDefault(ctx iris.Context, name, def string) string {
return ctx.FormValueDefault(name, def)
}
func FormValueInt(ctx iris.Context, name string) (int, error) {
str := ctx.FormValue(name)
if str == "" {
return 0, paramError(name)
}
return strconv.Atoi(str)
}
func FormValueIntDefault(ctx iris.Context, name string, def int) int {
if v, err := FormValueInt(ctx, name); err == nil {
return v
}
return def
}
func FormValueInt64(ctx iris.Context, name string) (int64, error) {
str := ctx.FormValue(name)
if str == "" {
return 0, paramError(name)
}
return strconv.ParseInt(str, 10, 64)
}
func FormValueInt64Default(ctx iris.Context, name string, def int64) int64 {
if v, err := FormValueInt64(ctx, name); err == nil {
return v
}
return def
}
func FormValueInt64Array(ctx iris.Context, name string) []int64 {
str := ctx.FormValue(name)
if str == "" {
return nil
}
ss := strings.Split(str, ",")
if len(ss) == 0 {
return nil
}
var ret []int64
for _, v := range ss {
item, err := strconv.ParseInt(v, 10, 64)
if err != nil {
continue
}
ret = append(ret, item)
}
return ret
}
func FormValueStringArray(ctx iris.Context, name string) []string {
str := ctx.FormValue(name)
if len(str) == 0 {
return nil
}
ss := strings.Split(str, ",")
if len(ss) == 0 {
return nil
}
var ret []string
for _, s := range ss {
s = strings.TrimSpace(s)
if len(s) == 0 {
continue
}
ret = append(ret, s)
}
return ret
}
func FormValueBool(ctx iris.Context, name string) (bool, error) {
str := ctx.FormValue(name)
if str == "" {
return false, paramError(name)
}
return strconv.ParseBool(str)
}
// 从请求中获取日期
func FormDate(ctx iris.Context, name string) *time.Time {
value := FormValue(ctx, name)
if IsBlank(value) {
return nil
}
layouts := []string{date.FmtDateTime, date.FmtDate, date.FmtDateTimeNoSeconds}
for _, layout := range layouts {
if ret, err := date.Parse(value, layout); err == nil {
return &ret
}
}
return nil
}
func GetPaging(ctx iris.Context) *Paging {
page := FormValueIntDefault(ctx, "page", 1)
limit := FormValueIntDefault(ctx, "limit", 20)
if page <= 0 {
page = 1
}
if limit <= 0 {
limit = 20
}
return &Paging{Page: page, Limit: limit}
}