89 lines
2.5 KiB
Go
89 lines
2.5 KiB
Go
package modules
|
|
|
|
import (
|
|
"encoding/json"
|
|
"go_dreamfactory/comm"
|
|
"go_dreamfactory/lego/core"
|
|
"go_dreamfactory/lego/sys/log"
|
|
"go_dreamfactory/pb"
|
|
"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
|
|
}
|
|
|
|
// 用户操作明细数据记录
|
|
func (this *modelUserLog) AddUserLog(uid string, itype int32, tag string, data interface{}) {
|
|
var (
|
|
dbModel *db.DBModel
|
|
err error
|
|
jsonStr []byte
|
|
logType string
|
|
)
|
|
if jsonStr, err = json.Marshal(data); err != nil {
|
|
log.Errorln(err)
|
|
return
|
|
}
|
|
if itype == 0 {
|
|
logType = "add" // 新增资源类型
|
|
} else if itype == 1 {
|
|
logType = "sub" // 消耗资源类型
|
|
}
|
|
if db.IsCross() { //如果是跨服 直接找到对应的本服
|
|
if tag, _, b := utils.UIdSplit(uid); b {
|
|
if conn, err := db.ServerDBConn(tag); err == nil {
|
|
dbModel = db.NewDBModel(comm.TableEntertainm, conn)
|
|
if _, err = dbModel.DB.InsertOne(core.SqlTable(this.TableName), &pb.DBUserLog{
|
|
Id: primitive.NewObjectID().Hex(),
|
|
Uid: uid,
|
|
ExpireAt: time.Now().Add(time.Hour * 24 * 8).Unix(),
|
|
Logtype: logType,
|
|
Tag: tag,
|
|
Data: string(jsonStr),
|
|
}); err != nil {
|
|
log.Errorln(err)
|
|
return
|
|
}
|
|
}
|
|
}
|
|
} else { // 本服 直接操作本服数据
|
|
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,
|
|
Logtype: logType,
|
|
Data: string(jsonStr),
|
|
}); err != nil {
|
|
log.Errorln(err)
|
|
return
|
|
}
|
|
}
|
|
}
|