1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- package system
- import (
- "context"
- "errors"
- "time"
- "github.com/flipped-aurora/gin-vue-admin/server/global"
- "github.com/flipped-aurora/gin-vue-admin/server/model/system"
- "gorm.io/gorm"
- )
- type JwtService struct {
- }
- //@author: [piexlmax](https://github.com/piexlmax)
- //@function: JsonInBlacklist
- //@description: 拉黑jwt
- //@param: jwtList model.JwtBlacklist
- //@return: err error
- func (jwtService *JwtService) JsonInBlacklist(jwtList system.JwtBlacklist) (err error) {
- err = global.GVA_DB.Create(&jwtList).Error
- return
- }
- //@author: [piexlmax](https://github.com/piexlmax)
- //@function: IsBlacklist
- //@description: 判断JWT是否在黑名单内部
- //@param: jwt string
- //@return: bool
- func (jwtService *JwtService) IsBlacklist(jwt string) bool {
- err := global.GVA_DB.Where("jwt = ?", jwt).First(&system.JwtBlacklist{}).Error
- isNotFound := errors.Is(err, gorm.ErrRecordNotFound)
- return !isNotFound
- }
- //@author: [piexlmax](https://github.com/piexlmax)
- //@function: GetRedisJWT
- //@description: 从redis取jwt
- //@param: userName string
- //@return: err error, redisJWT string
- func (jwtService *JwtService) GetRedisJWT(userName string) (err error, redisJWT string) {
- redisJWT, err = global.GVA_REDIS.Get(context.Background(), userName).Result()
- return err, redisJWT
- }
- //@author: [piexlmax](https://github.com/piexlmax)
- //@function: SetRedisJWT
- //@description: jwt存入redis并设置过期时间
- //@param: jwt string, userName string
- //@return: err error
- func (jwtService *JwtService) SetRedisJWT(jwt string, userName string) (err error) {
- // 此处过期时间等于jwt过期时间
- timer := time.Duration(global.GVA_CONFIG.JWT.ExpiresTime) * time.Second
- err = global.GVA_REDIS.Set(context.Background(), userName, jwt, timer).Err()
- return err
- }
|