-
Notifications
You must be signed in to change notification settings - Fork 29
/
context.go
220 lines (191 loc) · 4.34 KB
/
context.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
package service
import (
"encoding/json"
"net/http"
"github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin"
"github.com/hhandhuan/ku-bbs/pkg/config"
"github.com/hhandhuan/ku-bbs/internal/model"
)
const (
errKey = "err"
msgKey = "msg"
dataKey = "data"
flashKey = "flash"
userKey = "user"
unreadKey = "unread"
versionKey = "version"
nameKey = "name"
titleKey = "title"
)
func Context(ctx *gin.Context) *BaseContext {
stx := &BaseContext{
Ctx: ctx,
path: "/",
session: sessions.Default(ctx),
name: config.GetInstance().App.Name,
}
return stx
}
type BaseContext struct {
Ctx *gin.Context
session sessions.Session
path string
name string
title string
}
// Redirect 处理跳转
func (c *BaseContext) Redirect() {
c.Ctx.Redirect(http.StatusFound, c.path)
}
// clear 清除闪存消息
func (c *BaseContext) clear() {
c.session.Delete(errKey)
c.session.Delete(msgKey)
c.session.Delete(flashKey)
_ = c.session.Save()
}
// Back 返回上一页
func (c *BaseContext) Back() *BaseContext {
c.path = c.Ctx.Request.RequestURI
return c
}
// To 设置跳转路径
func (c *BaseContext) To(to string) *BaseContext {
c.path = to
return c
}
// WithData 闪存消息
func (c *BaseContext) WithData(data interface{}) *BaseContext {
r, _ := json.Marshal(data)
c.session.Set(flashKey, string(r))
_ = c.session.Save()
return c
}
// ParseFlash 解析闪存数据
func (c *BaseContext) ParseFlash() map[string]interface{} {
flashData := make(map[string]interface{})
if str := c.session.Get(flashKey); str != nil {
if v, ok := str.(string); ok {
_ = json.Unmarshal([]byte(v), &flashData)
}
}
return flashData
}
// WithError 错误信息跳转
func (c *BaseContext) WithError(err interface{}) *BaseContext {
errStr := ""
switch v := err.(type) {
case error:
errStr = v.Error()
case string:
errStr = v
}
c.session.Set(errKey, errStr)
_ = c.session.Save()
return c
}
// WithMsg 提示消息跳转
func (c *BaseContext) WithMsg(msg string) *BaseContext {
c.session.Set(msgKey, msg)
_ = c.session.Save()
return c
}
// SetAuth 设置授权
func (c *BaseContext) SetAuth(users model.Users) {
s, _ := json.Marshal(users)
c.session.Set(userKey, string(s))
_ = c.session.Save()
}
// Auth 获取授权
func (c *BaseContext) Auth() *model.Users {
var user *model.Users
str := c.session.Get(userKey)
if str == nil {
return user
}
if v, ok := str.(string); ok {
_ = json.Unmarshal([]byte(v), &user)
}
return user
}
// Refresh 刷新授权
func (c *BaseContext) Refresh() {
var user model.Users
model.User().Where("id", c.Auth().ID).Find(&user)
c.SetAuth(user)
}
// Check 检查授权
func (c *BaseContext) Check() bool {
user := c.Auth()
if user == nil {
return false
} else {
return user.ID > 0
}
}
// IsAdmin 检查授权
func (c *BaseContext) IsAdmin() bool {
user := c.Auth()
if user == nil {
return false
} else {
return user.IsAdmin > 0
}
}
// Forget 清除授权
func (c *BaseContext) Forget() {
c.session.Delete(userKey)
_ = c.session.Save()
}
// SetTitle 设置模版标题
func (c *BaseContext) SetTitle(title string) *BaseContext {
c.title = title
return c
}
// unread 消息未读数
func (c *BaseContext) unread() bool {
if !c.Check() {
return false
}
var (
remind *model.Reminds
notice *model.SystemUserNotices
)
UID := c.Auth().ID
// 提醒消息
r := model.Remind().Where("receiver", UID).Where("readed_at is null").Find(&remind)
if r.Error == nil && r.RowsAffected > 0 {
return true
}
// 未读系统消息
s := model.SystemUserNotice().Where("user_id", UID).Where("readed_at is null").Find(¬ice)
if s.Error == nil && s.RowsAffected > 0 {
return true
}
return false
}
// View 模版返回
func (c *BaseContext) View(tpl string, data interface{}) {
obj := gin.H{
versionKey: config.GetInstance().App.Version,
errKey: c.session.Get(errKey),
msgKey: c.session.Get(msgKey),
userKey: c.Auth(),
unreadKey: c.unread(),
dataKey: data,
flashKey: c.ParseFlash(),
nameKey: c.name,
titleKey: c.title,
}
c.clear()
c.Ctx.HTML(http.StatusOK, tpl, obj)
}
// Json 通用 JSON 响应
func (c *BaseContext) Json(data interface{}) {
c.Ctx.JSON(http.StatusOK, data)
}
// MDFileJson markdown 上传图片响应
func (c *BaseContext) MDFileJson(ok int, msg, url string) {
c.Json(gin.H{"success": ok, "message": msg, "url": url})
}