sys_session.go 974 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. package models
  2. import (
  3. "time"
  4. "github.com/beego/beego/v2/client/orm"
  5. )
  6. type SysSession struct {
  7. SysSessionId int `orm:"pk"`
  8. SysUserId int
  9. UserName string
  10. AccessToken string
  11. ExpiredTime time.Time
  12. CreatedTime time.Time
  13. LastUpdatedTime time.Time
  14. }
  15. func (s *SysSession) AddSysSession() (err error) {
  16. o := orm.NewOrm()
  17. insertId, err := o.Insert(s)
  18. s.SysSessionId = int(insertId)
  19. return
  20. }
  21. func GetSysSessionByToken(token string) (item *SysSession, err error) {
  22. sql := `SELECT * FROM sys_session WHERE access_token=? AND expired_time> NOW() ORDER BY expired_time DESC LIMIT 1 `
  23. o := orm.NewOrm()
  24. err = o.Raw(sql, token).QueryRow(&item)
  25. return
  26. }
  27. // ExpiredSysSessionBySysUserId 过期掉用户token
  28. func ExpiredSysSessionBySysUserId(sysUserId int) (err error) {
  29. sql := `UPDATE sys_session SET expired_time = NOW() WHERE sys_user_id=? AND expired_time > NOW()`
  30. o := orm.NewOrm()
  31. _, err = o.Raw(sql, sysUserId).Exec()
  32. return
  33. }