session.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package router
  2. import (
  3. "app.yhyue.com/moapp/jybase/redis"
  4. "encoding/json"
  5. "fmt"
  6. "github.com/gogf/gf/v2/frame/g"
  7. "github.com/gogf/gf/v2/net/ghttp"
  8. "github.com/gogf/gf/v2/util/gconv"
  9. "net/url"
  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. }
  23. // InitJySessionContext 获取用户session
  24. func InitJySessionContext(r *ghttp.Request) (jSession *JySession, err error) {
  25. jSession = &JySession{}
  26. cookie, err := r.Request.Cookie(JY_SESSIONNAME)
  27. if err != nil {
  28. return
  29. }
  30. if cookie.Value == "" {
  31. err = fmt.Errorf("cookie 内容为空")
  32. return
  33. }
  34. findKey, _ := url.QueryUnescape(cookie.Value)
  35. bs, err := redis.GetBytes("session", findKey)
  36. if err != nil {
  37. return
  38. }
  39. var data map[string]interface{}
  40. err = json.Unmarshal(*bs, &data)
  41. jSession.Phone, _ = data["phone"].(string)
  42. jSession.UserId, _ = data["userId"].(string)
  43. jSession.EntName, _ = data["entName"].(string)
  44. jSession.EntId = gconv.Int64(data["entId"])
  45. jSession.NewUid = gconv.Int64(data["userId_new"])
  46. jSession.Data = data
  47. return
  48. }