2017-04-19 18:19:27 +08:00
|
|
|
|
package controllers
|
|
|
|
|
|
2017-04-21 18:20:35 +08:00
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
2017-05-03 14:22:05 +08:00
|
|
|
|
"encoding/json"
|
2018-03-09 10:55:50 +08:00
|
|
|
|
"io"
|
|
|
|
|
"strings"
|
|
|
|
|
"time"
|
|
|
|
|
|
2021-04-21 12:22:34 +08:00
|
|
|
|
"html/template"
|
|
|
|
|
"io/ioutil"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
|
2021-03-26 14:26:19 +08:00
|
|
|
|
"github.com/beego/beego/v2/core/logs"
|
2021-03-23 21:55:50 +08:00
|
|
|
|
"github.com/beego/beego/v2/server/web"
|
2021-04-21 20:44:19 +08:00
|
|
|
|
"github.com/beego/i18n"
|
2021-03-29 18:41:10 +08:00
|
|
|
|
"github.com/mindoc-org/mindoc/conf"
|
|
|
|
|
"github.com/mindoc-org/mindoc/models"
|
|
|
|
|
"github.com/mindoc-org/mindoc/utils"
|
2017-04-21 18:20:35 +08:00
|
|
|
|
)
|
|
|
|
|
|
2017-04-19 18:19:27 +08:00
|
|
|
|
type BaseController struct {
|
2021-03-23 21:55:50 +08:00
|
|
|
|
web.Controller
|
2018-01-26 17:17:38 +08:00
|
|
|
|
Member *models.Member
|
|
|
|
|
Option map[string]string
|
|
|
|
|
EnableAnonymous bool
|
2017-05-20 15:27:03 +08:00
|
|
|
|
EnableDocumentHistory bool
|
2021-03-30 16:18:02 +08:00
|
|
|
|
Lang string
|
2017-04-21 18:20:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-03-09 10:55:50 +08:00
|
|
|
|
type CookieRemember struct {
|
|
|
|
|
MemberId int
|
|
|
|
|
Account string
|
|
|
|
|
Time time.Time
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-21 18:20:35 +08:00
|
|
|
|
// Prepare 预处理.
|
2018-01-26 17:17:38 +08:00
|
|
|
|
func (c *BaseController) Prepare() {
|
2017-04-21 18:20:35 +08:00
|
|
|
|
c.Data["SiteName"] = "MinDoc"
|
2018-03-09 10:55:50 +08:00
|
|
|
|
c.Data["Member"] = models.NewMember()
|
|
|
|
|
controller, action := c.GetControllerAndAction()
|
2018-02-28 16:43:19 +08:00
|
|
|
|
|
|
|
|
|
c.Data["ActionName"] = action
|
|
|
|
|
c.Data["ControllerName"] = controller
|
|
|
|
|
|
2017-04-30 22:13:12 +08:00
|
|
|
|
c.EnableAnonymous = false
|
2017-05-20 15:27:03 +08:00
|
|
|
|
c.EnableDocumentHistory = false
|
2017-04-21 18:20:35 +08:00
|
|
|
|
|
2018-01-26 17:17:38 +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
|
2018-01-26 17:17:38 +08:00
|
|
|
|
} else {
|
2018-03-09 10:55:50 +08:00
|
|
|
|
var remember CookieRemember
|
|
|
|
|
// //如果Cookie中存在登录信息,从cookie中获取用户信息
|
|
|
|
|
if cookie, ok := c.GetSecureCookie(conf.GetAppKey(), "login"); ok {
|
|
|
|
|
if err := utils.Decode(cookie, &remember); err == nil {
|
|
|
|
|
if member, err := models.NewMember().Find(remember.MemberId); err == nil {
|
|
|
|
|
c.Member = member
|
|
|
|
|
c.Data["Member"] = member
|
|
|
|
|
c.SetMember(*member)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-04-21 18:20:35 +08:00
|
|
|
|
}
|
2018-01-26 17:17:38 +08:00
|
|
|
|
conf.BaseUrl = c.BaseUrl()
|
|
|
|
|
c.Data["BaseUrl"] = c.BaseUrl()
|
2017-04-21 18:20:35 +08:00
|
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
|
if options, err := models.NewOption().All(); err == nil {
|
|
|
|
|
c.Option = make(map[string]string, len(options))
|
|
|
|
|
for _, item := range options {
|
2017-04-21 18:20:35 +08:00
|
|
|
|
c.Data[item.OptionName] = item.OptionValue
|
2018-01-27 14:32:10 +08:00
|
|
|
|
c.Option[item.OptionName] = item.OptionValue
|
2017-04-21 18:20:35 +08:00
|
|
|
|
}
|
2018-09-19 11:32:29 +08:00
|
|
|
|
c.EnableAnonymous = strings.EqualFold(c.Option["ENABLE_ANONYMOUS"], "true")
|
2018-09-19 16:12:32 +08:00
|
|
|
|
c.EnableDocumentHistory = strings.EqualFold(c.Option["ENABLE_DOCUMENT_HISTORY"], "true")
|
2017-04-21 18:20:35 +08:00
|
|
|
|
}
|
2021-03-25 10:42:18 +08:00
|
|
|
|
c.Data["HighlightStyle"] = web.AppConfig.DefaultString("highlight_style", "github")
|
2018-09-11 15:57:03 +08:00
|
|
|
|
|
2021-03-23 21:55:50 +08:00
|
|
|
|
if b, err := ioutil.ReadFile(filepath.Join(web.BConfig.WebConfig.ViewsPath, "widgets", "scripts.tpl")); err == nil {
|
2018-09-19 16:13:38 +08:00
|
|
|
|
c.Data["Scripts"] = template.HTML(string(b))
|
2018-09-11 15:57:03 +08:00
|
|
|
|
}
|
2018-09-19 16:12:32 +08:00
|
|
|
|
|
2021-03-30 16:18:02 +08:00
|
|
|
|
c.SetLang()
|
2017-04-21 18:20:35 +08:00
|
|
|
|
}
|
2021-03-29 17:56:36 +08:00
|
|
|
|
|
2018-11-14 15:57:55 +08:00
|
|
|
|
//判断用户是否登录.
|
2021-03-29 17:56:36 +08:00
|
|
|
|
func (c *BaseController) isUserLoggedIn() bool {
|
2018-11-14 15:57:55 +08:00
|
|
|
|
return c.Member != nil && c.Member.MemberId > 0
|
|
|
|
|
}
|
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 结果
|
2018-01-26 17:17:38 +08:00
|
|
|
|
func (c *BaseController) JsonResult(errCode int, errMsg string, data ...interface{}) {
|
|
|
|
|
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
|
|
|
|
|
2018-01-26 17:17:38 +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 {
|
2021-03-30 16:18:02 +08:00
|
|
|
|
logs.Error(err)
|
2017-05-03 14:22:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.Ctx.ResponseWriter.Header().Set("Content-Type", "application/json; charset=utf-8")
|
2018-02-06 11:27:34 +08:00
|
|
|
|
c.Ctx.ResponseWriter.Header().Set("Cache-Control", "no-cache, no-store")
|
2021-03-30 16:18:02 +08:00
|
|
|
|
_, err = io.WriteString(c.Ctx.ResponseWriter, string(returnJSON))
|
|
|
|
|
if err != nil {
|
|
|
|
|
logs.Error(err)
|
|
|
|
|
}
|
2017-05-03 14:22:05 +08:00
|
|
|
|
|
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
|
|
|
|
|
2018-11-05 18:50:01 +08:00
|
|
|
|
//如果错误不为空,则响应错误信息到浏览器.
|
2021-03-29 17:56:36 +08:00
|
|
|
|
func (c *BaseController) CheckJsonError(code int, err error) {
|
2018-11-05 18:50:01 +08:00
|
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
jsonData := make(map[string]interface{}, 3)
|
|
|
|
|
|
|
|
|
|
jsonData["errcode"] = code
|
|
|
|
|
jsonData["message"] = err.Error()
|
|
|
|
|
|
|
|
|
|
returnJSON, err := json.Marshal(jsonData)
|
|
|
|
|
if err != nil {
|
2021-03-30 16:18:02 +08:00
|
|
|
|
logs.Error(err)
|
2018-11-05 18:50:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.Ctx.ResponseWriter.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
|
|
|
c.Ctx.ResponseWriter.Header().Set("Cache-Control", "no-cache, no-store")
|
2021-03-30 16:18:02 +08:00
|
|
|
|
_, err = io.WriteString(c.Ctx.ResponseWriter, string(returnJSON))
|
|
|
|
|
if err != nil {
|
|
|
|
|
logs.Error(err)
|
|
|
|
|
}
|
2018-11-05 18:50:01 +08:00
|
|
|
|
|
|
|
|
|
c.StopRun()
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-21 18:20:35 +08:00
|
|
|
|
// ExecuteViewPathTemplate 执行指定的模板并返回执行结果.
|
2018-01-26 17:17:38 +08:00
|
|
|
|
func (c *BaseController) ExecuteViewPathTemplate(tplName string, data interface{}) (string, error) {
|
2017-04-21 18:20:35 +08:00
|
|
|
|
var buf bytes.Buffer
|
|
|
|
|
|
|
|
|
|
viewPath := c.ViewPath
|
|
|
|
|
|
|
|
|
|
if c.ViewPath == "" {
|
2021-03-23 21:55:50 +08:00
|
|
|
|
viewPath = web.BConfig.WebConfig.ViewsPath
|
2017-04-21 18:20:35 +08:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-26 15:37:19 +08:00
|
|
|
|
if err := web.ExecuteViewPathTemplate(&buf, tplName, viewPath, data); err != nil {
|
2018-01-26 17:17:38 +08:00
|
|
|
|
return "", err
|
2017-04-21 18:20:35 +08:00
|
|
|
|
}
|
2018-01-26 17:17:38 +08:00
|
|
|
|
return buf.String(), nil
|
2017-04-21 18:20:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-04-23 20:33:21 +08:00
|
|
|
|
func (c *BaseController) BaseUrl() string {
|
2021-03-25 10:42:18 +08:00
|
|
|
|
baseUrl := web.AppConfig.DefaultString("baseurl", "")
|
2018-01-25 19:18:59 +08:00
|
|
|
|
if baseUrl != "" {
|
2018-01-26 17:17:38 +08:00
|
|
|
|
if strings.HasSuffix(baseUrl, "/") {
|
|
|
|
|
baseUrl = strings.TrimSuffix(baseUrl, "/")
|
2018-01-25 19:18:59 +08:00
|
|
|
|
}
|
2018-01-26 17:17:38 +08:00
|
|
|
|
} else {
|
2018-01-25 19:18:59 +08:00
|
|
|
|
baseUrl = c.Ctx.Input.Scheme() + "://" + c.Ctx.Request.Host
|
|
|
|
|
}
|
|
|
|
|
return baseUrl
|
2017-04-23 20:33:21 +08:00
|
|
|
|
}
|
2017-06-12 17:58:45 +08:00
|
|
|
|
|
|
|
|
|
//显示错误信息页面.
|
2018-01-26 17:17:38 +08:00
|
|
|
|
func (c *BaseController) ShowErrorPage(errCode int, errMsg string) {
|
2017-06-12 17:58:45 +08:00
|
|
|
|
c.TplName = "errors/error.tpl"
|
2018-02-28 15:47:00 +08:00
|
|
|
|
|
2017-06-12 17:58:45 +08:00
|
|
|
|
c.Data["ErrorMessage"] = errMsg
|
|
|
|
|
c.Data["ErrorCode"] = errCode
|
2018-02-28 15:47:00 +08:00
|
|
|
|
|
|
|
|
|
var buf bytes.Buffer
|
2022-07-05 11:59:23 +08:00
|
|
|
|
exeData := map[string]interface{}{"ErrorMessage": errMsg, "ErrorCode": errCode, "BaseUrl": conf.BaseUrl, "Lang": c.Lang}
|
2018-02-28 15:47:00 +08:00
|
|
|
|
|
2022-07-05 11:59:23 +08:00
|
|
|
|
if err := web.ExecuteViewPathTemplate(&buf, "errors/error.tpl", web.BConfig.WebConfig.ViewsPath, exeData); err != nil {
|
2018-02-28 15:47:00 +08:00
|
|
|
|
c.Abort("500")
|
|
|
|
|
}
|
2018-07-18 17:10:24 +08:00
|
|
|
|
if errCode >= 200 && errCode <= 510 {
|
|
|
|
|
c.CustomAbort(errCode, buf.String())
|
2018-11-05 18:50:01 +08:00
|
|
|
|
} else {
|
2018-08-17 18:05:04 +08:00
|
|
|
|
c.CustomAbort(500, buf.String())
|
2018-07-18 17:10:24 +08:00
|
|
|
|
}
|
2018-01-26 17:17:38 +08:00
|
|
|
|
}
|
2018-11-05 18:50:01 +08:00
|
|
|
|
|
2021-03-29 17:56:36 +08:00
|
|
|
|
func (c *BaseController) CheckErrorResult(code int, err error) {
|
2018-11-05 18:50:01 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
c.ShowErrorPage(code, err.Error())
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-30 16:18:02 +08:00
|
|
|
|
|
|
|
|
|
func (c *BaseController) SetLang() {
|
|
|
|
|
hasCookie := false
|
2021-04-21 20:44:19 +08:00
|
|
|
|
lang := c.GetString("lang")
|
2021-03-30 16:18:02 +08:00
|
|
|
|
if len(lang) == 0 {
|
|
|
|
|
lang = c.Ctx.GetCookie("lang")
|
|
|
|
|
hasCookie = true
|
|
|
|
|
}
|
|
|
|
|
if len(lang) == 0 ||
|
|
|
|
|
!i18n.IsExist(lang) {
|
2021-04-21 20:44:19 +08:00
|
|
|
|
lang, _ = web.AppConfig.String("default_lang")
|
2021-03-30 16:18:02 +08:00
|
|
|
|
}
|
|
|
|
|
if !hasCookie {
|
|
|
|
|
c.Ctx.SetCookie("lang", lang, 1<<31-1, "/")
|
|
|
|
|
}
|
|
|
|
|
c.Data["Lang"] = lang
|
|
|
|
|
c.Lang = lang
|
|
|
|
|
}
|