session.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package models
  2. import (
  3. "github.com/beego/beego/v2/client/orm"
  4. "time"
  5. )
  6. func GetSessionByToken(token string) (item *CygxClptSession, err error) {
  7. sql := `SELECT * FROM cygx_clpt_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 CygxClptSession struct {
  13. SessionId int `orm:"column(session_id);pk"`
  14. Mobile string
  15. UserId int
  16. AccessToken string
  17. ExpireTime time.Time
  18. CreatedTime time.Time
  19. LastUpdatedTime time.Time
  20. OpenId string `description:"用户openid,最大长度:32"`
  21. UnionId string `description:"用户unionid,最大长度:64"`
  22. }
  23. //添加用户session信息
  24. func AddCygxClptSession(item *CygxClptSession) (err error) {
  25. o := orm.NewOrm()
  26. _, err = o.Insert(item)
  27. return
  28. }
  29. func GetXzsSessionCountByToken(token string) (count int, err error) {
  30. sql := `SELECT COUNT(1) AS count FROM cygx_clpt_session WHERE access_token=? LIMIT 1 `
  31. o := orm.NewOrm()
  32. err = o.Raw(sql, token).QueryRow(&count)
  33. return
  34. }
  35. //获取用户token详情
  36. func GetUnionidByToken(token string) (item *CygxClptSession, err error) {
  37. sql := `SELECT * FROM cygx_xzs_session WHERE access_token=? LIMIT 1 `
  38. o := orm.NewOrm()
  39. err = o.Raw(sql, token).QueryRow(&item)
  40. return
  41. }
  42. //根据用户openid获取token
  43. func GetTokenByOpenId(mobile string) (item *CygxClptSession, err error) {
  44. sql := `SELECT * FROM cygx_clpt_session WHERE mobile=? AND expire_time> NOW() ORDER BY session_id DESC LIMIT 1 `
  45. o := orm.NewOrm()
  46. err = o.Raw(sql, mobile).QueryRow(&item)
  47. return
  48. }
  49. //用户绑定手机号时同时绑定外呼手机号
  50. func BindSessionMobile(mobile, token string) (err error) {
  51. o := orm.NewOrm()
  52. sql := `UPDATE cygx_clpt_session SET mobile=? WHERE access_token=? `
  53. _, err = o.Raw(sql, mobile, token).Exec()
  54. return
  55. }