-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathrequest.go
475 lines (372 loc) · 11.1 KB
/
request.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
// PhalGo-Request
// 请求解析,获取get,post,json参数,签名加密,链式操作,并且参数验证
// 喵了个咪 <wenzhenxi@vip.qq.com> 2016/5/11
// 依赖情况:
// "github.com/astaxie/beego/validation" 基于beego的拦截器(已经集成)
// "github.com/labstack/echo" 依赖于echo
package phalgo
import (
"strconv"
"github.com/labstack/echo"
"github.com/wenzhenxi/phalgo/validation"
"github.com/wenzhenxi/phalgo/errors"
"crypto/md5"
"encoding/hex"
"encoding/base64"
"regexp"
"fmt"
)
type Request struct {
Context echo.Context
params *param
Jsonparam *Jsonparam
valid validation.Validation
Json *Js
Encryption bool
Des Des
jsonTag bool
Debug bool
}
type Jsonparam struct {
key string
val Js
}
type param struct {
key string
val string
min int
max int
}
// 初始化request
func NewRequest(c echo.Context) *Request {
R := new(Request)
R.Context = c
//增加debug参数的匹配
if R.Param("__debug__").SetDefault("").GetString() == "" {
R.Debug = false
} else {
R.Debug = true
}
return R
}
// 清理参数
func (this *Request)Clean() {
this.params = new(param)
this.Jsonparam = new(Jsonparam)
}
// 返回报错信息
func (this *Request)GetError() error {
if this.valid.HasErrors() {
for _, v := range this.valid.Errors {
return errors.ErrCustom(v.Message + v.Key)
}
}
return nil
}
// 进行签名验证以及DES加密验证
func (this *Request)InitDES() error {
params := ""
this.Json = new(Js)
params = this.PostParam(Config.GetString("system.DESParam")).GetString()
//如果是开启了 DES加密 需要验证是否加密,然后需要验证签名,和加密内容
if Config.GetBool("system.OpenDES") == true {
if params == "" {
return errors.ErrNoParams
}
}
if params != "" {
enableSignCheck := Config.GetBool("DES.EnableSignCheck")
sign := this.PostParam("sign").GetString()
timeStamp := this.PostParam("timeStamp").GetString()
randomNum := this.PostParam("randomNum").GetString()
isEncrypted := this.PostParam("isEncrypted").GetString()
if enableSignCheck {
if sign == "" || timeStamp == "" || randomNum == "" {
return errors.ErrMD5
}
keymd5 := md5.New()
keymd5.Write([]byte(Config.GetString("system.MD5key")))
md5key := hex.EncodeToString(keymd5.Sum(nil))
signmd5 := md5.New()
signmd5.Write([]byte(params + isEncrypted + timeStamp + randomNum + md5key))
sign2 := hex.EncodeToString(signmd5.Sum(nil))
if sign != sign2 {
return errors.ErrMD5
}
}
//如果是加密的params那么进行解密操作
if isEncrypted == "1" {
base64params, err := base64.StdEncoding.DecodeString(params)
if err != nil {
return err
}
origData, err := this.Des.DesDecrypt(base64params, Config.GetString("system.DESkey"), Config.GetString("system.DESiv"))
if err != nil {
return err
}
params = string(origData)
}
this.Json = Json(params)
this.Encryption = true
}
return nil;
}
// 使用Json参数传入Json字符
func (this *Request)SetJson(json string) {
this.Json = Json(json)
}
//--------------------------------------------------------获取参数-------------------------------------
// 获取Json参数
func (this *Request)DESParam(keys ...string) *Request {
var key string
var str string
this.Clean()
if (this.Encryption) {
json := *this.Json
for _, v := range keys {
json.Get(v)
key = key + v
}
this.Jsonparam.val = json
this.Jsonparam.key = key
this.jsonTag = true
} else {
str = this.Context.QueryParam(keys[0])
if str == "" {
str = this.Context.FormValue(keys[0])
}
this.params.val = str
this.params.key = keys[0]
this.jsonTag = false
}
return this
}
// 获取Json参数
func (this *Request)JsonParam(keys ...string) *Request {
var key string
this.Clean()
json := *this.Json
for _, v := range keys {
json.Get(v)
key = key + v
}
this.Jsonparam.val = json
this.Jsonparam.key = key
this.jsonTag = true
return this
}
// 获取Get参数
func (this *Request)GetParam(key string) *Request {
this.Clean()
str := this.Context.QueryParam(key)
this.params.val = str
this.params.key = key
this.jsonTag = false
return this
}
// 获取post参数
func (this *Request)PostParam(key string) *Request {
this.Clean()
str := this.Context.FormValue(key)
this.params.val = str
this.params.key = key
this.jsonTag = false
return this
}
// 获取请求参数顺序get->post
func (this *Request)Param(key string) *Request {
var str string
this.Clean()
str = this.Context.QueryParam(key)
if str == "" {
str = this.Context.FormValue(key)
}
this.params.val = str
this.params.key = key
return this
}
func (this *Request)SetDefault(val string) *Request {
if this.jsonTag == true {
defJson := fmt.Sprintf(`{"index":"%s"}`, val)
this.Jsonparam.val = *Json(defJson).Get("index")
/* fmt.Println(defJson)
fmt.Println(this.Jsonparam.val.Tostring())*/
} else {
this.params.val = val
}
return this
}
//----------------------------------------------------过滤验证------------------------------------
// GET,POST或JSON参数是否必须
func (this *Request)Require(b bool) *Request {
this.valid.Required(this.getParamVal(), this.getParamKey()).Message("缺少必要参数,参数名称:")
return this
}
// 设置参数最大值
func (this *Request)Max(i int) *Request {
this.params.max = i
return this
}
//设置参数最小值
func (this *Request)Min(i int) *Request {
this.params.min = i
return this
}
//--------------------------------------------GET,POST获取参数------------------------------------
// 获取并且验证参数 string类型 适用于GET或POST参数
func (this *Request)GetString() string {
var str string
str = this.getParamVal()
if this.params.min != 0 {
this.valid.MinSize(str, this.params.min, this.getParamKey()).
Message("参数异常!参数长度为%d不能小于%d,参数名称:", len([]rune(str)), this.params.min)
}
if this.params.max != 0 {
this.valid.MaxSize(str, this.params.max, this.getParamKey()).
Message("参数异常!参数长度为%d不能大于%d,参数名称:", len([]rune(str)), this.params.max)
}
return str
}
// 获取并且验证参数 int类型 适用于GET或POST参数
func (this *Request)GetInt() int {
var (
i int
err error
)
if this.getParamVal() == "" {
i = 0
} else {
i, err = strconv.Atoi(this.getParamVal())
if err != nil {
this.valid.SetError(this.getParamKey(), "参数异常!参数不是int类型,参数名称:")
}
}
if this.params.min != 0 {
this.valid.Min(i, this.params.min, this.getParamKey()).
Message("参数异常!参数值为%d不能小于%d,参数名称:", i, this.params.min)
}
if this.params.max != 0 {
this.valid.Max(i, this.params.max, this.getParamKey()).
Message("参数异常!参数值为%d不能大于%d,参数名称:", i, this.params.max)
}
return i
}
// 获取并且验证参数 float64类型 适用于GET或POST参数
func (this *Request)GetFloat() float64 {
var (
i float64
err error
)
if this.getParamVal() == "" {
i = 0
} else {
i, err = strconv.ParseFloat(this.getParamVal(), 64)
if err != nil {
this.valid.SetError(this.getParamKey(), "此参数无法转换为float64类型,参数名称:")
}
}
if this.params.min != 0 {
this.valid.Min(int(i), this.params.min, this.getParamKey()).
Message("参数异常!参数值为%f不能小于%d,参数名称:", i, this.params.min)
}
if this.params.max != 0 {
this.valid.Max(int(i), this.params.max, this.getParamKey()).
Message("参数异常!参数值为%f不能大于%d,参数名称:", i, this.params.max)
}
return i
}
// 邮政编码
func (this *Request)ZipCode() *Request {
this.valid.ZipCode(this.getParamVal(), this.getParamKey()).Message("参数异常!邮政编码验证失败,参数名称:")
return this
}
// 手机号或固定电话号
func (this *Request)Phone() *Request {
this.valid.Phone(this.getParamVal(), this.getParamKey()).Message("参数异常!手机号或固定电话号验证失败,参数名称:")
return this
}
// 固定电话号
func (this *Request)Tel() *Request {
this.valid.Tel(this.getParamVal(), this.getParamKey()).Message("参数异常!固定电话号验证失败,参数名称:")
return this
}
// 手机号
func (this *Request)Mobile() *Request {
this.valid.Mobile(this.getParamVal(), this.getParamKey()).Message("参数异常!手机号验证失败,参数名称:")
return this
}
// base64编码
func (this *Request)Base64() *Request {
this.valid.Base64(this.getParamVal(), this.getParamKey()).Message("参数异常!base64编码验证失败,参数名称:")
return this
}
// IP格式,目前只支持IPv4格式验证
func (this *Request)IP() *Request {
this.valid.IP(this.getParamVal(), this.getParamKey()).Message("参数异常!IP格式验证失败,参数名称:")
return this
}
// 邮箱格式
func (this *Request)Email() *Request {
this.valid.Email(this.getParamVal(), this.getParamKey()).Message("参数异常!邮箱格式验证失败,参数名称:")
return this
}
// 正则匹配,其他类型都将被转成字符串再匹配(fmt.Sprintf(“%v”, obj).Match)
func (this *Request)Match(match string) *Request {
this.valid.Match(this.getParamVal(), regexp.MustCompile(match), this.getParamKey()).Message("参数异常!正则验证失败,参数名称:")
return this
}
// 反正则匹配,其他类型都将被转成字符串再匹配(fmt.Sprintf(“%v”, obj).Match)
func (this *Request)NoMatch(match string) *Request {
this.valid.NoMatch(this.getParamVal(), regexp.MustCompile(match), this.getParamKey()).Message("参数异常!邮箱格式验证失败,参数名称:")
return this
}
// 数字
func (this *Request)Numeric() *Request {
this.valid.Numeric(this.getParamVal(), this.getParamKey()).Message("参数异常!数字格式验证失败,参数名称:")
return this
}
// alpha字符
func (this *Request)Alpha() *Request {
this.valid.Alpha(this.getParamVal(), this.getParamKey()).Message("参数异常!alpha格式验证失败,参数名称:")
return this
}
// alpha字符或数字
func (this *Request)AlphaNumeric() *Request {
this.valid.AlphaNumeric(this.getParamVal(), this.getParamKey()).Message("参数异常!AlphaNumeric格式验证失败,参数名称:")
return this
}
// alpha字符或数字或横杠-_
func (this *Request)AlphaDash() *Request {
this.valid.AlphaDash(this.getParamVal(), this.getParamKey()).Message("参数异常!AlphaDash格式验证失败,参数名称:")
return this
}
// 返回解析参数的Val
func (this *Request)getParamVal() string {
if this.jsonTag {
return this.Jsonparam.val.Tostring()
} else {
return this.params.val
}
}
// 反回解析参数的Key
func (this *Request)getParamKey() string {
if this.jsonTag {
return this.Jsonparam.key
} else {
return this.params.key
}
}
// 获取并且验证参数 Json类型 适用于Json参数
func (this *Request)GetJson() Js {
return this.Jsonparam.val
}
// 捕获panic异样防止程序终止 并且记录到日志
func (this *Request)ErrorLogRecover() {
if err := recover(); err != nil {
this.Context.Response().Write([]byte("系统错误!具体原因:" + TurnString(err)))
LogError(err, map[string]interface{}{
"URL.Path":this.Context.Request().URL.Path,
"QueryParams":this.Context.QueryParams(),
})
}
}