session.go 2.0 KB

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