base.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. timeNowUnix := time.Now().Unix()
  28. if !(((timeNowUnix - int64(timestamp)) <= 20) && ((timeNowUnix - int64(timestamp)) >= -20) && (crypt.Md5(configKey+strconv.Itoa(timestamp)) == md5Key)) {
  29. if s.GetSession("auth") != true {
  30. s.Redirect("/login/index", 302)
  31. }
  32. }
  33. }
  34. //加载模板
  35. func (s *BaseController) display(tpl ...string) {
  36. var tplname string
  37. if s.Data["menu"] == nil {
  38. s.Data["menu"] = s.actionName
  39. }
  40. if len(tpl) > 0 {
  41. tplname = strings.Join([]string{tpl[0], "html"}, ".")
  42. } else {
  43. tplname = s.controllerName + "/" + s.actionName + ".html"
  44. }
  45. ip := s.Ctx.Request.Host
  46. s.Data["ip"] = common.GetIpByAddr(ip)
  47. s.Data["bridgeType"] = beego.AppConfig.String("bridge_type")
  48. if common.IsWindows() {
  49. s.Data["win"] = ".exe"
  50. }
  51. s.Data["p"] = server.Bridge.TunnelPort
  52. s.Data["proxyPort"] = beego.AppConfig.String("hostPort")
  53. s.Layout = "public/layout.html"
  54. s.TplName = tplname
  55. }
  56. //错误
  57. func (s *BaseController) error() {
  58. s.Layout = "public/layout.html"
  59. s.TplName = "public/error.html"
  60. }
  61. //去掉没有err返回值的int
  62. func (s *BaseController) GetIntNoErr(key string, def ...int) int {
  63. strv := s.Ctx.Input.Query(key)
  64. if len(strv) == 0 && len(def) > 0 {
  65. return def[0]
  66. }
  67. val, _ := strconv.Atoi(strv)
  68. return val
  69. }
  70. //获取去掉错误的bool值
  71. func (s *BaseController) GetBoolNoErr(key string, def ...bool) bool {
  72. strv := s.Ctx.Input.Query(key)
  73. if len(strv) == 0 && len(def) > 0 {
  74. return def[0]
  75. }
  76. val, _ := strconv.ParseBool(strv)
  77. return val
  78. }
  79. //ajax正确返回
  80. func (s *BaseController) AjaxOk(str string) {
  81. s.Data["json"] = ajax(str, 1)
  82. s.ServeJSON()
  83. s.StopRun()
  84. }
  85. //ajax错误返回
  86. func (s *BaseController) AjaxErr(str string) {
  87. s.Data["json"] = ajax(str, 0)
  88. s.ServeJSON()
  89. s.StopRun()
  90. }
  91. //组装ajax
  92. func ajax(str string, status int) map[string]interface{} {
  93. json := make(map[string]interface{})
  94. json["status"] = status
  95. json["msg"] = str
  96. return json
  97. }
  98. //ajax table返回
  99. func (s *BaseController) AjaxTable(list interface{}, cnt int, recordsTotal int) {
  100. json := make(map[string]interface{})
  101. json["rows"] = list
  102. json["total"] = recordsTotal
  103. s.Data["json"] = json
  104. s.ServeJSON()
  105. s.StopRun()
  106. }
  107. //ajax table参数
  108. func (s *BaseController) GetAjaxParams() (start, limit int) {
  109. return s.GetIntNoErr("offset"), s.GetIntNoErr("limit")
  110. }
  111. func (s *BaseController) SetInfo(name string) {
  112. s.Data["name"] = name
  113. }
  114. func (s *BaseController) SetType(name string) {
  115. s.Data["type"] = name
  116. }