-
Notifications
You must be signed in to change notification settings - Fork 738
/
BaseController.go
163 lines (146 loc) · 4.15 KB
/
BaseController.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
package AdminControllers
import (
"strings"
"github.com/TruthHun/DocHub/models"
"time"
"fmt"
"github.com/TruthHun/DocHub/helper"
"github.com/astaxie/beego"
)
type BaseController struct {
beego.Controller
TplTheme string //模板主题
TplStatic string //模板静态文件
AdminId int //管理员是否已登录,如果已登录,则管理员ID大于0
Sys models.Sys
}
//初始化函数
func (this *BaseController) Prepare() {
var ok bool
this.Sys, _ = models.NewSys().Get()
//检测是否已登录,未登录则跳转到登录页
AdminId := this.GetSession("AdminId")
this.AdminId, ok = AdminId.(int)
this.Data["Admin"], _ = models.NewAdmin().GetById(this.AdminId)
if !ok || this.AdminId == 0 {
this.Redirect("/admin/login", 302)
return
}
version := helper.VERSION
if helper.Debug {
version = fmt.Sprintf("%v.%v", version, time.Now().Unix())
}
this.Data["Version"] = version
//后台关闭XSRF功能
this.EnableXSRF = false
ctrl, _ := this.GetControllerAndAction()
ctrl = strings.TrimSuffix(ctrl, "Controller")
//设置默认模板
this.TplTheme = "default"
this.TplPrefix = "Admin/" + this.TplTheme + "/" + ctrl + "/"
this.Layout = "Admin/" + this.TplTheme + "/layout.html"
//当前模板静态文件
this.Data["TplStatic"] = "/static/Admin/" + this.TplTheme
//this.Data["PreviewDomain"] = beego.AppConfig.String("oss::PreviewUrl")
if cs, err := models.NewCloudStore(false); err == nil {
this.Data["PreviewDomain"] = cs.GetPublicDomain()
} else {
helper.Logger.Error(err.Error())
this.Data["PreviewDomain"] = ""
}
this.Data["Sys"] = this.Sys
this.Data["Title"] = "文库系统管理后台"
this.Data["Lang"] = "zh-CN"
}
//自定义的文档错误
func (this *BaseController) ErrorDiy(status, redirect, msg interface{}, timewait int) {
this.Data["status"] = status
this.Data["redirect"] = redirect
this.Data["msg"] = msg
this.Data["timewait"] = timewait
this.TplName = "error_diy.html"
}
//是否已经登录,如果已登录,则返回用户的id
func (this *BaseController) CheckLogin() int {
uid := this.GetSession("uid")
if uid != nil {
id, ok := uid.(int)
if ok && id > 0 {
return id
}
}
return 0
}
//404
func (this *BaseController) Error404() {
this.Layout = ""
this.Data["content"] = "Page Not Foud"
this.Data["code"] = "404"
this.Data["content_zh"] = "页面被外星人带走了"
this.TplName = "error.html"
}
//501
func (this *BaseController) Error501() {
this.Layout = ""
this.Data["code"] = "501"
this.Data["content"] = "Server Error"
this.Data["content_zh"] = "服务器被外星人戳炸了"
this.TplName = "error.html"
}
//数据库错误
func (this *BaseController) ErrorDb() {
this.Layout = ""
this.Data["content"] = "Database is now down"
this.Data["content_zh"] = "数据库被外星人抢走了"
this.TplName = "error.html"
}
//更新内容
func (this *BaseController) Update() {
id := strings.Split(this.GetString("id"), ",")
i, err := models.UpdateByIds(this.GetString("table"), this.GetString("field"), this.GetString("value"), id)
ret := map[string]interface{}{"status": 0, "msg": "更新失败,可能您未对内容作更改"}
if i > 0 && err == nil {
ret["status"] = 1
ret["msg"] = "更新成功"
}
if err != nil {
ret["msg"] = err.Error()
}
this.Data["json"] = ret
this.ServeJSON()
}
//删除内容
func (this *BaseController) Del() {
id := strings.Split(this.GetString("id"), ",")
i, err := models.DelByIds(this.GetString("table"), id)
ret := map[string]interface{}{"status": 0, "msg": "删除失败,可能您要删除的内容已经不存在"}
if i > 0 && err == nil {
ret["status"] = 1
ret["msg"] = "删除成功"
}
if err != nil {
ret["msg"] = err.Error()
}
this.Data["json"] = ret
this.ServeJSON()
}
//响应json
func (this *BaseController) ResponseJson(isSuccess bool, msg string, data ...interface{}) {
status := 0
if isSuccess {
status = 1
}
ret := map[string]interface{}{"status": status, "msg": msg}
if len(data) > 0 {
ret["data"] = data[0]
}
this.Data["json"] = ret
this.ServeJSON()
this.StopRun()
}
//响应json
func (this *BaseController) Response(data map[string]interface{}) {
this.Data["json"] = data
this.ServeJSON()
this.StopRun()
}