package model import ( "aiChat/internal/consts" "context" "fmt" "github.com/gogf/gf/v2/encoding/gjson" "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/net/ghttp" "github.com/gogf/gf/v2/util/gconv" "net/url" ) type ( sSessionCtx struct{} Context struct { JSession *JySession } ) var SessionCtx = sSessionCtx{} func (s *sSessionCtx) Init(r *ghttp.Request, customCtx *Context) { r.SetCtxVar(consts.ContextKey, customCtx) } func (s *sSessionCtx) Get(ctx context.Context) *Context { value := ctx.Value(consts.ContextKey) if value == nil { return nil } if localCtx, ok := value.(*Context); ok { return localCtx } return nil } // SetSession injects business user object into context. func (s *sSessionCtx) SetSession(ctx context.Context, session *JySession) { s.Get(ctx).JSession = session } // JySession 剑鱼程序SESSION获取 type JySession struct { UserId string // 上下文用户信息 NewUid int64 // 新用户id EntId int64 // 当前企业id EntName string // 当前企业名称 Phone string // 手机号 Data g.Map // 当前Session管理对象 EntUserId int64 //当前企业用户id UserName string //用户名称 UserPositionId int64 //个人职位id UserAccountId int64 //个人账户id EntUserPositionId int64 //企业职位id EntUserName string //企业员工姓名 PersonId int64 //自然人id AccountId int64 //账户id EntAccountId int64 //企业账户id PositionId int64 //职位id PositionType int64 //职位类型 MgoUserId string //mongodb用户id } func GetSession(r *ghttp.Request) (jSession *JySession, err error) { jSession = &JySession{} cookie, err := r.Request.Cookie(consts.JY_SESSIONNAME) if err != nil { return } if cookie.Value == "" { err = fmt.Errorf("cookie 内容为空") return } findKey, _ := url.QueryUnescape(cookie.Value) rVal, err := g.Redis("session").Get(r.GetCtx(), findKey) if err != nil { return jSession, err } var data map[string]interface{} err = gjson.Unmarshal(rVal.Bytes(), &data) jSession.Phone, _ = data["phone"].(string) jSession.UserId, _ = data["userId"].(string) jSession.EntName, _ = data["entName"].(string) jSession.EntId = gconv.Int64(data["entId"]) jSession.NewUid = gconv.Int64(data["base_user_id"]) jSession.EntUserId = gconv.Int64(data["entUserId"]) jSession.UserName, _ = data["userName"].(string) jSession.EntUserName, _ = data["entUserName"].(string) jSession.UserPositionId = gconv.Int64(data["userPositionId"]) jSession.UserAccountId = gconv.Int64(data["userAccountId"]) jSession.EntUserPositionId = gconv.Int64(data["entUserPositionId"]) jSession.PersonId = gconv.Int64(data["personId"]) jSession.AccountId = gconv.Int64(data["accountId"]) jSession.EntAccountId = gconv.Int64(data["entAccountId"]) jSession.PositionId = gconv.Int64(data["positionId"]) jSession.PositionType = gconv.Int64(data["positionType"]) jSession.MgoUserId = gconv.String(data["mgoUserId"]) jSession.Data = data return }