hotime/context.go

102 lines
2.1 KiB
Go
Raw Normal View History

2017-08-04 08:20:59 +00:00
package hotime
import (
2022-03-12 17:12:29 +00:00
. "code.hoteas.com/golang/hotime/common"
. "code.hoteas.com/golang/hotime/db"
2017-08-04 08:20:59 +00:00
"encoding/json"
"net/http"
2022-08-20 14:38:15 +00:00
"strings"
2022-03-26 08:53:40 +00:00
"time"
2017-08-04 08:20:59 +00:00
)
type Context struct {
*Application
2017-08-04 08:20:59 +00:00
Resp http.ResponseWriter
Req *http.Request
2022-05-13 01:30:09 +00:00
Log Map //日志有则创建
2017-08-04 08:20:59 +00:00
RouterString []string
Config Map
Db *HoTimeDB
RespData Map
2022-05-13 01:30:09 +00:00
RespFunc func()
2022-08-07 18:37:15 +00:00
//CacheIns
2017-08-04 08:20:59 +00:00
SessionIns
2021-11-07 22:49:34 +00:00
DataSize int
2017-08-22 08:24:55 +00:00
HandlerStr string //复写请求url
2017-08-04 08:20:59 +00:00
}
// 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
2017-08-04 08:20:59 +00:00
return d
}
//打印
func (that *Context) Display(statu int, data interface{}) {
2017-08-04 08:20:59 +00:00
2021-06-11 00:06:44 +00:00
resp := Map{"status": statu}
2017-08-04 08:20:59 +00:00
if statu != 0 {
temp := Map{}
2017-08-10 10:14:56 +00:00
tpe := that.Config.GetMap("error").GetString(ObjToStr(statu))
2017-08-22 08:24:55 +00:00
if tpe == "" {
2021-05-23 23:27:41 +00:00
//logFmt(errors.New("找不到对应的错误码"), 2, LOG_WARN)
2017-08-10 10:14:56 +00:00
}
temp["type"] = tpe
2017-08-04 08:20:59 +00:00
temp["msg"] = data
resp["result"] = temp
//兼容android等需要json转对象的服务
resp["error"] = temp
2017-08-04 08:20:59 +00:00
} else {
resp["result"] = data
}
that.RespData = resp
2017-08-04 08:20:59 +00:00
//that.Data=d;
2017-08-04 08:20:59 +00:00
}
func (that *Context) View() {
2022-05-13 01:30:09 +00:00
if that.RespFunc != nil {
2022-03-26 08:53:40 +00:00
that.RespFunc()
}
if that.RespData == nil {
2017-08-04 08:20:59 +00:00
return
}
2022-03-26 08:53:40 +00:00
//创建日志
2022-05-13 01:30:09 +00:00
if that.Log != nil {
2022-07-26 09:37:17 +00:00
that.Log["time"] = time.Now().Format("2006-01-02 15:04")
2022-05-13 01:30:09 +00:00
if that.Session("admin_id").Data != nil {
that.Log["admin_id"] = that.Session("admin_id").ToCeilInt()
2022-03-26 08:53:40 +00:00
}
2022-05-13 01:30:09 +00:00
if that.Session("user_id").Data != nil {
that.Log["user_id"] = that.Session("user_id").ToCeilInt()
2022-03-26 08:53:40 +00:00
}
2022-08-20 14:38:15 +00:00
ipStr := Substr(that.Req.RemoteAddr, 0, strings.Index(that.Req.RemoteAddr, ":"))
//负载均衡优化
if ipStr == "127.0.0.1" {
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")
}
}
that.Log["ip"] = ipStr
2022-05-13 01:30:09 +00:00
that.Db.Insert("logs", that.Log)
2022-03-26 08:53:40 +00:00
}
d, err := json.Marshal(that.RespData)
2017-08-04 08:20:59 +00:00
if err != nil {
2022-05-13 01:30:09 +00:00
that.Display(1, err.Error())
that.View()
2017-08-04 08:20:59 +00:00
return
}
2021-11-07 22:49:34 +00:00
that.DataSize = len(d)
that.RespData = nil
that.Resp.Write(d)
2021-11-07 22:49:34 +00:00
return
2017-08-04 08:20:59 +00:00
}