forked from UlricQin/beego-blog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
base_controller.go
102 lines (90 loc) · 2.42 KB
/
base_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
package controllers
import (
"github.com/astaxie/beego"
"github.com/ulricqin/goutils/paginator"
"github.com/vincentruan/beego-blog/g"
"github.com/vincentruan/beego-blog/models/admin"
"strconv"
)
type Checker interface {
CheckLogin()
}
type BaseController struct {
beego.Controller
IsAdmin bool
}
func (this *BaseController) Prepare() {
this.Data["BlogLogo"] = g.BlogLogo
this.Data["BlogTitle"] = g.BlogTitle
this.Data["BlogResume"] = g.BlogResume
this.AssignIsAdmin()
if app, ok := this.AppController.(Checker); ok {
app.CheckLogin()
}
}
func (this *BaseController) AssignIsAdmin() {
if sess := this.GetSession("username"); sess != nil {
beego.BeeLogger.Debug("Retrive user name [%s] from session", sess.(string))
this.IsAdmin = true
} else {
//从cookie中获取
bb_name := this.Ctx.GetCookie("bb_name")
bb_password := this.Ctx.GetCookie("bb_password")
if bb_name == "" || bb_password == "" {
this.IsAdmin = false
} else {
this.IsAdmin = admin.CheckAdminUser(bb_name, bb_password, "host")
if this.IsAdmin {
this.SetSession("username", bb_name)
beego.BeeLogger.Debug("Put user name [%s] into session", bb_name)
}
}
}
beego.BeeLogger.Debug("Is Admin [%t]", this.IsAdmin)
this.Data["IsAdmin"] = this.IsAdmin
}
func (this *BaseController) SetPaginator(per int, nums int64) *paginator.Paginator {
p := paginator.NewPaginator(this.Ctx.Request, per, nums)
this.Data["paginator"] = p
return p
}
func (this *BaseController) GetIntWithDefault(paramKey string, defaultVal int) int {
valStr := this.GetString(paramKey)
var val int
if valStr == "" {
val = defaultVal
} else {
var err error
val, err = strconv.Atoi(valStr)
if err != nil {
val = defaultVal
}
}
return val
}
func (this *BaseController) JsStorage(action, key string, values ...string) {
value := action + ":::" + key
if len(values) > 0 {
value += ":::" + values[0]
}
this.Ctx.SetCookie("JsStorage", value, 1<<31-1, "/")
}
//返回分页参数
func (this *BaseController) GetPaginationParam() (limit, offset int) {
var err error
limit, err = this.GetInt("limit")
if err != nil {
beego.Info("Unable to format limit from parameters, use -1 as default!", err)
limit = -1
}
offset, err = this.GetInt("offset")
if err != nil {
beego.Info("Unable to format offset from parameters, use 0 as default!", err)
offset = 0
}
if limit == 0 {
beego.Info("limit 0 is illegal, change to -1 as default!")
limit = -1
}
return
}