-
Notifications
You must be signed in to change notification settings - Fork 1
/
WordsController.go
99 lines (90 loc) · 2.68 KB
/
WordsController.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
package controllers
import (
"GRE3000/base/cache"
"GRE3000/const_conf"
"GRE3000/filters"
"GRE3000/models"
"github.com/astaxie/beego"
"strconv"
"time"
)
type WordsController struct {
beego.Controller
}
func (c *WordsController) Index() {
var rawWordsList []*models.WordsList
var userWordsList []*const_conf.RawWord
isLogin, UserInfo := filters.IsLogin(c.Controller.Ctx)
c.Data["IsLogin"], c.Data["UserInfo"] = isLogin, UserInfo
needRandom, _ := c.GetBool("RandomSort")
if isLogin && !needRandom {
userWordsList = models.LoadWordsListForUser(&UserInfo)
} else if !needRandom {
rawWordsList = models.LoadRawWords()
}
if isLogin {
c.Data["PageTitle"] = UserInfo.Username + "同学的单词表"
} else {
c.Data["PageTitle"] = "GRE单词表"
}
c.Data["IsWordsPage"] = true
c.Data["RawWords"] = &rawWordsList
c.Data["UserWords"] = &userWordsList
c.Data["ShowMeans"], _ = c.GetBool("ShowMeans")
c.Data["RandomSort"] = needRandom
c.Layout = "layout/layout.tpl"
c.TplName = "words/vocabulary.tpl"
}
func (c *WordsController) LoadWordsJson() {
needRandom, _ := c.GetBool("RandomSort")
isLogin, UserInfo := filters.IsLogin(c.Controller.Ctx)
if isLogin {
c.Data["json"] = models.LoadUserWordsJson(&UserInfo, needRandom)
} else {
c.Data["json"] = models.LoadRawWordsJson(needRandom)
}
c.ServeJSON()
}
func (c *WordsController) IncrMark() {
ErrCode := -1
id := c.Ctx.Input.Param(":id")
token, flag := c.GetSecureCookie(const_conf.CookieSecure, const_conf.WebCookieName)
userWordId, err := strconv.Atoi(id)
if flag && !cache.Redis.IsExist(token+id) && err == nil && userWordId > 0 {
isLogin, UserInfo := filters.IsLogin(c.Controller.Ctx)
if isLogin {
userWord, ok := models.FindUserWordByWordId(&UserInfo, userWordId)
if ok {
go models.IncrWordMark(userWord, &UserInfo)
go cache.Redis.Put(token+id, UserInfo.Username, time.Duration(const_conf.MarkWordTimeLimit)*time.Minute)
ErrCode = 0
}
}
}
c.Data["json"] = map[string]int{"ErrCode": ErrCode}
c.ServeJSON()
}
func (c *WordsController) DeleteWord() {
ErrCode := -1
id := c.Ctx.Input.Param(":id")
userWordId, err := strconv.Atoi(id)
if err == nil && userWordId > 0 {
isLogin, UserInfo := filters.IsLogin(c.Controller.Ctx)
if isLogin {
go models.DeleteWord(&UserInfo, userWordId)
ErrCode = 0
}
}
c.Data["json"] = map[string]int{"ErrCode": ErrCode}
c.ServeJSON()
}
func (c *WordsController) Statistics() {
isLogin, UserInfo := filters.IsLogin(c.Controller.Ctx)
var countMarked, countAll int64
if isLogin {
countMarked = models.CountOfMarkedWords(&UserInfo)
countAll = models.CountOfUserWords(&UserInfo)
}
c.Data["json"] = map[string]int64{"Marked": countMarked, "All": countAll}
c.ServeJSON()
}