You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
1.7 KiB

  1. package service
  2. import (
  3. "errors"
  4. "gin-vue-admin/global"
  5. "gin-vue-admin/model"
  6. "gorm.io/gorm"
  7. "time"
  8. )
  9. // @title JsonInBlacklist
  10. // @description create jwt blacklist
  11. // @param jwtList model.JwtBlacklist
  12. // @auth (2020/04/05 20:22)
  13. // @return err error
  14. func JsonInBlacklist(jwtList model.JwtBlacklist) (err error) {
  15. err = global.GVA_DB.Create(&jwtList).Error
  16. return
  17. }
  18. // @title IsBlacklist
  19. // @description check if the Jwt is in the blacklist or not, 判断JWT是否在黑名单内部
  20. // @auth (2020/04/05 20:22)
  21. // @param jwt string
  22. // @param jwtList model.JwtBlacklist
  23. // @return err error
  24. func IsBlacklist(jwt string) bool {
  25. isNotFound := errors.Is(global.GVA_DB.Where("jwt = ?", jwt).First(&model.JwtBlacklist{}).Error, gorm.ErrRecordNotFound)
  26. return !isNotFound
  27. }
  28. // @title GetRedisJWT
  29. // @description Get user info in redis
  30. // @auth (2020/04/05 20:22)
  31. // @param userName string
  32. // @return err error
  33. // @return redisJWT string
  34. func GetRedisJWT(userName string) (err error, redisJWT string) {
  35. redisJWT, err = global.GVA_REDIS.Get(userName).Result()
  36. return err, redisJWT
  37. }
  38. // @title SetRedisJWT
  39. // @description set jwt into the Redis
  40. // @auth (2020/04/05 20:22)
  41. // @param jwtList model.JwtBlacklist
  42. // @param userName string
  43. // @return err error
  44. func SetRedisJWT(jwt string, userName string) (err error) {
  45. // 此处过期时间等于jwt过期时间
  46. timer := 60 * 60 * 24 * 7 * time.Second
  47. err = global.GVA_REDIS.Set(userName, jwt, timer).Err()
  48. return err
  49. }