session.go 2.2 KB

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