105 lines
2.2 KiB
Go
105 lines
2.2 KiB
Go
package hotime
|
|
|
|
import (
|
|
. "code.hoteas.com/golang/hotime/common"
|
|
. "code.hoteas.com/golang/hotime/db"
|
|
"encoding/json"
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
type Context struct {
|
|
*Application
|
|
Resp http.ResponseWriter
|
|
Req *http.Request
|
|
Log Map //日志有则创建
|
|
RouterString []string
|
|
Config Map
|
|
Db *HoTimeDB
|
|
RespData Map
|
|
RespFunc func()
|
|
//CacheIns
|
|
SessionIns
|
|
DataSize int
|
|
HandlerStr string //复写请求url
|
|
}
|
|
|
|
// Mtd 唯一标志
|
|
func (that *Context) Mtd(router [3]string) Map {
|
|
that.Application.Router[router[0]][router[1]][router[2]](that)
|
|
d := that.RespData
|
|
that.RespData = nil
|
|
return d
|
|
}
|
|
|
|
//打印
|
|
func (that *Context) Display(statu int, data interface{}) {
|
|
|
|
resp := Map{"status": statu}
|
|
if statu != 0 {
|
|
temp := Map{}
|
|
|
|
tpe := that.Config.GetMap("error").GetString(ObjToStr(statu))
|
|
if tpe == "" {
|
|
//logFmt(errors.New("找不到对应的错误码"), 2, LOG_WARN)
|
|
}
|
|
|
|
temp["type"] = tpe
|
|
temp["msg"] = data
|
|
resp["result"] = temp
|
|
//兼容android等需要json转对象的服务
|
|
resp["error"] = temp
|
|
} else {
|
|
resp["result"] = data
|
|
}
|
|
|
|
that.RespData = resp
|
|
|
|
//that.Data=d;
|
|
}
|
|
|
|
func (that *Context) View() {
|
|
if that.RespFunc != nil {
|
|
that.RespFunc()
|
|
}
|
|
if that.RespData == nil {
|
|
return
|
|
}
|
|
//创建日志
|
|
if that.Log != nil {
|
|
that.Log["time"] = time.Now().Format("2006-01-02 15:04")
|
|
if that.Session("admin_id").Data != nil {
|
|
that.Log["admin_id"] = that.Session("admin_id").ToCeilInt()
|
|
}
|
|
if that.Session("user_id").Data != nil {
|
|
that.Log["user_id"] = that.Session("user_id").ToCeilInt()
|
|
}
|
|
//负载均衡优化
|
|
ipStr := ""
|
|
if that.Req.Header.Get("X-Forwarded-For") != "" {
|
|
ipStr = that.Req.Header.Get("X-Forwarded-For")
|
|
} else if that.Req.Header.Get("X-Real-IP") != "" {
|
|
ipStr = that.Req.Header.Get("X-Real-IP")
|
|
}
|
|
//负载均衡优化
|
|
if ipStr == "" {
|
|
//RemoteAddr := that.Req.RemoteAddr
|
|
ipStr = Substr(that.Req.RemoteAddr, 0, strings.Index(that.Req.RemoteAddr, ":"))
|
|
}
|
|
that.Log["ip"] = ipStr
|
|
that.Db.Insert("logs", that.Log)
|
|
}
|
|
|
|
d, err := json.Marshal(that.RespData)
|
|
if err != nil {
|
|
that.Display(1, err.Error())
|
|
that.View()
|
|
return
|
|
}
|
|
that.DataSize = len(d)
|
|
that.RespData = nil
|
|
that.Resp.Write(d)
|
|
return
|
|
}
|