package models import ( "github.com/beego/beego/v2/client/orm" "time" ) func GetSessionByToken(token string) (item *CygxClptSession, err error) { //sql := `SELECT * FROM cygx_clpt_session WHERE access_token=? AND expire_time> NOW() ORDER BY session_id DESC LIMIT 1 ` sql := `SELECT * FROM cygx_clpt_session WHERE access_token=? ORDER BY session_id DESC LIMIT 1 ` o := orm.NewOrm() err = o.Raw(sql, token).QueryRow(&item) return } type CygxClptSession struct { SessionId int `orm:"column(session_id);pk"` Mobile string UserId int AccessToken string ExpireTime time.Time CreatedTime time.Time LastUpdatedTime time.Time OpenId string `description:"用户openid,最大长度:32"` UnionId string `description:"用户unionid,最大长度:64"` } // 添加用户session信息 func AddCygxClptSession(item *CygxClptSession) (err error) { o := orm.NewOrm() _, err = o.Insert(item) return } func GetXzsSessionCountByToken(token string) (count int, err error) { sql := `SELECT COUNT(1) AS count FROM cygx_clpt_session WHERE access_token=? LIMIT 1 ` o := orm.NewOrm() err = o.Raw(sql, token).QueryRow(&count) return } // 获取用户token详情 func GetUnionidByToken(token string) (item *CygxClptSession, err error) { sql := `SELECT * FROM cygx_xzs_session WHERE access_token=? LIMIT 1 ` o := orm.NewOrm() err = o.Raw(sql, token).QueryRow(&item) return } // 根据用户openid获取token func GetTokenByOpenId(mobile string) (item *CygxClptSession, err error) { sql := `SELECT * FROM cygx_clpt_session WHERE mobile=? AND expire_time> NOW() ORDER BY session_id DESC LIMIT 1 ` o := orm.NewOrm() err = o.Raw(sql, mobile).QueryRow(&item) return } // 用户绑定手机号时同时绑定外呼手机号 func BindSessionMobile(mobile, token string) (err error) { o := orm.NewOrm() sql := `UPDATE cygx_clpt_session SET mobile=? WHERE access_token=? ` _, err = o.Raw(sql, mobile, token).Exec() return } // 根据用户id和平台id获取用户关系 func GetUserSessionByUserId(userId int) (item *CygxClptSession, err error) { sql := `SELECT * FROM cygx_clpt_session WHERE user_id=? ` err = orm.NewOrm().Raw(sql, userId).QueryRow(&item) return }