forked from leanote/leanote
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseController.go
More file actions
250 lines (222 loc) · 5.64 KB
/
Copy pathBaseController.go
File metadata and controls
250 lines (222 loc) · 5.64 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
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
package controllers
import (
"github.com/revel/revel"
"gopkg.in/mgo.v2/bson"
"encoding/json"
"github.com/leanote/leanote/app/info"
// . "github.com/leanote/leanote/app/lea"
// "io/ioutil"
// "fmt"
"math"
"strconv"
"strings"
"bytes"
)
// 公用Controller, 其它Controller继承它
type BaseController struct {
*revel.Controller
}
func (c BaseController) GetUserId() string {
if userId, ok := c.Session["UserId"]; ok {
return userId
}
return ""
}
// 是否已登录
func (c BaseController) HasLogined() bool {
return c.GetUserId() != ""
}
func (c BaseController) GetObjectUserId() bson.ObjectId {
userId := c.GetUserId()
if userId != "" {
return bson.ObjectIdHex(userId)
}
return ""
}
func (c BaseController) GetEmail() string {
if email, ok := c.Session["Email"]; ok {
return email
}
return ""
}
func (c BaseController) GetUsername() string {
if email, ok := c.Session["Username"]; ok {
return email
}
return ""
}
// 得到用户信息
func (c BaseController) GetUserInfo() info.User {
if userId, ok := c.Session["UserId"]; ok && userId != "" {
return userService.GetUserInfo(userId);
/*
notebookWidth, _ := strconv.Atoi(c.Session["NotebookWidth"])
noteListWidth, _ := strconv.Atoi(c.Session["NoteListWidth"])
mdEditorWidth, _ := strconv.Atoi(c.Session["MdEditorWidth"])
LogJ(c.Session)
user := info.User{UserId: bson.ObjectIdHex(userId),
Email: c.Session["Email"],
Logo: c.Session["Logo"],
Username: c.Session["Username"],
UsernameRaw: c.Session["UsernameRaw"],
Theme: c.Session["Theme"],
NotebookWidth: notebookWidth,
NoteListWidth: noteListWidth,
MdEditorWidth: mdEditorWidth,
}
if c.Session["Verified"] == "1" {
user.Verified = true
}
if c.Session["LeftIsMin"] == "1" {
user.LeftIsMin = true
}
return user
*/
}
return info.User{}
}
// 这里的session都是cookie中的, 与数据库session无关
func (c BaseController) GetSession(key string) string {
v, ok := c.Session[key]
if !ok {
v = ""
}
return v
}
func (c BaseController) SetSession(userInfo info.User) {
if userInfo.UserId.Hex() != "" {
c.Session["UserId"] = userInfo.UserId.Hex()
c.Session["Email"] = userInfo.Email
c.Session["Username"] = userInfo.Username
c.Session["UsernameRaw"] = userInfo.UsernameRaw
c.Session["Theme"] = userInfo.Theme
c.Session["Logo"] = userInfo.Logo
c.Session["NotebookWidth"] = strconv.Itoa(userInfo.NotebookWidth)
c.Session["NoteListWidth"] = strconv.Itoa(userInfo.NoteListWidth)
if userInfo.Verified {
c.Session["Verified"] = "1"
} else {
c.Session["Verified"] = "0"
}
if userInfo.LeftIsMin {
c.Session["LeftIsMin"] = "1"
} else {
c.Session["LeftIsMin"] = "0"
}
}
}
func (c BaseController) ClearSession() {
delete(c.Session, "UserId")
delete(c.Session, "Email")
delete(c.Session, "Username")
delete(c.Session, "theme")
}
// 修改session
func (c BaseController) UpdateSession(key, value string) {
c.Session[key] = value
}
// 返回json
func (c BaseController) Json(i interface{}) string {
// b, _ := json.MarshalIndent(i, "", " ")
b, _ := json.Marshal(i)
return string(b)
}
// 得到第几页
func (c BaseController) GetPage() int {
page := 0
c.Params.Bind(&page, "page")
if page == 0 {
return 1
}
return page
}
// 判断是否含有某参数
func (c BaseController) Has(key string) bool {
if _, ok := c.Params.Values[key]; ok {
return true;
}
return false;
}
/*
func (c Blog) GetPage(page, count int, list interface{}) info.Page {
return info.Page{page, int(math.Ceil(float64(count)/float64(page))), list}
}
*/
func (c BaseController) GetTotalPage(page, count int) int {
return int(math.Ceil(float64(count)/float64(page)))
}
//-------------
func (c BaseController) E404() revel.Result {
c.RenderArgs["title"] = "404";
return c.NotFound("", nil)
}
// 设置本地
func (c BaseController) SetLocale() string {
locale := string(c.Request.Locale) // zh-CN
lang := locale
if strings.Contains(locale, "-") {
pos := strings.Index(locale, "-")
lang = locale[0:pos]
}
if lang != "zh" && lang != "en" {
lang = "en";
}
c.RenderArgs["locale"] = lang;
c.RenderArgs["siteUrl"] = configService.GetSiteUrl();
c.RenderArgs["blogUrl"] = configService.GetBlogUrl()
c.RenderArgs["leaUrl"] = configService.GetLeaUrl()
c.RenderArgs["noteUrl"] = configService.GetNoteUrl()
return lang
}
// 设置userInfo
func (c BaseController) SetUserInfo() info.User {
userInfo := c.GetUserInfo()
c.RenderArgs["userInfo"] = userInfo
if(userInfo.Username == configService.GetAdminUsername()) {
c.RenderArgs["isAdmin"] = true
}
return userInfo
}
// life
// 返回解析后的字符串, 只是为了解析模板得到字符串
func (c BaseController) RenderTemplateStr(templatePath string) string {
// Get the Template.
// 返回 GoTemplate{tmpl, loader}
template, err := revel.MainTemplateLoader.Template(templatePath)
if err != nil {
}
tpl := &revel.RenderTemplateResult{
Template: template,
RenderArgs: c.RenderArgs, // 把args给它
}
var buffer bytes.Buffer
tpl.Template.Render(&buffer, c.RenderArgs)
return buffer.String();
}
// json, result
// 为了msg
// msg-v1-v2-v3
func (c BaseController) RenderRe(re info.Re) revel.Result {
oldMsg := re.Msg
if re.Msg != "" {
if(strings.Contains(re.Msg, "-")) {
msgAndValues := strings.Split(re.Msg, "-")
if len(msgAndValues) == 2 {
re.Msg = c.Message(msgAndValues[0], msgAndValues[1])
} else {
others := msgAndValues[0:]
a := make([]interface{}, len(others))
for i, v := range others {
a[i] = v
}
re.Msg = c.Message(msgAndValues[0], a...)
}
} else {
re.Msg = c.Message(re.Msg)
}
}
if strings.HasPrefix(re.Msg, "???") {
re.Msg = oldMsg
}
return c.RenderJson(re)
}