session.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. }
  24. // InitJySessionContext 获取用户session
  25. func InitJySessionContext(r *ghttp.Request) (jSession *JySession, err error) {
  26. jSession = &JySession{}
  27. cookie, err := r.Request.Cookie(JY_SESSIONNAME)
  28. if err != nil {
  29. return
  30. }
  31. if cookie.Value == "" {
  32. err = fmt.Errorf("cookie 内容为空")
  33. return
  34. }
  35. findKey, _ := url.QueryUnescape(cookie.Value)
  36. bs, err := redis.GetBytes("session", findKey)
  37. if err != nil {
  38. return
  39. }
  40. var data map[string]interface{}
  41. err = json.Unmarshal(*bs, &data)
  42. jSession.Phone, _ = data["phone"].(string)
  43. jSession.UserId, _ = data["userId"].(string)
  44. jSession.EntName, _ = data["entName"].(string)
  45. jSession.EntId = gconv.Int64(data["entId"])
  46. jSession.NewUid = gconv.Int64(data["base_user_id"])
  47. jSession.EntUserId = gconv.Int64(data["entUserId"])
  48. jSession.Data = data
  49. return
  50. }