sys_session.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. // AddSysSession 新增用户登录session信息
  17. func AddSysSession(item *SysSession) (err error) {
  18. o := orm.NewOrm()
  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.NewOrm()
  29. err = o.Raw(sql, sysUserId).QueryRow(&item)
  30. return
  31. }
  32. // GetSysSessionByToken 根据token获取session
  33. func GetSysSessionByToken(token string) (item *SysSession, err error) {
  34. sql := `SELECT * FROM sys_session WHERE access_token=? AND expired_time> NOW() ORDER BY expired_time DESC LIMIT 1 `
  35. o := orm.NewOrm()
  36. err = o.Raw(sql, token).QueryRow(&item)
  37. return
  38. }
  39. // ExpiredSysSessionByAdminId 过期掉用户token
  40. func ExpiredSysSessionByAdminId(adminId int) (err error) {
  41. sql := `update sys_session set expired_time = NOW() WHERE sys_user_id=? `
  42. o := orm.NewOrm()
  43. _, err = o.Raw(sql, adminId).Exec()
  44. return
  45. }
  46. // UnBindAdminRecordByUserId 根据系统用户id解除绑定用户关系
  47. func UnBindAdminRecordByUserId(userId int) (err error) {
  48. o := orm.NewOrm()
  49. msql := ` UPDATE admin_record SET user_id = 0 WHERE user_id = ? `
  50. _, err = o.Raw(msql, userId).Exec()
  51. return
  52. }