beegoinit/mvc/controller.go

63 line
1.2 KiB
Go

2020-07-02 15:59:04 +08:00
package mvc
import (
"github.com/astaxie/beego"
2020-07-10 10:51:40 +08:00
"golib.gaore.com/GaoreGo/beegoinit/response"
2020-07-02 15:59:04 +08:00
"strings"
)
type NestInterface interface {
NestPrepare()
}
type BaseController struct {
beego.Controller
}
func (this *BaseController) Prepare() {
sep := "/"
s := this.GetRouterSlice()
this.ViewPath = "applications/" + s[0] + sep + beego.AppConfig.String("customviewdir") + sep
this.TplName = s[1] + sep + s[2] + ".tpl"
if app, ok := this.AppController.(NestInterface); ok {
app.NestPrepare()
}
}
func (this *BaseController) RespJson(status bool, code int, msg string, data interface{}) {
2020-07-10 10:51:40 +08:00
this.Data["json"] = &response.Json{
2020-07-09 15:01:25 +08:00
Code: code,
Data: data,
Msg: msg,
Status: status,
}
2020-07-02 15:59:04 +08:00
this.ServeJSON()
}
func (this *BaseController) SetLayoutEmpty() {
this.Layout = ""
}
func (this *BaseController) StopRender() {
this.EnableRender = false
}
func (this *BaseController) GetRouterSlice() (s []string) {
sep := "/"
s = strings.Split(strings.Trim(this.Ctx.Input.URL(), sep), sep)
if s[0] == "" {
s[0] = strings.TrimSpace(strings.Split(beego.AppConfig.String("custommodules"), ",")[0])
}
if len(s) < 2 {
s = append(s, "main")
}
if len(s) < 3 {
s = append(s, "index")
}
return
}