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"
|
2022-03-26 08:53:40 +00:00
|
|
|
"time"
|
2017-08-04 08:20:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Context struct {
|
2021-05-24 21:08:17 +00:00
|
|
|
*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
|
2020-02-20 06:20:56 +00:00
|
|
|
RespData Map
|
2022-05-13 01:30:09 +00:00
|
|
|
RespFunc func()
|
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
|
|
|
}
|
|
|
|
|
2021-05-24 21:08:17 +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
|
|
|
|
}
|
|
|
|
|
|
|
|
//打印
|
2021-05-24 21:08:17 +00:00
|
|
|
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
|
|
|
|
2021-05-24 21:08:17 +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
|
2021-06-03 18:57:56 +00:00
|
|
|
//兼容android等需要json转对象的服务
|
|
|
|
resp["error"] = temp
|
2017-08-04 08:20:59 +00:00
|
|
|
} else {
|
|
|
|
resp["result"] = data
|
|
|
|
}
|
|
|
|
|
2021-05-24 21:08:17 +00:00
|
|
|
that.RespData = resp
|
2017-08-04 08:20:59 +00:00
|
|
|
|
2021-05-24 21:08:17 +00:00
|
|
|
//that.Data=d;
|
2017-08-04 08:20:59 +00:00
|
|
|
}
|
|
|
|
|
2021-05-24 21:08:17 +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()
|
|
|
|
}
|
2021-05-24 21:08:17 +00:00
|
|
|
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 {
|
|
|
|
that.Log["time"] = time.Now().Unix()
|
|
|
|
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-05-13 01:30:09 +00:00
|
|
|
that.Db.Insert("logs", that.Log)
|
2022-03-26 08:53:40 +00:00
|
|
|
}
|
|
|
|
|
2021-05-24 21:08:17 +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)
|
2021-05-24 21:08:17 +00:00
|
|
|
that.RespData = nil
|
|
|
|
that.Resp.Write(d)
|
2021-11-07 22:49:34 +00:00
|
|
|
return
|
2017-08-04 08:20:59 +00:00
|
|
|
}
|