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.

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