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