session_mobile.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package models
  2. import (
  3. "github.com/beego/beego/v2/client/orm"
  4. "time"
  5. )
  6. type CygxSessionMobile struct {
  7. SessionId int `orm:"column(session_id);pk"`
  8. Mobile string
  9. UserId int
  10. AccessToken string
  11. ExpireTime time.Time
  12. CreatedTime time.Time
  13. LastUpdatedTime time.Time
  14. }
  15. func GetCygxSessionMobile(token string) (item *CygxSessionMobile, err error) {
  16. sql := `SELECT * FROM cygx_session_mobile WHERE access_token=? AND expire_time> NOW() ORDER BY session_id DESC LIMIT 1 `
  17. o := orm.NewOrm()
  18. err = o.Raw(sql, token).QueryRow(&item)
  19. return
  20. }
  21. func GetSessionMobileCountByToken(token string) (count int, err error) {
  22. sql := `SELECT COUNT(1) AS count FROM cygx_session_mobile WHERE access_token=? AND expire_time> NOW() ORDER BY session_id DESC LIMIT 1 `
  23. o := orm.NewOrm()
  24. err = o.Raw(sql, token).QueryRow(&count)
  25. return
  26. }
  27. //添加用户session信息
  28. func AddCygxSessionMobile(item *CygxSessionMobile) (err error) {
  29. o := orm.NewOrm()
  30. _, err = o.Insert(item)
  31. return
  32. }
  33. //根据用户id获取token
  34. func GetSessionMobileTokenByOpenId(openId string) (item *CygxSession, err error) {
  35. sql := `SELECT * FROM cygx_session_mobile WHERE mobile=? AND expire_time> NOW() ORDER BY session_id DESC LIMIT 1 `
  36. o := orm.NewOrm()
  37. err = o.Raw(sql, openId).QueryRow(&item)
  38. return
  39. }