sys_session.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package system
  2. import (
  3. "github.com/beego/beego/v2/client/orm"
  4. "time"
  5. )
  6. type SysSession struct {
  7. Id int `orm:"column(id);pk"`
  8. SysUserId int
  9. UserName string
  10. AccessToken string
  11. IsRemember int `description:"是否属于受信设备"`
  12. ExpiredTime time.Time
  13. CreatedTime time.Time
  14. LastUpdatedTime time.Time
  15. }
  16. //新增用户登录session信息
  17. func AddSysSession(item *SysSession) (err error) {
  18. o := orm.NewOrmUsingDB("rddp")
  19. lastId, err := o.Insert(item)
  20. if err != nil {
  21. return
  22. }
  23. item.Id = int(lastId)
  24. return
  25. }
  26. func GetSysSessionBySysUserId(sysUserId int) (item *SysSession, err error) {
  27. sql := `SELECT * FROM sys_session WHERE sys_user_id=? AND expired_time> NOW() ORDER BY expired_time DESC LIMIT 1 `
  28. o := orm.NewOrmUsingDB("rddp")
  29. err = o.Raw(sql, sysUserId).QueryRow(&item)
  30. return
  31. }
  32. func GetSysSessionByToken(token string) (item *SysSession, err error) {
  33. sql := `SELECT * FROM sys_session WHERE access_token=? AND expired_time> NOW() ORDER BY expired_time DESC LIMIT 1 `
  34. o := orm.NewOrmUsingDB("rddp")
  35. err = o.Raw(sql, token).QueryRow(&item)
  36. return
  37. }
  38. //过期掉用户token
  39. func ExpiredSysSessionByAdminId(adminId int) (err error) {
  40. sql := `update sys_session set expired_time = NOW() WHERE sys_user_id=? `
  41. o := orm.NewOrmUsingDB("rddp")
  42. _, err = o.Raw(sql, adminId).Exec()
  43. return
  44. }