123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- package models
- import (
- "github.com/beego/beego/v2/client/orm"
- "time"
- )
- type Session struct {
- SessionId int `orm:"column(session_id);pk"`
- UserId int
- OpenId string
- AccessToken string
- ExpireTime time.Time
- CreatedTime time.Time
- LastUpdatedTime time.Time
- }
- func GetSessionByToken(token string) (item *Session, err error) {
- sql := `SELECT * FROM session WHERE access_token=? AND expire_time> NOW() ORDER BY session_id DESC LIMIT 1 `
- o := orm.NewOrmUsingDB("rddp")
- err = o.Raw(sql, token).QueryRow(&item)
- return
- }
- func GetSessionCountByToken(token string) (count int, err error) {
- sql := `SELECT COUNT(1) AS count FROM session WHERE access_token=? AND expire_time> NOW() ORDER BY session_id DESC LIMIT 1 `
- o := orm.NewOrmUsingDB("rddp")
- err = o.Raw(sql, token).QueryRow(&count)
- return
- }
- //添加用户session信息
- func AddSession(item *Session) (err error) {
- o := orm.NewOrmUsingDB("rddp")
- _, err = o.Insert(item)
- return
- }
- func GetTokenByUid(uid int) (item *Session, err error) {
- sql := `SELECT * FROM session WHERE user_id=? AND expire_time> NOW() ORDER BY session_id DESC LIMIT 1 `
- o := orm.NewOrmUsingDB("rddp")
- err = o.Raw(sql, uid).QueryRow(&item)
- return
- }
- //根据用户id获取token
- func GetTokenByOpenId(openId string) (item *Session, err error) {
- sql := `SELECT * FROM session WHERE open_id=? AND expire_time> NOW() ORDER BY session_id DESC LIMIT 1 `
- o := orm.NewOrmUsingDB("rddp")
- err = o.Raw(sql, openId).QueryRow(&item)
- return
- }
- //更新session
- func UpdateSession(sessionId, userId int, expireTime time.Time) (err error) {
- sql := `update session set user_id=?,expire_time=? where session_id = ? `
- o := orm.NewOrmUsingDB("rddp")
- _, err = o.Raw(sql, userId, expireTime, sessionId).Exec()
- return
- }
|