go_dreamfactory/modules/modeluserlog.go
2023-11-06 18:26:39 +08:00

59 lines
1.7 KiB
Go

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
}
}