package models import ( "github.com/beego/beego/v2/client/orm" "time" ) type UserSession struct { Id int `orm:"column(id);pk"` UserId int UserName string AccessToken string IsRemember int `description:"是否属于受信设备"` ExpiredTime time.Time CreatedTime time.Time LastUpdatedTime time.Time } // AddUserSession 新增用户登录session信息 func AddUserSession(item *UserSession) (err error) { o := orm.NewOrm() lastId, err := o.Insert(item) if err != nil { return } item.Id = int(lastId) return } func GetUserSessionBySysUserId(sysUserId int) (item *UserSession, err error) { sql := `SELECT * FROM user_session WHERE user_id=? AND expired_time> NOW() ORDER BY expired_time DESC LIMIT 1 ` o := orm.NewOrm() err = o.Raw(sql, sysUserId).QueryRow(&item) return } // GetUserSessionByToken 根据token获取session func GetUserSessionByToken(token string) (item *UserSession, err error) { sql := `SELECT * FROM user_session WHERE access_token=? AND expired_time> NOW() ORDER BY expired_time DESC LIMIT 1 ` o := orm.NewOrm() err = o.Raw(sql, token).QueryRow(&item) return } // ExpiredUserSessionByAdminId 过期掉用户token func ExpiredUserSessionByAdminId(adminId int) (err error) { sql := `update user_session set expired_time = NOW() WHERE user_id=? ` o := orm.NewOrm() _, err = o.Raw(sql, adminId).Exec() return } // UnBindAdminRecordByUserId 根据系统用户id解除绑定用户关系 func UnBindAdminRecordByUserId(userId int) (err error) { o := orm.NewOrm() msql := ` UPDATE admin_record SET user_id = 0 WHERE user_id = ? ` _, err = o.Raw(msql, userId).Exec() return }