171 lines
3.5 KiB
Go
171 lines
3.5 KiB
Go
package jxcontext
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
"sync"
|
|
|
|
"git.rosy.net.cn/baseapi/utils"
|
|
"git.rosy.net.cn/jx-callback/business/auth2"
|
|
"git.rosy.net.cn/jx-callback/business/jxcallback/auth"
|
|
"git.rosy.net.cn/jx-callback/business/jxutils/tasksch"
|
|
"git.rosy.net.cn/jx-callback/business/model"
|
|
"git.rosy.net.cn/jx-callback/globals"
|
|
"github.com/astaxie/beego"
|
|
)
|
|
|
|
type IAuther interface {
|
|
GetAuthID() string
|
|
GetAuthType() string
|
|
}
|
|
|
|
type Context struct {
|
|
rootTask tasksch.ITask
|
|
token string
|
|
accessUUID string
|
|
userInfo IAuther //*auth.LoginInfo
|
|
w http.ResponseWriter
|
|
r *http.Request
|
|
mapData map[interface{}]interface{}
|
|
locker sync.RWMutex
|
|
}
|
|
|
|
const (
|
|
MaxUserNameLen = 30
|
|
)
|
|
|
|
var (
|
|
AdminCtx *Context
|
|
)
|
|
|
|
func init() {
|
|
AdminCtx = NewWithUserName(nil, "jxadmin", nil, nil)
|
|
}
|
|
|
|
func NewWithUserName(rootTask tasksch.ITask, userName string, w http.ResponseWriter, r *http.Request) (ctx *Context) {
|
|
ctx = &Context{
|
|
rootTask: rootTask,
|
|
token: userName,
|
|
w: w,
|
|
r: r,
|
|
mapData: make(map[interface{}]interface{}),
|
|
accessUUID: utils.GetUUID(),
|
|
}
|
|
return ctx
|
|
}
|
|
|
|
func New(rootTask tasksch.ITask, token string, w http.ResponseWriter, r *http.Request) (ctx *Context, errCode string, err error) {
|
|
ctx = &Context{
|
|
rootTask: rootTask,
|
|
token: token,
|
|
w: w,
|
|
r: r,
|
|
mapData: make(map[interface{}]interface{}),
|
|
accessUUID: utils.GetUUID(),
|
|
}
|
|
if auth2.IsV2Token(token) {
|
|
authInfo, err2 := auth2.GetTokenInfo(token)
|
|
if err = err2; err == nil {
|
|
ctx.userInfo = authInfo
|
|
if authInfo.TokenType != auth2.TokenTypeNormal {
|
|
err = errors.New("需要正式TOKEN")
|
|
}
|
|
}
|
|
} else {
|
|
ctx.userInfo, err = auth.GetUserInfo(token)
|
|
}
|
|
if err != nil && beego.BConfig.RunMode == "prod" {
|
|
globals.SugarLogger.Debugf("token is invalid, token:%s", token)
|
|
return nil, model.ErrCodeTokenIsInvalid, err
|
|
}
|
|
return ctx, "", nil
|
|
}
|
|
|
|
func (ctx *Context) GetUserName() string {
|
|
userName := ctx.token
|
|
if ctx.userInfo != nil {
|
|
userName = ctx.userInfo.GetAuthID()
|
|
}
|
|
if len(userName) > MaxUserNameLen {
|
|
userName = userName[:MaxUserNameLen]
|
|
}
|
|
return userName
|
|
}
|
|
|
|
func (ctx *Context) GetLoginID() string {
|
|
if ctx.userInfo != nil {
|
|
return ctx.userInfo.GetAuthID()
|
|
}
|
|
return ""
|
|
|
|
}
|
|
|
|
func (ctx *Context) GetLoginType() string {
|
|
if ctx.userInfo != nil {
|
|
return ctx.userInfo.GetAuthType()
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func (ctx *Context) GetToken() string {
|
|
return ctx.token
|
|
}
|
|
|
|
func (ctx *Context) GetAccessUUID() string {
|
|
return ctx.accessUUID
|
|
}
|
|
|
|
func (ctx *Context) GetLoginInfo() IAuther {
|
|
return ctx.userInfo
|
|
}
|
|
|
|
func (ctx *Context) GetRootTask() tasksch.ITask {
|
|
ctx.locker.RLock()
|
|
defer ctx.locker.RUnlock()
|
|
|
|
return ctx.rootTask
|
|
}
|
|
|
|
func (ctx *Context) SetTaskOrAddChild(task tasksch.ITask, parentTask tasksch.ITask) bool {
|
|
if parentTask != nil {
|
|
parentTask.AddChild(task)
|
|
} else {
|
|
parentTask = task
|
|
}
|
|
ctx.locker.RLock()
|
|
if ctx.rootTask == nil {
|
|
ctx.locker.RUnlock()
|
|
|
|
ctx.locker.Lock()
|
|
if ctx.rootTask == nil {
|
|
ctx.rootTask = parentTask
|
|
}
|
|
ctx.locker.Unlock()
|
|
return true
|
|
}
|
|
ctx.locker.RUnlock()
|
|
return false
|
|
}
|
|
|
|
func (ctx *Context) GetResponseWriter() http.ResponseWriter {
|
|
return ctx.w
|
|
}
|
|
|
|
func (ctx *Context) GetRequest() *http.Request {
|
|
return ctx.r
|
|
}
|
|
|
|
func (ctx *Context) SetValue(key, value interface{}) {
|
|
ctx.locker.Lock()
|
|
defer ctx.locker.Unlock()
|
|
|
|
ctx.mapData[key] = value
|
|
}
|
|
|
|
func (ctx *Context) GetValue(key interface{}) interface{} {
|
|
ctx.locker.RLock()
|
|
defer ctx.locker.RUnlock()
|
|
|
|
return ctx.mapData[key]
|
|
}
|