统一初始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.

63 lines
1.2 KiB

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