-
Notifications
You must be signed in to change notification settings - Fork 1
/
controller.go
136 lines (124 loc) · 3.15 KB
/
controller.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
package types
import (
"fmt"
libJson "github.com/ZYallers/golib/utils/json"
"github.com/ZYallers/zgin/consts"
"github.com/gin-gonic/gin"
"github.com/spf13/cast"
"net/http"
"strconv"
)
type IController interface {
SetContext(*gin.Context)
}
type Controller struct {
Ctx *gin.Context
}
func (c *Controller) SetContext(ctx *gin.Context) {
c.Ctx = ctx
}
func (c *Controller) DumpRequest() string {
reqStr, _ := c.Ctx.Get(consts.ReqStrKey)
return reqStr.(string)
}
// GetLoggedUserId 获取已登陆用户的ID
func (c *Controller) GetLoggedUserId() int {
if data, ok := c.Ctx.Get(consts.SessDataKey); ok && data != nil {
vars := data.(map[string]interface{})
if userInfo, ok := vars["userinfo"].(map[string]interface{}); ok {
if str, ok := userInfo["userid"].(string); ok && str != "" {
userId, _ := strconv.Atoi(str)
return userId
}
}
}
return 0
}
// Json 输出方法
// args 三个参数: 第一个是code,第二个是msg,第三个是data
func (c *Controller) Json(args ...interface{}) {
var result gin.H
switch len(args) {
case 1:
result = gin.H{"code": cast.ToInt(args[0]), "msg": "ok", "data": nil}
case 2:
result = gin.H{"code": cast.ToInt(args[0]), "msg": cast.ToString(args[1]), "data": nil}
case 3:
result = gin.H{"code": cast.ToInt(args[0]), "msg": cast.ToString(args[1]), "data": args[2]}
case 4:
result = gin.H{"code": cast.ToInt(args[0]), "msg": cast.ToString(args[1]), "data": args[2], "record": args[3]}
}
bte, err := libJson.Marshal(result)
if err != nil {
bte = []byte(fmt.Sprintf(`{"code":%d,"msg":"%s"}`, http.StatusInternalServerError, cast.ToString(err)))
}
c.Ctx.Abort()
c.Ctx.Status(http.StatusOK)
c.Ctx.Writer.Header().Set("Content-Type", "application/json;charset=utf-8")
_, _ = c.Ctx.Writer.Write(bte)
}
// 从Get和Post里获取Key的值
func (c *Controller) GetQueryPostForm(keys ...string) string {
if len(keys) == 0 {
return ""
}
if val, ok := c.Ctx.GetQuery(keys[0]); ok {
return val
}
if val, ok := c.Ctx.GetPostForm(keys[0]); ok {
return val
}
if len(keys) == 2 {
return keys[1]
}
return ""
}
// GetQueryByMethod 根据ctx.httpMethod获取GET或POST参数
func (c *Controller) GetQueryByMethod(key, defaultValue string) string {
var query string
switch c.Ctx.Request.Method {
case http.MethodPost:
query = c.Ctx.DefaultPostForm(key, defaultValue)
default:
query = c.Ctx.DefaultQuery(key, defaultValue)
}
return query
}
// QueryPostNumber 从Query或PostForm中获取数字类型key值
func (c *Controller) QueryPostNumber(key string, defaultValue ...int) int {
s := c.GetQueryPostForm(key)
if s != "" {
i, _ := strconv.Atoi(s)
return i
}
if len(defaultValue) > 0 {
return defaultValue[0]
}
return 0
}
// Query 从Get里获取Key的值
func (c *Controller) Query(keys ...string) string {
if len(keys) == 0 {
return ""
}
if val, ok := c.Ctx.GetQuery(keys[0]); ok {
return val
}
if len(keys) == 2 {
return keys[1]
}
return ""
}
// PostForm 从Post里获取Key的值
func (c *Controller) PostForm(keys ...string) string {
if len(keys) == 0 {
return ""
}
if val, ok := c.Ctx.GetPostForm(keys[0]); ok {
return val
}
if len(keys) == 2 {
return keys[1]
}
return ""
}