sys_session.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. // ExpiredSysSessionByAdminId 过期掉用户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. }
  45. // UnBindAdminRecordByUserId 根据系统用户id解除绑定用户关系
  46. func UnBindAdminRecordByUserId(userId int) (err error) {
  47. o := orm.NewOrm()
  48. msql := ` UPDATE admin_record SET user_id = 0 WHERE user_id = ? `
  49. _, err = o.Raw(msql, userId).Exec()
  50. return
  51. }