diff --git a/comm/const.go b/comm/const.go index b577da1cd..231b991d8 100644 --- a/comm/const.go +++ b/comm/const.go @@ -415,6 +415,9 @@ const ( TableIsLandHero = "islandhero" //彩蛋 TableEgghunt = "egghunt" + + //用户日志 + TableUserLog = "userlog" ) // RPC服务接口定义处 diff --git a/modules/modeluserlog.go b/modules/modeluserlog.go new file mode 100644 index 000000000..1ddc1ba38 --- /dev/null +++ b/modules/modeluserlog.go @@ -0,0 +1,58 @@ +package modules + +import ( + "encoding/json" + "go_dreamfactory/comm" + "go_dreamfactory/lego/core" + "go_dreamfactory/lego/sys/log" + "go_dreamfactory/pb" + "time" + + "go.mongodb.org/mongo-driver/bson" + "go.mongodb.org/mongo-driver/bson/primitive" + "go.mongodb.org/mongo-driver/mongo" + mgooptions "go.mongodb.org/mongo-driver/mongo/options" + "go.mongodb.org/mongo-driver/x/bsonx" +) + +type modelUserLog struct { + MCompModel + module core.IModule +} + +func (this *modelUserLog) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) { + err = this.MCompModel.Init(service, module, comp, options) + this.TableName = comm.TableUserLog + this.module = module + // uid 创建索引 + this.DB.CreateIndex(core.SqlTable(this.TableName), mongo.IndexModel{ + Keys: bsonx.Doc{{Key: "uid", Value: bsonx.Int32(1)}}, + }) + indexModel := mongo.IndexModel{ + Keys: bson.M{"expireAt": 1}, // 设置TTL索引列"expire_date" + Options: mgooptions.Index().SetExpireAfterSeconds(0), // 设置过期时间(单位:秒) + } + _, err = this.DB.CreateIndex(core.SqlTable(this.TableName), indexModel) //设置 验证码过期时间索引 + return +} + +func (this *modelUserLog) AddUserLog(uid string, tag string, data interface{}) { + var ( + jsonStr []byte + err error + ) + if jsonStr, err = json.Marshal(data); err != nil { + log.Errorln(err) + return + } + if _, err = this.DBModel.DB.InsertOne(core.SqlTable(this.TableName), &pb.DBUserLog{ + Id: primitive.NewObjectID().Hex(), + Uid: uid, + ExpireAt: time.Now().Add(time.Hour * 24 * 8).Unix(), + Tag: tag, + Data: string(jsonStr), + }); err != nil { + log.Errorln(err) + return + } +} diff --git a/modules/modulebase.go b/modules/modulebase.go index 3c4b7d913..c02facf43 100644 --- a/modules/modulebase.go +++ b/modules/modulebase.go @@ -27,6 +27,7 @@ type ModuleBase struct { service base.IRPCXService options IOptions scomp comm.ISC_GateRouteComp //网关服务组件 + userlog *modelUserLog //用户日志 //常用的一些通用模块 在底层注册好 ModuleSys comm.ISys //系统 ModuleUser comm.IUser //用户模块 @@ -63,6 +64,10 @@ func (this *ModuleBase) Init(service core.IService, module core.IModule, options } return } +func (this *ModuleBase) OnInstallComp() { + this.ModuleBase.OnInstallComp() + this.userlog = this.RegisterComp(new(modelUserLog)).(*modelUserLog) +} // 模块启动接口 func (this *ModuleBase) Start() (err error) { @@ -946,3 +951,8 @@ func (this *ModuleBase) AsynHandleSession(session comm.IUserSession, handle func session.Push() this.PutUserSession(session) } + +//写用户日志 +func (this *ModuleBase) WriteUserLog(uid string, tag string, data interface{}) { + this.userlog.AddUserLog(uid, tag, data) +} diff --git a/pb/userexpand.pb.go b/pb/userexpand.pb.go index c900fc42d..df88d3a3e 100644 --- a/pb/userexpand.pb.go +++ b/pb/userexpand.pb.go @@ -354,8 +354,12 @@ type DBUserLog struct { sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id" bson:"_id"` //唯一ID - Uid string `protobuf:"bytes,2,opt,name=uid,proto3" json:"uid" bson:"uid"` //玩家ID + Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id" bson:"_id"` //唯一ID + Uid string `protobuf:"bytes,2,opt,name=uid,proto3" json:"uid"` + ExpireAt int64 `protobuf:"varint,3,opt,name=expireAt,proto3" json:"expireAt"` + Logtype string `protobuf:"bytes,4,opt,name=logtype,proto3" json:"logtype"` + Tag string `protobuf:"bytes,5,opt,name=tag,proto3" json:"tag"` + Data string `protobuf:"bytes,6,opt,name=data,proto3" json:"data"` } func (x *DBUserLog) Reset() { @@ -404,6 +408,34 @@ func (x *DBUserLog) GetUid() string { return "" } +func (x *DBUserLog) GetExpireAt() int64 { + if x != nil { + return x.ExpireAt + } + return 0 +} + +func (x *DBUserLog) GetLogtype() string { + if x != nil { + return x.Logtype + } + return "" +} + +func (x *DBUserLog) GetTag() string { + if x != nil { + return x.Tag + } + return "" +} + +func (x *DBUserLog) GetData() string { + if x != nil { + return x.Data + } + return "" +} + var File_userexpand_proto protoreflect.FileDescriptor var file_userexpand_proto_rawDesc = []byte{ @@ -508,11 +540,17 @@ var file_userexpand_proto_rawDesc = []byte{ 0x66, 0x72, 0x61, 0x67, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x12, 0x10, 0x0a, 0x03, 0x6b, 0x65, 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x12, 0x14, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x05, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, - 0x65, 0x3a, 0x02, 0x38, 0x01, 0x22, 0x2d, 0x0a, 0x09, 0x44, 0x42, 0x55, 0x73, 0x65, 0x72, 0x4c, - 0x6f, 0x67, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x02, - 0x69, 0x64, 0x12, 0x10, 0x0a, 0x03, 0x75, 0x69, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, - 0x03, 0x75, 0x69, 0x64, 0x42, 0x06, 0x5a, 0x04, 0x2e, 0x3b, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, - 0x6f, 0x74, 0x6f, 0x33, + 0x65, 0x3a, 0x02, 0x38, 0x01, 0x22, 0x89, 0x01, 0x0a, 0x09, 0x44, 0x42, 0x55, 0x73, 0x65, 0x72, + 0x4c, 0x6f, 0x67, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, + 0x02, 0x69, 0x64, 0x12, 0x10, 0x0a, 0x03, 0x75, 0x69, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, + 0x52, 0x03, 0x75, 0x69, 0x64, 0x12, 0x1a, 0x0a, 0x08, 0x65, 0x78, 0x70, 0x69, 0x72, 0x65, 0x41, + 0x74, 0x18, 0x03, 0x20, 0x01, 0x28, 0x03, 0x52, 0x08, 0x65, 0x78, 0x70, 0x69, 0x72, 0x65, 0x41, + 0x74, 0x12, 0x18, 0x0a, 0x07, 0x6c, 0x6f, 0x67, 0x74, 0x79, 0x70, 0x65, 0x18, 0x04, 0x20, 0x01, + 0x28, 0x09, 0x52, 0x07, 0x6c, 0x6f, 0x67, 0x74, 0x79, 0x70, 0x65, 0x12, 0x10, 0x0a, 0x03, 0x74, + 0x61, 0x67, 0x18, 0x05, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x74, 0x61, 0x67, 0x12, 0x12, 0x0a, + 0x04, 0x64, 0x61, 0x74, 0x61, 0x18, 0x06, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x64, 0x61, 0x74, + 0x61, 0x42, 0x06, 0x5a, 0x04, 0x2e, 0x3b, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, + 0x33, } var (