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.

62 lines
1.6 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package model
import (
"time"
"go.uber.org/zap"
"gorm.io/gorm"
)
type Logout struct {
Id int64 `gorm:"primaryKey;autoIncrement"`
UserId int64
UUID string `gorm:"column:uuid;type:varchar(36);size:36;not null;uniqueIndex;comment:jwt的uuid"`
ExpiredAt int64 `gorm:"column:expired_at;type:bigint;not null;comment:过期时间,超过这个时间之后,可以从当前数据表中删除;index"`
CreatedAt *time.Time `form:"created_at" json:"created_at,omitempty" gorm:"column:created_at;type:datetime;comment:创建时间;"`
}
func (Logout) TableName() string {
return tablePrefix + "logout"
}
// SetLogout 设置退出登录
func (m *DBModel) Logout(userId int64, uuid string, expiredAt int64) {
// 将token加入到退出登录表中
m.invalidToken.Store(uuid, struct{}{})
m.validToken.Delete(uuid)
logout := &Logout{
UserId: userId,
UUID: uuid,
ExpiredAt: expiredAt,
}
if err := m.db.Create(logout).Error; err != nil {
m.logger.Error("SetLogout", zap.Error(err))
}
}
// IsInvalidToken 根据token对应的uuid判断token是否有效
func (m *DBModel) IsInvalidToken(uuid string) bool {
if _, ok := m.invalidToken.Load(uuid); ok {
return ok
}
if _, ok := m.validToken.Load(uuid); ok {
return !ok
}
logout := &Logout{}
err := m.db.Select("id").Where("uuid = ?", uuid).First(logout).Error
if err != nil && err != gorm.ErrRecordNotFound {
m.logger.Error("IsInvalidToken", zap.Error(err))
return true
}
if logout.Id > 0 {
m.invalidToken.Store(uuid, struct{}{})
m.validToken.Delete(uuid)
return true
}
m.validToken.Store(uuid, struct{}{})
return false
}