2018-12-11 08:37:12 +00:00
|
|
|
package controllers
|
|
|
|
|
|
|
|
import (
|
2019-02-09 09:07:47 +00:00
|
|
|
"github.com/cnlh/nps/lib/common"
|
2019-03-01 09:23:14 +00:00
|
|
|
"github.com/cnlh/nps/lib/crypt"
|
2019-02-05 16:35:23 +00:00
|
|
|
"github.com/cnlh/nps/server"
|
2019-02-16 12:43:26 +00:00
|
|
|
"github.com/cnlh/nps/vender/github.com/astaxie/beego"
|
2018-12-11 08:37:12 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2019-03-01 09:23:14 +00:00
|
|
|
"time"
|
2018-12-11 08:37:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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)
|
2019-03-01 09:23:14 +00:00
|
|
|
// 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("authKey")
|
|
|
|
if !(time.Now().Unix()-int64(timestamp) < 20 && time.Now().Unix()-int64(timestamp) > 0 && crypt.Md5(configKey+strconv.Itoa(timestamp)) == md5Key) {
|
2019-01-29 03:20:39 +00:00
|
|
|
if s.GetSession("auth") != true {
|
|
|
|
s.Redirect("/login/index", 302)
|
|
|
|
}
|
2018-12-11 08:37:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//加载模板
|
|
|
|
func (s *BaseController) display(tpl ...string) {
|
|
|
|
var tplname string
|
2019-01-25 04:10:12 +00:00
|
|
|
if s.Data["menu"] == nil {
|
|
|
|
s.Data["menu"] = s.actionName
|
|
|
|
}
|
2018-12-11 08:37:12 +00:00
|
|
|
if len(tpl) > 0 {
|
|
|
|
tplname = strings.Join([]string{tpl[0], "html"}, ".")
|
|
|
|
} else {
|
|
|
|
tplname = s.controllerName + "/" + s.actionName + ".html"
|
|
|
|
}
|
|
|
|
ip := s.Ctx.Request.Host
|
2019-01-25 04:10:12 +00:00
|
|
|
if strings.LastIndex(ip, ":") > 0 {
|
2019-02-09 09:07:47 +00:00
|
|
|
arr := strings.Split(common.GetHostByName(ip), ":")
|
2019-01-31 18:06:30 +00:00
|
|
|
s.Data["ip"] = arr[0]
|
2019-01-25 04:10:12 +00:00
|
|
|
}
|
2019-01-09 12:33:00 +00:00
|
|
|
s.Data["p"] = server.Bridge.TunnelPort
|
2018-12-11 08:37:12 +00:00
|
|
|
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
|
2019-02-16 12:43:26 +00:00
|
|
|
func ajax(str string, status int) map[string]interface{} {
|
2018-12-11 08:37:12 +00:00
|
|
|
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{})
|
2019-03-01 09:23:14 +00:00
|
|
|
json["rows"] = list
|
|
|
|
json["total"] = recordsTotal
|
2018-12-11 08:37:12 +00:00
|
|
|
s.Data["json"] = json
|
|
|
|
s.ServeJSON()
|
|
|
|
s.StopRun()
|
|
|
|
}
|
|
|
|
|
|
|
|
//ajax table参数
|
|
|
|
func (s *BaseController) GetAjaxParams() (start, limit int) {
|
2019-03-01 09:23:14 +00:00
|
|
|
return s.GetIntNoErr("offset"), s.GetIntNoErr("limit")
|
2018-12-11 08:37:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *BaseController) SetInfo(name string) {
|
|
|
|
s.Data["name"] = name
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *BaseController) SetType(name string) {
|
|
|
|
s.Data["type"] = name
|
|
|
|
}
|