h5_admin_session.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package h5_admin_session
  2. import (
  3. "github.com/beego/beego/v2/client/orm"
  4. "time"
  5. )
  6. type H5AdminSession struct {
  7. SessionId int `orm:"column(session_id);pk" json:"session_id"`
  8. AdminId int `orm:"column(admin_id);" json:"admin_id"` // 后台管理员id
  9. OpenId string `orm:"column(open_id);" json:"open_id"` // 第三方openid(微信等)
  10. AccessToken string `orm:"column(access_token);" json:"access_token"`
  11. ExpireTime time.Time `orm:"column(expire_time);" json:"expire_time"` // 过期时间
  12. CreatedTime time.Time `orm:"column(created_time);" json:"created_time"` // 创建时间
  13. LastUpdatedTime time.Time `orm:"column(last_updated_time);" json:"last_updated_time"`
  14. }
  15. //更新session信息
  16. func (h5AdminSession *H5AdminSession) Update(cols []string) (err error) {
  17. o := orm.NewOrm()
  18. _, err = o.Update(h5AdminSession, cols...)
  19. return
  20. }
  21. func GetSessionByToken(token string) (item *H5AdminSession, err error) {
  22. sql := `SELECT * FROM h5_admin_session 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(&item)
  25. return
  26. }
  27. func GetSessionCountByToken(token string) (count int, err error) {
  28. sql := `SELECT COUNT(1) AS count FROM h5_admin_session WHERE access_token=? AND expire_time> NOW() ORDER BY session_id DESC LIMIT 1 `
  29. o := orm.NewOrm()
  30. err = o.Raw(sql, token).QueryRow(&count)
  31. return
  32. }
  33. //添加用户session信息
  34. func AddSession(item *H5AdminSession) (err error) {
  35. o := orm.NewOrm()
  36. _, err = o.Insert(item)
  37. return
  38. }
  39. func GetTokenByUid(adminId int) (item *H5AdminSession, err error) {
  40. sql := `SELECT * FROM h5_admin_session WHERE admin_id=? AND expire_time> NOW() ORDER BY session_id DESC LIMIT 1 `
  41. o := orm.NewOrm()
  42. err = o.Raw(sql, adminId).QueryRow(&item)
  43. return
  44. }
  45. //根据用户id获取token
  46. func GetTokenByOpenId(openId string) (item *H5AdminSession, err error) {
  47. sql := `SELECT * FROM h5_admin_session WHERE open_id=? AND expire_time> NOW() ORDER BY session_id DESC LIMIT 1 `
  48. o := orm.NewOrm()
  49. err = o.Raw(sql, openId).QueryRow(&item)
  50. return
  51. }
  52. //更新session
  53. func UpdateSession(sessionId, adminId int, expireTime time.Time) (err error) {
  54. sql := `update h5_admin_session set admin_id=?,expire_time=? where session_id = ? `
  55. o := orm.NewOrm()
  56. _, err = o.Raw(sql, adminId, expireTime, sessionId).Exec()
  57. return
  58. }