session.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package router
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/url"
  6. "app.yhyue.com/moapp/jybase/redis"
  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. )
  11. const (
  12. JY_SESSIONNAME = "SESSIONID"
  13. )
  14. // JySession 剑鱼程序SESSION获取
  15. type JySession struct {
  16. UserId string // 上下文用户信息
  17. NewUid int64 // 新用户id
  18. EntId int64 // 当前企业id
  19. EntName string // 当前企业名称
  20. Phone string // 手机号
  21. Data g.Map // 当前Session管理对象
  22. EntUserId int64 //当前企业用户id
  23. UserName string //用户名称
  24. UserPositionId int64 //个人职位id
  25. UserAccountId int64 //个人账户id
  26. EntUserPositionId int64 //企业职位id
  27. EntUserName string //企业员工姓名
  28. PersonId int64 //自然人id
  29. AccountId int64 //账户id
  30. EntAccountId int64 //企业账户id
  31. PositionId int64 //职位id
  32. PositionType int64 //职位类型
  33. MgoUserId string //mongodb用户id
  34. RbUser int64 //消息bitmap
  35. }
  36. // InitJySessionContext 获取用户session
  37. func InitJySessionContext(r *ghttp.Request) (jSession *JySession, err error) {
  38. jSession = &JySession{}
  39. cookie, err := r.Request.Cookie(JY_SESSIONNAME)
  40. if err != nil {
  41. return
  42. }
  43. if cookie.Value == "" {
  44. err = fmt.Errorf("cookie 内容为空")
  45. return
  46. }
  47. findKey, _ := url.QueryUnescape(cookie.Value)
  48. bs, err := redis.GetBytes("session", findKey)
  49. if err != nil {
  50. return
  51. }
  52. var data map[string]interface{}
  53. err = json.Unmarshal(*bs, &data)
  54. jSession.Phone, _ = data["phone"].(string)
  55. jSession.UserId, _ = data["userId"].(string)
  56. jSession.EntName, _ = data["entName"].(string)
  57. jSession.EntId = gconv.Int64(data["entId"])
  58. jSession.NewUid = gconv.Int64(data["base_user_id"])
  59. jSession.EntUserId = gconv.Int64(data["entUserId"])
  60. jSession.UserName, _ = data["userName"].(string)
  61. jSession.EntUserName, _ = data["entUserName"].(string)
  62. jSession.UserPositionId = gconv.Int64(data["userPositionId"])
  63. jSession.UserAccountId = gconv.Int64(data["userAccountId"])
  64. jSession.EntUserPositionId = gconv.Int64(data["entUserPositionId"])
  65. jSession.PersonId = gconv.Int64(data["personId"])
  66. jSession.AccountId = gconv.Int64(data["accountId"])
  67. jSession.EntAccountId = gconv.Int64(data["entAccountId"])
  68. jSession.PositionId = gconv.Int64(data["positionId"])
  69. jSession.PositionType = gconv.Int64(data["positionType"])
  70. jSession.MgoUserId = gconv.String(data["mgoUserId"])
  71. jSession.RbUser = gconv.Int64(data["rbUser"])
  72. jSession.Data = data
  73. return
  74. }