session.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package api
  2. import (
  3. "cockData/server/cache"
  4. "cockData/server/log"
  5. "crypto/md5"
  6. "encoding/hex"
  7. "errors"
  8. "fmt"
  9. "github.com/gin-gonic/gin"
  10. "github.com/gin-gonic/gin/binding"
  11. )
  12. const SessionRedisKey = "cocks:sessions:%s"
  13. type SessionCache struct {
  14. UserID uint
  15. Region string
  16. }
  17. func getSessionKey(session string) string {
  18. return fmt.Sprintf(SessionRedisKey, session)
  19. }
  20. type SessionParams struct {
  21. Session string `json:"session" binding:"required"`
  22. }
  23. func createSessionAndSave(userID uint, region string) (session string, err error) {
  24. //originStr := fmt.Sprintf("%d:%d", userID, time.Now().UnixNano())
  25. originStr := fmt.Sprintf("%d", userID)
  26. hash := md5.New()
  27. _, err = hash.Write([]byte(originStr))
  28. if err != nil {
  29. log.Warn("创建Session MD5失败")
  30. return
  31. }
  32. salt := "rd-session"
  33. session = hex.EncodeToString(hash.Sum([]byte(salt))) // TODO 用MD5没问题就不改
  34. sessionCache := SessionCache{
  35. UserID: userID,
  36. Region: region,
  37. }
  38. //ex, _ := time.ParseDuration("72h")
  39. err = cache.RedisSetObj(getSessionKey(session), sessionCache)
  40. return
  41. }
  42. func getSession(session string) (userID uint, err error) {
  43. var sessionCache SessionCache
  44. //ex, _ := time.ParseDuration("72h")
  45. err = cache.RedisGetObj(getSessionKey(session), &sessionCache)
  46. if err != nil {
  47. return
  48. }
  49. userID = sessionCache.UserID
  50. return
  51. }
  52. func CheckSession(context *gin.Context) {
  53. log.Warnf("访问服务端 %s", context.Request.URL)
  54. var params SessionParams
  55. err := context.ShouldBindBodyWith(&params, binding.JSON)
  56. if err != nil {
  57. log.Warnf("未携带session参数 %+v", params)
  58. ResponseApi(context, *CreateSessionErrorResponse())
  59. return
  60. }
  61. userId, err := getSession(params.Session)
  62. if err != nil {
  63. log.Warnf("session获取失败 %+v", err)
  64. ResponseApi(context, *CreateSessionErrorResponse())
  65. return
  66. }
  67. saveOpenID(context, userId)
  68. }
  69. const OpenIdKey = "OPEN_ID"
  70. const OpenNameKey = "USER_NAME"
  71. const OpenAvatarKey = "USER_AVATAR"
  72. func saveOpenID(context *gin.Context, openID uint) {
  73. context.Set(OpenIdKey, openID)
  74. }
  75. func CheckParamsAndOpenId(context *gin.Context, obj interface{}) (uint, error) {
  76. err := context.ShouldBindBodyWith(&obj, binding.JSON)
  77. log.Infof("请求参数 %s", obj)
  78. if err != nil {
  79. ResponseApi(context, *CreateParamsErrorResponse())
  80. return 0, errors.New("params error")
  81. }
  82. return GetOpenId(context)
  83. }
  84. func GetOpenId(context *gin.Context) (uint, error) {
  85. openID := context.GetUint(OpenIdKey)
  86. if openID == 0 {
  87. return 0, errors.New("no open id")
  88. }
  89. return openID, nil
  90. }