hotime/context.go

66 lines
1.1 KiB
Go
Raw Permalink Normal View History

2017-08-04 08:20:59 +00:00
package hotime
import (
"encoding/json"
2017-08-22 08:24:55 +00:00
"errors"
2017-08-04 08:20:59 +00:00
"net/http"
)
type Context struct {
2017-08-23 07:35:49 +00:00
contextBase
2017-08-04 08:20:59 +00:00
Resp http.ResponseWriter
Req *http.Request
2017-08-22 08:24:55 +00:00
Application *Application
2017-08-04 08:20:59 +00:00
RouterString []string
Config Map
Db *HoTimeDB
RespData Map
2017-08-04 08:20:59 +00:00
CacheIns
SessionIns
2017-08-22 08:24:55 +00:00
HandlerStr string //复写请求url
2017-08-04 08:20:59 +00:00
}
//唯一标志
func (this *Context) Mtd(router [3]string) Map {
2017-08-17 02:14:22 +00:00
this.Application.Router[router[0]][router[1]][router[2]](this)
2017-08-22 08:24:55 +00:00
d := this.RespData
this.RespData = nil
2017-08-04 08:20:59 +00:00
return d
}
//打印
func (this *Context) Display(statu int, data interface{}) {
resp := Map{"statu": statu}
if statu != 0 {
temp := Map{}
2017-08-10 10:14:56 +00:00
2017-08-22 08:24:55 +00:00
tpe := this.Config.GetMap("error").GetString(ObjToStr(statu))
if tpe == "" {
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
} else {
resp["result"] = data
}
2017-08-22 08:24:55 +00:00
this.RespData = resp
2017-08-04 08:20:59 +00:00
//this.Data=d;
}
func (this *Context) View() {
2017-08-22 08:24:55 +00:00
if this.RespData == nil {
2017-08-04 08:20:59 +00:00
return
}
2017-08-22 08:24:55 +00:00
d, err := json.Marshal(this.RespData)
2017-08-04 08:20:59 +00:00
if err != nil {
return
}
2017-08-22 08:24:55 +00:00
this.RespData = nil
2017-08-04 08:20:59 +00:00
this.Resp.Write(d)
}