session.go 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package models
  2. import (
  3. "github.com/beego/beego/v2/client/orm"
  4. "time"
  5. )
  6. func GetSessionByToken(token string) (item *CygxMfyxWebSession, err error) {
  7. sql := `SELECT * FROM cygx_mfyx_web_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 CygxClptSession1 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. type CygxMfyxWebSession struct {
  24. SessionId int `orm:"column(session_id);pk"`
  25. Mobile string
  26. UserId int
  27. AccessToken string
  28. ExpireTime time.Time
  29. CreatedTime time.Time
  30. LastUpdatedTime time.Time
  31. OpenId string `description:"用户openid,最大长度:32"`
  32. UnionId string `description:"用户unionid,最大长度:64"`
  33. SessionStatus int `description:"token状态,0正常,1被顶号"`
  34. }
  35. // 添加用户session信息
  36. func AddCygxMfyxWebSession(item *CygxMfyxWebSession) (err error) {
  37. o := orm.NewOrm()
  38. _, err = o.Insert(item)
  39. return
  40. }
  41. func GetXzsSessionCountByToken(token string) (count int, err error) {
  42. sql := `SELECT COUNT(1) AS count FROM cygx_mfyx_web_session WHERE access_token=? LIMIT 1 `
  43. o := orm.NewOrm()
  44. err = o.Raw(sql, token).QueryRow(&count)
  45. return
  46. }
  47. // 获取用户token详情
  48. func GetUnionidByToken(token string) (item *CygxMfyxWebSession, err error) {
  49. sql := `SELECT * FROM cygx_xzs_session WHERE access_token=? LIMIT 1 `
  50. o := orm.NewOrm()
  51. err = o.Raw(sql, token).QueryRow(&item)
  52. return
  53. }
  54. // 根据用户openid获取token
  55. func GetTokenByOpenId(mobile string) (item *CygxMfyxWebSession, err error) {
  56. sql := `SELECT * FROM cygx_mfyx_web_session WHERE mobile=? AND expire_time> NOW() ORDER BY session_id DESC LIMIT 1 `
  57. o := orm.NewOrm()
  58. err = o.Raw(sql, mobile).QueryRow(&item)
  59. return
  60. }
  61. // 用户绑定手机号时同时绑定外呼手机号
  62. func BindSessionMobile(mobile, token string) (err error) {
  63. o := orm.NewOrm()
  64. sql := `UPDATE cygx_mfyx_web_session SET mobile=? WHERE access_token=? `
  65. _, err = o.Raw(sql, mobile, token).Exec()
  66. return
  67. }
  68. // 根据用户id和平台id获取用户关系
  69. func GetUserSessionByUserId(userId int) (item *CygxMfyxWebSession, err error) {
  70. sql := `SELECT * FROM cygx_mfyx_web_session WHERE user_id=? `
  71. err = orm.NewOrm().Raw(sql, userId).QueryRow(&item)
  72. return
  73. }
  74. // 把其他有效token改为被顶号状态
  75. func UpdateSessionStatusByMobile(mobile, token string) (err error) {
  76. o := orm.NewOrm()
  77. sql := `UPDATE cygx_mfyx_web_session SET session_status= 1 WHERE mobile=? AND expire_time> NOW() AND access_token != ? `
  78. _, err = o.Raw(sql, mobile, token).Exec()
  79. return
  80. }