base.go 3.1 KB

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