base.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. if strings.LastIndex(ip, ":") > 0 {
  46. arr := strings.Split(common.GetHostByName(ip), ":")
  47. s.Data["ip"] = arr[0]
  48. }
  49. s.Data["bridgeType"] = beego.AppConfig.String("bridge_type")
  50. if common.IsWindows() {
  51. s.Data["win"] = ".exe"
  52. }
  53. s.Data["p"] = server.Bridge.TunnelPort
  54. s.Data["proxyPort"] = beego.AppConfig.String("hostPort")
  55. s.Layout = "public/layout.html"
  56. s.TplName = tplname
  57. }
  58. //错误
  59. func (s *BaseController) error() {
  60. s.Layout = "public/layout.html"
  61. s.TplName = "public/error.html"
  62. }
  63. //去掉没有err返回值的int
  64. func (s *BaseController) GetIntNoErr(key string, def ...int) int {
  65. strv := s.Ctx.Input.Query(key)
  66. if len(strv) == 0 && len(def) > 0 {
  67. return def[0]
  68. }
  69. val, _ := strconv.Atoi(strv)
  70. return val
  71. }
  72. //获取去掉错误的bool值
  73. func (s *BaseController) GetBoolNoErr(key string, def ...bool) bool {
  74. strv := s.Ctx.Input.Query(key)
  75. if len(strv) == 0 && len(def) > 0 {
  76. return def[0]
  77. }
  78. val, _ := strconv.ParseBool(strv)
  79. return val
  80. }
  81. //ajax正确返回
  82. func (s *BaseController) AjaxOk(str string) {
  83. s.Data["json"] = ajax(str, 1)
  84. s.ServeJSON()
  85. s.StopRun()
  86. }
  87. //ajax错误返回
  88. func (s *BaseController) AjaxErr(str string) {
  89. s.Data["json"] = ajax(str, 0)
  90. s.ServeJSON()
  91. s.StopRun()
  92. }
  93. //组装ajax
  94. func ajax(str string, status int) map[string]interface{} {
  95. json := make(map[string]interface{})
  96. json["status"] = status
  97. json["msg"] = str
  98. return json
  99. }
  100. //ajax table返回
  101. func (s *BaseController) AjaxTable(list interface{}, cnt int, recordsTotal int) {
  102. json := make(map[string]interface{})
  103. json["rows"] = list
  104. json["total"] = recordsTotal
  105. s.Data["json"] = json
  106. s.ServeJSON()
  107. s.StopRun()
  108. }
  109. //ajax table参数
  110. func (s *BaseController) GetAjaxParams() (start, limit int) {
  111. return s.GetIntNoErr("offset"), s.GetIntNoErr("limit")
  112. }
  113. func (s *BaseController) SetInfo(name string) {
  114. s.Data["name"] = name
  115. }
  116. func (s *BaseController) SetType(name string) {
  117. s.Data["type"] = name
  118. }