hotime/context.go

73 lines
1.3 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/cache"
. "code.hoteas.com/golang/hotime/common"
. "code.hoteas.com/golang/hotime/db"
2017-08-04 08:20:59 +00:00
"encoding/json"
"net/http"
)
type Context struct {
*Application
2017-08-04 08:20:59 +00:00
Resp http.ResponseWriter
Req *http.Request
RouterString []string
Config Map
Db *HoTimeDB
RespData Map
2017-08-04 08:20:59 +00:00
CacheIns
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() {
2021-11-07 22:49:34 +00:00
if that.RespData == nil {
2017-08-04 08:20:59 +00:00
return
}
d, err := json.Marshal(that.RespData)
2017-08-04 08:20:59 +00:00
if err != nil {
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
}