beegoinit/mvc/controller.go

57 lines
1.1 KiB
Go
Raw Normal View History

2020-07-02 15:59:04 +08:00
package mvc
import (
"github.com/astaxie/beego"
"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-09 12:01:51 +08:00
this.Data["json"] = &map[string]interface{}{"status": status, "code": code, "msg": msg, "data": data}
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
}