91 lines
2.3 KiB
Go
91 lines
2.3 KiB
Go
package user
|
|
|
|
import (
|
|
"go_dreamfactory/comm"
|
|
"go_dreamfactory/lego/core"
|
|
"go_dreamfactory/lego/sys/log"
|
|
"go_dreamfactory/modules"
|
|
"go_dreamfactory/pb"
|
|
"go_dreamfactory/sys/db"
|
|
"go_dreamfactory/utils"
|
|
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
"go.mongodb.org/mongo-driver/x/bsonx"
|
|
)
|
|
|
|
// 记录一些扩展数据
|
|
type ModelExpand struct {
|
|
modules.MCompModel
|
|
module *User
|
|
}
|
|
|
|
func (this *ModelExpand) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) {
|
|
this.TableName = comm.TableUserExpand
|
|
err = this.MCompModel.Init(service, module, comp, options)
|
|
this.module = module.(*User)
|
|
this.DB.CreateIndex(core.SqlTable(this.TableName), mongo.IndexModel{
|
|
Keys: bsonx.Doc{{Key: "uid", Value: bsonx.Int32(1)}},
|
|
})
|
|
return
|
|
}
|
|
|
|
//获取用户通过扩展表
|
|
func (this *ModelExpand) GetUserExpand(uid string) (result *pb.DBUserExpand, err error) {
|
|
result = &pb.DBUserExpand{}
|
|
if db.IsCross() {
|
|
if model, err := this.module.GetDBModuleByUid(uid, this.TableName, this.Expired); err != nil {
|
|
this.module.Error("Cross GetDBModuleByUid", log.Fields{"uid": uid})
|
|
return result, err
|
|
} else {
|
|
if err = model.Get(uid, result); err != nil && mongo.ErrNoDocuments != err {
|
|
this.module.Error("Cross Get", log.Fields{"uid": uid})
|
|
return result, err
|
|
}
|
|
}
|
|
} else {
|
|
if err = this.Get(uid, result); err != nil && mongo.ErrNoDocuments != err {
|
|
this.module.Error("Get", log.Fields{"uid": uid})
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
//修改用户扩展数据
|
|
func (this *ModelExpand) ChangeUserExpand(uid string, value map[string]interface{}) (err error) {
|
|
if len(value) == 0 {
|
|
return nil
|
|
}
|
|
var (
|
|
model *db.DBModel
|
|
)
|
|
if db.IsCross() {
|
|
if model, err = this.module.GetDBModuleByUid(uid, this.TableName, this.Expired); err == nil {
|
|
return model.Change(uid, value)
|
|
} else {
|
|
this.module.Errorln(err)
|
|
return err
|
|
}
|
|
}
|
|
|
|
return this.Change(uid, value)
|
|
|
|
}
|
|
|
|
// 累计登录天数
|
|
func (this *ModelExpand) updateLoginDay(uid string, timestamp int64) (err error) {
|
|
var de *pb.DBUserExpand
|
|
if de, err = this.GetUserExpand(uid); err == nil {
|
|
count := de.LoginAddCount + 1
|
|
update := map[string]interface{}{
|
|
"loginAddCount": count,
|
|
}
|
|
|
|
// 更新连续等登录天数
|
|
if utils.IsYestoday(timestamp) {
|
|
update["loginContinueCount"] = de.LoginContinueCount + 1
|
|
}
|
|
err = this.ChangeUserExpand(uid, update)
|
|
}
|
|
return
|
|
}
|