- add jxcontext.Context to all public API.

This commit is contained in:
gazebo
2018-10-21 22:41:11 +08:00
parent 5d2bf9a510
commit b51ceb3d1d
22 changed files with 228 additions and 200 deletions

View File

@@ -4,30 +4,49 @@ import (
"net/http"
"sync"
"git.rosy.net.cn/jx-callback/business/jxcallback/auth"
"git.rosy.net.cn/jx-callback/business/jxutils/tasksch"
)
type Context struct {
rootTask tasksch.ITask
token string
userInfo *auth.LoginInfo
w http.ResponseWriter
r *http.Request
mapData map[interface{}]interface{}
locker sync.RWMutex
}
var (
AdminCtx *Context
)
func init() {
AdminCtx, _, _ = New(nil, "jxadmin", nil, nil)
}
func New(rootTask tasksch.ITask, token string, w http.ResponseWriter, r *http.Request) (ctx *Context, errCode string, err error) {
return &Context{
ctx = &Context{
rootTask: rootTask,
token: token,
w: w,
r: r,
mapData: make(map[interface{}]interface{}),
}, "", nil
}
ctx.userInfo, err = auth.GetUserInfo(token)
return ctx, "", nil
}
func (ctx *Context) GetUserName() string {
return ctx.token
userName := ctx.token
if ctx.userInfo != nil {
userName = ctx.userInfo.ID
}
if len(userName) > 10 {
userName = userName[:10]
}
return userName
}
func (ctx *Context) GetUserID() string {
@@ -41,7 +60,7 @@ func (ctx *Context) GetRootTask() tasksch.ITask {
return ctx.rootTask
}
func (ctx *Context) TaskCreated(task tasksch.ITask) bool {
func (ctx *Context) SetTaskOrAddChild(task tasksch.ITask) bool {
ctx.locker.Lock()
if ctx.rootTask == nil {