统一初始beego mvc 的方法
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
1.1 KiB

  1. package mvc
  2. import (
  3. "github.com/astaxie/beego"
  4. "strings"
  5. )
  6. type NestInterface interface {
  7. NestPrepare()
  8. }
  9. type BaseController struct {
  10. beego.Controller
  11. }
  12. func (this *BaseController) Prepare() {
  13. sep := "/"
  14. s := this.GetRouterSlice()
  15. this.ViewPath = "applications/" + s[0] + sep + beego.AppConfig.String("customviewdir") + sep
  16. this.TplName = s[1] + sep + s[2] + ".tpl"
  17. if app, ok := this.AppController.(NestInterface); ok {
  18. app.NestPrepare()
  19. }
  20. }
  21. func (this *BaseController) RespJson(status bool, code int, msg string, data interface{}) {
  22. this.Data["json"] = &map[string]interface{}{"status": status, "code": code, "msg": msg, "machine": data}
  23. this.ServeJSON()
  24. }
  25. func (this *BaseController) SetLayoutEmpty() {
  26. this.Layout = ""
  27. }
  28. func (this *BaseController) StopRender() {
  29. this.EnableRender = false
  30. }
  31. func (this *BaseController) GetRouterSlice() (s []string) {
  32. sep := "/"
  33. s = strings.Split(strings.Trim(this.Ctx.Input.URL(), sep), sep)
  34. if s[0] == "" {
  35. s[0] = strings.TrimSpace(strings.Split(beego.AppConfig.String("custommodules"), ",")[0])
  36. }
  37. if len(s) < 2 {
  38. s = append(s, "main")
  39. }
  40. if len(s) < 3 {
  41. s = append(s, "index")
  42. }
  43. return
  44. }