session.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package model
  2. import (
  3. "aiChat/internal/consts"
  4. "context"
  5. "fmt"
  6. "github.com/gogf/gf/v2/encoding/gjson"
  7. "github.com/gogf/gf/v2/frame/g"
  8. "github.com/gogf/gf/v2/net/ghttp"
  9. "github.com/gogf/gf/v2/util/gconv"
  10. "net/url"
  11. )
  12. type (
  13. sSessionCtx struct{}
  14. Context struct {
  15. JSession *JySession
  16. }
  17. )
  18. var SessionCtx = sSessionCtx{}
  19. func (s *sSessionCtx) Init(r *ghttp.Request, customCtx *Context) {
  20. r.SetCtxVar(consts.ContextKey, customCtx)
  21. }
  22. func (s *sSessionCtx) Get(ctx context.Context) *Context {
  23. value := ctx.Value(consts.ContextKey)
  24. if value == nil {
  25. return nil
  26. }
  27. if localCtx, ok := value.(*Context); ok {
  28. return localCtx
  29. }
  30. return nil
  31. }
  32. // SetSession injects business user object into context.
  33. func (s *sSessionCtx) SetSession(ctx context.Context, session *JySession) {
  34. s.Get(ctx).JSession = session
  35. }
  36. // JySession 剑鱼程序SESSION获取
  37. type JySession struct {
  38. UserId string // 上下文用户信息
  39. NewUid int64 // 新用户id
  40. EntId int64 // 当前企业id
  41. EntName string // 当前企业名称
  42. Phone string // 手机号
  43. Data g.Map // 当前Session管理对象
  44. EntUserId int64 //当前企业用户id
  45. UserName string //用户名称
  46. UserPositionId int64 //个人职位id
  47. UserAccountId int64 //个人账户id
  48. EntUserPositionId int64 //企业职位id
  49. EntUserName string //企业员工姓名
  50. PersonId int64 //自然人id
  51. AccountId int64 //账户id
  52. EntAccountId int64 //企业账户id
  53. PositionId int64 //职位id
  54. PositionType int64 //职位类型
  55. MgoUserId string //mongodb用户id
  56. }
  57. func GetSession(r *ghttp.Request) (jSession *JySession, err error) {
  58. jSession = &JySession{}
  59. cookie, err := r.Request.Cookie(consts.JY_SESSIONNAME)
  60. if err != nil {
  61. return
  62. }
  63. if cookie.Value == "" {
  64. err = fmt.Errorf("cookie 内容为空")
  65. return
  66. }
  67. findKey, _ := url.QueryUnescape(cookie.Value)
  68. rVal, err := g.Redis("session").Get(r.GetCtx(), findKey)
  69. if err != nil {
  70. return jSession, err
  71. }
  72. var data map[string]interface{}
  73. err = gjson.Unmarshal(rVal.Bytes(), &data)
  74. jSession.Phone, _ = data["phone"].(string)
  75. jSession.UserId, _ = data["userId"].(string)
  76. jSession.EntName, _ = data["entName"].(string)
  77. jSession.EntId = gconv.Int64(data["entId"])
  78. jSession.NewUid = gconv.Int64(data["base_user_id"])
  79. jSession.EntUserId = gconv.Int64(data["entUserId"])
  80. jSession.UserName, _ = data["userName"].(string)
  81. jSession.EntUserName, _ = data["entUserName"].(string)
  82. jSession.UserPositionId = gconv.Int64(data["userPositionId"])
  83. jSession.UserAccountId = gconv.Int64(data["userAccountId"])
  84. jSession.EntUserPositionId = gconv.Int64(data["entUserPositionId"])
  85. jSession.PersonId = gconv.Int64(data["personId"])
  86. jSession.AccountId = gconv.Int64(data["accountId"])
  87. jSession.EntAccountId = gconv.Int64(data["entAccountId"])
  88. jSession.PositionId = gconv.Int64(data["positionId"])
  89. jSession.PositionType = gconv.Int64(data["positionType"])
  90. jSession.MgoUserId = gconv.String(data["mgoUserId"])
  91. jSession.Data = data
  92. return
  93. }