mindoc/controllers/base.go

121 lines
2.9 KiB
Go
Raw Normal View History

2017-04-19 18:19:27 +08:00
package controllers
2017-04-21 18:20:35 +08:00
import (
"bytes"
2017-06-14 09:23:29 +08:00
"github.com/lifei6671/mindoc/models"
"github.com/lifei6671/mindoc/conf"
2017-04-21 18:20:35 +08:00
"github.com/astaxie/beego"
"strings"
2017-05-03 14:22:05 +08:00
"encoding/json"
"io"
2017-04-21 18:20:35 +08:00
)
2017-04-19 18:19:27 +08:00
type BaseController struct {
beego.Controller
2017-04-21 18:20:35 +08:00
Member *models.Member
Option map[string]string
EnableAnonymous bool
2017-05-20 15:27:03 +08:00
EnableDocumentHistory bool
2017-04-21 18:20:35 +08:00
}
// Prepare 预处理.
func (c *BaseController) Prepare (){
c.Data["SiteName"] = "MinDoc"
c.Data["Member"] = models.Member{}
c.EnableAnonymous = false
2017-05-20 15:27:03 +08:00
c.EnableDocumentHistory = false
2017-04-21 18:20:35 +08:00
if member,ok := c.GetSession(conf.LoginSessionName).(models.Member); ok && member.MemberId > 0{
2017-04-21 18:20:35 +08:00
c.Member = &member
c.Data["Member"] = c.Member
}else{
//c.Member = models.NewMember()
//c.Member.Find(1)
//c.Data["Member"] = *c.Member
2017-04-21 18:20:35 +08:00
}
c.Data["BaseUrl"] = c.Ctx.Input.Scheme() + "://" + c.Ctx.Request.Host
if options,err := models.NewOption().All();err == nil {
c.Option = make(map[string]string,len(options))
2017-04-21 18:20:35 +08:00
for _,item := range options {
c.Data[item.OptionName] = item.OptionValue
c.Option[item.OptionName] = item.OptionValue
if strings.EqualFold(item.OptionName,"ENABLE_ANONYMOUS") && item.OptionValue == "true" {
c.EnableAnonymous = true
}
2017-05-20 15:27:03 +08:00
if strings.EqualFold(item.OptionName,"ENABLE_DOCUMENT_HISTORY") && item.OptionValue == "true" {
c.EnableDocumentHistory = true
}
2017-04-21 18:20:35 +08:00
}
}
}
// SetMember 获取或设置当前登录用户信息,如果 MemberId 小于 0 则标识删除 Session
func (c *BaseController) SetMember(member models.Member) {
if member.MemberId <= 0 {
c.DelSession(conf.LoginSessionName)
c.DelSession("uid")
c.DestroySession()
} else {
c.SetSession(conf.LoginSessionName, member)
c.SetSession("uid", member.MemberId)
}
}
// JsonResult 响应 json 结果
func (c *BaseController) JsonResult(errCode int,errMsg string,data ...interface{}){
2017-05-03 14:22:05 +08:00
jsonData := make(map[string]interface{},3)
2017-04-21 18:20:35 +08:00
2017-05-03 14:22:05 +08:00
jsonData["errcode"] = errCode
jsonData["message"] = errMsg
2017-04-21 18:20:35 +08:00
if len(data) > 0 && data[0] != nil{
2017-05-03 14:22:05 +08:00
jsonData["data"] = data[0]
2017-04-21 18:20:35 +08:00
}
2017-05-03 14:22:05 +08:00
returnJSON, err := json.Marshal(jsonData)
if err != nil {
beego.Error(err)
}
c.Ctx.ResponseWriter.Header().Set("Content-Type", "application/json; charset=utf-8")
io.WriteString(c.Ctx.ResponseWriter,string(returnJSON))
2017-04-21 18:20:35 +08:00
c.StopRun()
2017-04-19 18:19:27 +08:00
}
2017-04-21 18:20:35 +08:00
// ExecuteViewPathTemplate 执行指定的模板并返回执行结果.
func (c *BaseController) ExecuteViewPathTemplate(tplName string,data interface{}) (string,error){
var buf bytes.Buffer
viewPath := c.ViewPath
if c.ViewPath == "" {
viewPath = beego.BConfig.WebConfig.ViewsPath
}
if err := beego.ExecuteViewPathTemplate(&buf,tplName,viewPath,data); err != nil {
return "",err
}
return buf.String(),nil
}
func (c *BaseController) BaseUrl() string {
return c.Ctx.Input.Scheme() + "://" + c.Ctx.Request.Host
}
//显示错误信息页面.
func (c *BaseController) ShowErrorPage(errCode int,errMsg string) {
c.TplName = "errors/error.tpl"
c.Data["ErrorMessage"] = errMsg
c.Data["ErrorCode"] = errCode
c.StopRun()
}