session.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. package models
  2. import (
  3. "github.com/beego/beego/v2/client/orm"
  4. "time"
  5. )
  6. func GetSessionByToken(token string) (item *CygxMfyxGzhSession, err error) {
  7. sql := `SELECT * FROM cygx_mfyx_gzh_session WHERE access_token=? AND expire_time> NOW() ORDER BY session_id DESC LIMIT 1 `
  8. o := orm.NewOrm()
  9. err = o.Raw(sql, token).QueryRow(&item)
  10. return
  11. }
  12. type CygxMfyxGzhSession struct {
  13. SessionId int `orm:"column(session_id);pk"`
  14. UnionId string
  15. UserId int
  16. OpenId string
  17. AccessToken string
  18. ExpireTime time.Time
  19. CreatedTime time.Time
  20. LastUpdatedTime time.Time
  21. }
  22. // 添加用户session信息
  23. func AddCygxXzsSession(item *CygxMfyxGzhSession) (err error) {
  24. o := orm.NewOrm()
  25. _, err = o.Insert(item)
  26. return
  27. }
  28. // 根据用户openid获取token
  29. func GetTokenByOpenId(openId string) (item *CygxMfyxGzhSession, err error) {
  30. sql := `SELECT * FROM cygx_mfyx_gzh_session WHERE open_id=? AND expire_time> NOW() ORDER BY session_id DESC LIMIT 1 `
  31. o := orm.NewOrm()
  32. err = o.Raw(sql, openId).QueryRow(&item)
  33. return
  34. }