82 lines
2.2 KiB
Go
82 lines
2.2 KiB
Go
package user
|
|
|
|
import (
|
|
"fmt"
|
|
"go_dreamfactory/comm"
|
|
"go_dreamfactory/lego/core"
|
|
"go_dreamfactory/lego/sys/log"
|
|
"go_dreamfactory/modules"
|
|
"go_dreamfactory/pb"
|
|
"go_dreamfactory/sys/db"
|
|
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
)
|
|
|
|
type ModelSession struct {
|
|
modules.MCompModel
|
|
module *User
|
|
}
|
|
|
|
func (this *ModelSession) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) {
|
|
err = this.MCompModel.Init(service, module, comp, options)
|
|
this.module = module.(*User)
|
|
this.TableName = comm.TableSession
|
|
this.Expired = 0 //不自动过期
|
|
return
|
|
}
|
|
|
|
func (this *ModelSession) Start() (err error) {
|
|
this.MCompModel.Start()
|
|
this.clean(this.module.service.GetTag())
|
|
return
|
|
}
|
|
|
|
// 获取用户
|
|
func (this *ModelSession) getUserSession(uid string) (user *pb.CacheUser) {
|
|
user = &pb.CacheUser{}
|
|
if err := this.GetListObj(comm.RDS_EMPTY, uid, user); err != nil {
|
|
if err != mongo.ErrNoDocuments {
|
|
this.module.Errorln(err)
|
|
}
|
|
return nil
|
|
}
|
|
return user
|
|
}
|
|
|
|
// 设置用户session
|
|
func (this *ModelSession) addUserSession(uid string, session comm.IUserSession) (err error) {
|
|
// if err = this.AddList(comm.RDS_EMPTY, uid, map[string]interface{}{
|
|
// "uid": uid,
|
|
// "sessionId": session.GetSessionId(),
|
|
// "serviceTag": session.GetServiecTag(),
|
|
// "gatewayServiceId": session.GetGatewayServiceId(),
|
|
// "ip": session.GetIP(),
|
|
// }, db.SetDBMgoLog(false)); err != nil {
|
|
if err = this.AddList(comm.RDS_EMPTY, uid, &pb.CacheUser{
|
|
Uid: uid,
|
|
SessionId: session.GetSessionId(),
|
|
ServiceTag: session.GetServiecTag(),
|
|
GatewayServiceId: session.GetGatewayServiceId(),
|
|
Ip: session.GetIP(),
|
|
}, db.SetDBMgoLog(false)); err != nil {
|
|
log.Debug("setUserSession err:%v", log.Field{Key: "uid", Value: uid}, log.Field{Key: "err", Value: err})
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
// 启动时清理session
|
|
func (this *ModelSession) clean(key string) {
|
|
keys, err := this.Redis.Keys(fmt.Sprintf("session:%s-%s_*", comm.RDS_EMPTY, key))
|
|
if err != nil {
|
|
log.Errorf("redis keys err:%v", err)
|
|
return
|
|
}
|
|
|
|
for _, k := range keys {
|
|
if err := this.Redis.Delete(k); err != nil {
|
|
log.Errorf("redis delete key err:%v", err)
|
|
}
|
|
}
|
|
}
|