session.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. EntPositionId int64 //企业职位id
  27. EntAccountId int64 //企业账户id
  28. }
  29. // InitJySessionContext 获取用户session
  30. func InitJySessionContext(r *ghttp.Request) (jSession *JySession, err error) {
  31. jSession = &JySession{}
  32. cookie, err := r.Request.Cookie(JY_SESSIONNAME)
  33. if err != nil {
  34. return
  35. }
  36. if cookie.Value == "" {
  37. err = fmt.Errorf("cookie 内容为空")
  38. return
  39. }
  40. findKey, _ := url.QueryUnescape(cookie.Value)
  41. bs, err := redis.GetBytes("session", findKey)
  42. if err != nil {
  43. return
  44. }
  45. var data map[string]interface{}
  46. err = json.Unmarshal(*bs, &data)
  47. jSession.Phone, _ = data["phone"].(string)
  48. jSession.UserId, _ = data["userId"].(string)
  49. jSession.EntName, _ = data["entName"].(string)
  50. jSession.EntId = gconv.Int64(data["entId"])
  51. jSession.NewUid = gconv.Int64(data["base_user_id"])
  52. jSession.EntUserId = gconv.Int64(data["entUserId"])
  53. jSession.UserName, _ = data["userName"].(string)
  54. jSession.UserPositionId = gconv.Int64(data["userPositionId"])
  55. jSession.UserAccountId = gconv.Int64(data["userAccountId"])
  56. jSession.EntPositionId = gconv.Int64(data["entPositionId"])
  57. jSession.EntAccountId = gconv.Int64(data["entAccountId"])
  58. jSession.Data = data
  59. return
  60. }