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.5 KiB

  1. package service
  2. import (
  3. "context"
  4. "errors"
  5. "gin-vue-admin/global"
  6. "gin-vue-admin/model"
  7. "time"
  8. "gorm.io/gorm"
  9. )
  10. //@author: [piexlmax](https://github.com/piexlmax)
  11. //@function: JsonInBlacklist
  12. //@description: 拉黑jwt
  13. //@param: jwtList model.JwtBlacklist
  14. //@return: err error
  15. func JsonInBlacklist(jwtList model.JwtBlacklist) (err error) {
  16. err = global.GVA_DB.Create(&jwtList).Error
  17. return
  18. }
  19. //@author: [piexlmax](https://github.com/piexlmax)
  20. //@function: IsBlacklist
  21. //@description: 判断JWT是否在黑名单内部
  22. //@param: jwt string
  23. //@return: bool
  24. func IsBlacklist(jwt string) bool {
  25. err := global.GVA_DB.Where("jwt = ?", jwt).First(&model.JwtBlacklist{}).Error
  26. isNotFound := errors.Is(err, gorm.ErrRecordNotFound)
  27. return !isNotFound
  28. }
  29. //@author: [piexlmax](https://github.com/piexlmax)
  30. //@function: GetRedisJWT
  31. //@description: 从redis取jwt
  32. //@param: userName string
  33. //@return: err error, redisJWT string
  34. func GetRedisJWT(userName string) (err error, redisJWT string) {
  35. redisJWT, err = global.GVA_REDIS.Get(context.Background(), userName).Result()
  36. return err, redisJWT
  37. }
  38. //@author: [piexlmax](https://github.com/piexlmax)
  39. //@function: SetRedisJWT
  40. //@description: jwt存入redis并设置过期时间
  41. //@param: jwt string, userName string
  42. //@return: err error
  43. func SetRedisJWT(jwt string, userName string) (err error) {
  44. // 此处过期时间等于jwt过期时间
  45. timer := time.Duration(global.GVA_CONFIG.JWT.ExpiresTime) * time.Second
  46. err = global.GVA_REDIS.Set(context.Background(), userName, jwt, timer).Err()
  47. return err
  48. }