12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- package h5_admin_session
- import (
- "github.com/beego/beego/v2/client/orm"
- "time"
- )
- type H5AdminSession struct {
- SessionId int `orm:"column(session_id);pk" json:"session_id"`
- AdminId int `orm:"column(admin_id);" json:"admin_id"` // 后台管理员id
- OpenId string `orm:"column(open_id);" json:"open_id"` // 第三方openid(微信等)
- AccessToken string `orm:"column(access_token);" json:"access_token"`
- ExpireTime time.Time `orm:"column(expire_time);" json:"expire_time"` // 过期时间
- CreatedTime time.Time `orm:"column(created_time);" json:"created_time"` // 创建时间
- LastUpdatedTime time.Time `orm:"column(last_updated_time);" json:"last_updated_time"`
- }
- //更新session信息
- func (h5AdminSession *H5AdminSession) Update(cols []string) (err error) {
- o := orm.NewOrm()
- _, err = o.Update(h5AdminSession, cols...)
- return
- }
- func GetSessionByToken(token string) (item *H5AdminSession, err error) {
- sql := `SELECT * FROM h5_admin_session WHERE access_token=? AND expire_time> NOW() ORDER BY session_id DESC LIMIT 1 `
- o := orm.NewOrm()
- err = o.Raw(sql, token).QueryRow(&item)
- return
- }
- func GetSessionCountByToken(token string) (count int, err error) {
- sql := `SELECT COUNT(1) AS count FROM h5_admin_session WHERE access_token=? AND expire_time> NOW() ORDER BY session_id DESC LIMIT 1 `
- o := orm.NewOrm()
- err = o.Raw(sql, token).QueryRow(&count)
- return
- }
- //添加用户session信息
- func AddSession(item *H5AdminSession) (err error) {
- o := orm.NewOrm()
- _, err = o.Insert(item)
- return
- }
- func GetTokenByUid(adminId int) (item *H5AdminSession, err error) {
- sql := `SELECT * FROM h5_admin_session WHERE admin_id=? AND expire_time> NOW() ORDER BY session_id DESC LIMIT 1 `
- o := orm.NewOrm()
- err = o.Raw(sql, adminId).QueryRow(&item)
- return
- }
- //根据用户id获取token
- func GetTokenByOpenId(openId string) (item *H5AdminSession, err error) {
- sql := `SELECT * FROM h5_admin_session WHERE open_id=? AND expire_time> NOW() ORDER BY session_id DESC LIMIT 1 `
- o := orm.NewOrm()
- err = o.Raw(sql, openId).QueryRow(&item)
- return
- }
- //更新session
- func UpdateSession(sessionId, adminId int, expireTime time.Time) (err error) {
- sql := `update h5_admin_session set admin_id=?,expire_time=? where session_id = ? `
- o := orm.NewOrm()
- _, err = o.Raw(sql, adminId, expireTime, sessionId).Exec()
- return
- }
|