-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
83 lines (75 loc) · 1.89 KB
/
utils.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
package main
import (
"regexp"
"strconv"
"strings"
"github.com/gin-gonic/gin"
//"github.com/smtc/glog"
)
const (
ErrCodeNeedLogin = 100
ErrMsgNeedLogin = "need login"
ErrCodeVisitor = 101
ErrMsgVisitor = "current user is visitor"
ErrCodeDBQuery = 102
ErrMsgDBQuery = "db query failed"
ErrCodeParam = 103
ErrCodeNotFound = 104
ErrCodeContentType = 105
ErrCodeReadRequest = 106
ErrCodeUnmarshalUser = 107
ErrCodeUpdateUser = 108
ErrCodeGetUserPosts = 109
ErrCodeGetUserReplies = 110
ErrCodeGetPostsByAction = 111
ErrCodeInvalidMethod = 112
ErrCodeUnmarshalPost = 113
ErrCodeNeeAuthen = 114
ErrCodeNeePerm = 115
ErrCodeUnmarshaLTax = 116
ErrCodeInvalidUUID = 117
ErrCodeGetPost = 118
ErrCodeReadReqPost = 119
ErrCodeCreateReply = 120
)
var (
// xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
// 54c7dae5-dc4b-4121-bf40-bcbd1f2958d8
idRe = regexp.MustCompile("[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}")
)
// 返回的数据格式
// code: 0:成功;其他值失败
// message: 对code的解释
// data: 返回的数据,失败时,可能为空
type RespResult struct {
Code int `json:"code"`
Message string `json:"message"`
Data interface{} `json:"data"`
}
func getQueryInt(c *gin.Context, key string) int {
v := strings.TrimSpace(c.Query(key))
if v == "" {
return 0
}
i, err := strconv.Atoi(v)
if err != nil {
return 0
}
return int(i)
}
// 获取query参数,如果没有或参数非法,返回默认值
func getQueryIntDefault(c *gin.Context, key string, d int) int {
v := strings.TrimSpace(c.Query(key))
if v == "" {
return d
}
i, err := strconv.Atoi(v)
if err != nil {
return d
}
return int(i)
}
// 校验uuid是否合法
func validUUID(id string) bool {
return idRe.MatchString(id)
}