go_dreamfactory/modules/modeluserlog.go

120 lines
3.5 KiB
Go

package modules
import (
"encoding/json"
"go_dreamfactory/comm"
"go_dreamfactory/lego/core"
"go_dreamfactory/lego/sys/log"
"go_dreamfactory/sys/configure"
"go_dreamfactory/sys/db"
"go_dreamfactory/utils"
"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
}
type DBUserLogRecord struct {
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"`
Logtype string `protobuf:"bytes,3,opt,name=logtype,proto3" json:"logtype"`
Tag string `protobuf:"bytes,4,opt,name=tag,proto3" json:"tag"`
Data string `protobuf:"bytes,5,opt,name=data,proto3" json:"data"`
Ctime int64 `protobuf:"varint,6,opt,name=ctime,proto3" json:"ctime"` // 写日志的时间
Req string `protobuf:"bytes,7,opt,name=req,proto3" json:"req"` // 请求参数
ExpireAt time.Time `bson:"expireAt,omitempty"`
}
// 用户操作明细数据记录
func (this *modelUserLog) AddUserLog(uid string, req interface{}, itype int32, tag string, data interface{}) {
var (
err error
jsonStr []byte
logType string
jsonReq []byte
)
if data == nil { // 过滤空数据
return
}
if jsonStr, err = json.Marshal(data); err != nil {
log.Errorln(err)
return
}
if len(jsonStr) == 0 {
return
}
if jsonReq, err = json.Marshal(req); err != nil {
log.Errorln(err)
return
}
if len(jsonReq) > 128 {
jsonReq = make([]byte, 0)
}
if itype == 0 {
logType = "add" // 新增资源类型
} else if itype == 1 {
logType = "sub" // 消耗资源类型
} else {
logType = "undefine"
}
if db.IsCross() { //如果是跨服 直接找到对应的本服
if tag, _, b := utils.UIdSplit(uid); b {
if conn, err := db.ServerDBConn(tag); err == nil {
if _, err = conn.Mgo.InsertOne(core.SqlTable(this.TableName), &DBUserLogRecord{
Id: primitive.NewObjectID().Hex(),
Uid: uid,
Logtype: logType,
Tag: tag,
Data: string(jsonStr),
Ctime: configure.Now().Unix(),
Req: string(jsonReq),
ExpireAt: configure.Now().Add(time.Hour * 24 * 8),
}); err != nil {
log.Errorln(err)
return
}
}
}
} else { // 本服 直接操作本服数据
if _, err = this.DBModel.DB.InsertOne(core.SqlTable(this.TableName), &DBUserLogRecord{
Id: primitive.NewObjectID().Hex(),
Uid: uid,
Logtype: logType,
Tag: tag,
Data: string(jsonStr),
Ctime: configure.Now().Unix(),
Req: string(jsonReq),
ExpireAt: configure.Now().Add(time.Hour * 24 * 8),
}); err != nil {
log.Errorln(err)
return
}
}
}