sys_session.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. package rddp
  2. import (
  3. "eta_gn/eta_bridge/global"
  4. "time"
  5. )
  6. type SysSession struct {
  7. Id int `gorm:"primaryKey;column:id;type:bigint(20);not null" json:"id"`
  8. SysUserId int `gorm:"column:sys_user_id;type:bigint(20)" json:"sys_user_id"`
  9. UserName string `gorm:"column:user_name;type:varchar(32)" json:"user_name"`
  10. AccessToken string `gorm:"index:access_token;column:access_token;type:varchar(255)" json:"access_token"`
  11. IsRemember int `gorm:"column:is_remember;type:tinyint(9) unsigned;default:0" json:"is_remember"` // 是否记住密码,属于受信设备
  12. ExpiredTime time.Time `gorm:"column:expired_time;type:datetime" json:"expired_time"`
  13. CreatedTime time.Time `gorm:"index:created_time;column:created_time;type:datetime;default:CURRENT_TIMESTAMP" json:"created_time"` // 创建时间
  14. LastUpdatedTime time.Time `gorm:"index:last_updated_time;column:last_updated_time;type:timestamp;not null;default:CURRENT_TIMESTAMP" json:"last_updated_time"`
  15. }
  16. func (m *SysSession) TableName() string {
  17. return "sys_session"
  18. }
  19. // AddSysSession 新增session
  20. func AddSysSession(item *SysSession) (err error) {
  21. err = global.MYSQL["rddp"].Create(item).Error
  22. return
  23. }
  24. // GetSysSessionByToken 根据Token获取session
  25. func GetSysSessionByToken(token string) (item *SysSession, err error) {
  26. err = global.MYSQL["rddp"].Where("access_token = ? AND expired_time > NOW()", token).Order("expired_time DESC").First(&item).Error
  27. return
  28. }
  29. // GetUserUnexpiredSysSession 获取用户未过期的Token
  30. func GetUserUnexpiredSysSession(userName, expiredTime string) (item *SysSession, err error) {
  31. err = global.MYSQL["rddp"].Where("user_name = ? AND expired_time > ?", userName, expiredTime).Order("expired_time DESC").First(&item).Error
  32. return
  33. }