base.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package controllers
  2. import (
  3. "github.com/astaxie/beego"
  4. "github.com/cnlh/nps/lib"
  5. "github.com/cnlh/nps/server"
  6. "strconv"
  7. "strings"
  8. )
  9. type BaseController struct {
  10. beego.Controller
  11. controllerName string
  12. actionName string
  13. }
  14. //初始化参数
  15. func (s *BaseController) Prepare() {
  16. controllerName, actionName := s.GetControllerAndAction()
  17. s.controllerName = strings.ToLower(controllerName[0 : len(controllerName)-10])
  18. s.actionName = strings.ToLower(actionName)
  19. arr := strings.Split(s.Ctx.Request.RemoteAddr, ":")
  20. if len(arr) > 0 && arr[0] != beego.AppConfig.String("authip") {
  21. if s.GetSession("auth") != true {
  22. s.Redirect("/login/index", 302)
  23. }
  24. }
  25. }
  26. //加载模板
  27. func (s *BaseController) display(tpl ...string) {
  28. var tplname string
  29. if s.Data["menu"] == nil {
  30. s.Data["menu"] = s.actionName
  31. }
  32. if len(tpl) > 0 {
  33. tplname = strings.Join([]string{tpl[0], "html"}, ".")
  34. } else {
  35. tplname = s.controllerName + "/" + s.actionName + ".html"
  36. }
  37. ip := s.Ctx.Request.Host
  38. if strings.LastIndex(ip, ":") > 0 {
  39. arr := strings.Split(lib.GetHostByName(ip), ":")
  40. s.Data["ip"] = arr[0]
  41. }
  42. s.Data["p"] = server.Bridge.TunnelPort
  43. s.Data["proxyPort"] = beego.AppConfig.String("hostPort")
  44. s.Layout = "public/layout.html"
  45. s.TplName = tplname
  46. }
  47. //错误
  48. func (s *BaseController) error() {
  49. s.Layout = "public/layout.html"
  50. s.TplName = "public/error.html"
  51. }
  52. //去掉没有err返回值的int
  53. func (s *BaseController) GetIntNoErr(key string, def ...int) int {
  54. strv := s.Ctx.Input.Query(key)
  55. if len(strv) == 0 && len(def) > 0 {
  56. return def[0]
  57. }
  58. val, _ := strconv.Atoi(strv)
  59. return val
  60. }
  61. //获取去掉错误的bool值
  62. func (s *BaseController) GetBoolNoErr(key string, def ...bool) bool {
  63. strv := s.Ctx.Input.Query(key)
  64. if len(strv) == 0 && len(def) > 0 {
  65. return def[0]
  66. }
  67. val, _ := strconv.ParseBool(strv)
  68. return val
  69. }
  70. //ajax正确返回
  71. func (s *BaseController) AjaxOk(str string) {
  72. s.Data["json"] = ajax(str, 1)
  73. s.ServeJSON()
  74. s.StopRun()
  75. }
  76. //ajax错误返回
  77. func (s *BaseController) AjaxErr(str string) {
  78. s.Data["json"] = ajax(str, 0)
  79. s.ServeJSON()
  80. s.StopRun()
  81. }
  82. //组装ajax
  83. func ajax(str string, status int) (map[string]interface{}) {
  84. json := make(map[string]interface{})
  85. json["status"] = status
  86. json["msg"] = str
  87. return json
  88. }
  89. //ajax table返回
  90. func (s *BaseController) AjaxTable(list interface{}, cnt int, recordsTotal int) {
  91. json := make(map[string]interface{})
  92. json["data"] = list
  93. json["draw"] = s.GetIntNoErr("draw")
  94. json["err"] = ""
  95. json["recordsTotal"] = recordsTotal
  96. json["recordsFiltered"] = cnt
  97. s.Data["json"] = json
  98. s.ServeJSON()
  99. s.StopRun()
  100. }
  101. //ajax table参数
  102. func (s *BaseController) GetAjaxParams() (start, limit int) {
  103. s.Ctx.Input.Bind(&start, "start")
  104. s.Ctx.Input.Bind(&limit, "length")
  105. return
  106. }
  107. func (s *BaseController) SetInfo(name string) {
  108. s.Data["name"] = name
  109. }
  110. func (s *BaseController) SetType(name string) {
  111. s.Data["type"] = name
  112. }