session.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. }
  35. // InitJySessionContext 获取用户session
  36. func InitJySessionContext(r *ghttp.Request) (jSession *JySession, err error) {
  37. jSession = &JySession{}
  38. cookie, err := r.Request.Cookie(JY_SESSIONNAME)
  39. if err != nil {
  40. return
  41. }
  42. if cookie.Value == "" {
  43. err = fmt.Errorf("cookie 内容为空")
  44. return
  45. }
  46. findKey, _ := url.QueryUnescape(cookie.Value)
  47. bs, err := redis.GetBytes("session", findKey)
  48. if err != nil {
  49. return
  50. }
  51. var data map[string]interface{}
  52. err = json.Unmarshal(*bs, &data)
  53. jSession.Phone, _ = data["phone"].(string)
  54. jSession.UserId, _ = data["userId"].(string)
  55. jSession.EntName, _ = data["entName"].(string)
  56. jSession.EntId = gconv.Int64(data["entId"])
  57. jSession.NewUid = gconv.Int64(data["base_user_id"])
  58. jSession.EntUserId = gconv.Int64(data["entUserId"])
  59. jSession.UserName, _ = data["userName"].(string)
  60. jSession.EntUserName, _ = data["entUserName"].(string)
  61. jSession.UserPositionId = gconv.Int64(data["userPositionId"])
  62. jSession.UserAccountId = gconv.Int64(data["userAccountId"])
  63. jSession.EntUserPositionId = gconv.Int64(data["entUserPositionId"])
  64. jSession.PersonId = gconv.Int64(data["personId"])
  65. jSession.AccountId = gconv.Int64(data["accountId"])
  66. jSession.EntAccountId = gconv.Int64(data["entAccountId"])
  67. jSession.PositionId = gconv.Int64(data["positionId"])
  68. jSession.PositionType = gconv.Int64(data["positionType"])
  69. jSession.MgoUserId = gconv.String(data["mgoUserId"])
  70. jSession.Data = data
  71. return
  72. }