base.go 3.1 KB

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