package models import ( "context" mhayaTime "github.com/mhaya/extend/time" "github.com/mhaya/game/game_cluster/internal/code" "github.com/mhaya/game/game_cluster/internal/constant" "github.com/mhaya/game/game_cluster/internal/guid" "github.com/mhaya/game/game_cluster/internal/mdb" "github.com/mhaya/game/game_cluster/internal/param" clog "github.com/mhaya/logger" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/mongo" ) type Account struct { UserName string `json:"username" bson:"userName"` Platform string `json:"platform" bson:"platform"` Channel string `json:"channel" bson:"channel"` OpenId string `json:"openId" bson:"openId"` JoinIp string `json:"joinip" bson:"JoinIp"` JoinTime int64 `json:"jointime" bson:"JoinTime"` } func (ac *Account) AccountRegisterOrLogin(req *param.LoginReq) (*Account, int32) { var account Account findFilter := bson.M{"openId": req.OpenID} err := mdb.MDB.Collection(constant.CNameAccount).FindOne(context.Background(), findFilter).Decode(&account) if err != nil && err != mongo.ErrNoDocuments { clog.Errorf("Failed to AccountRegisterOrLogin select err request: %v err = %v", req, err.Error()) return nil, code.LoginError } //设置IP 规则 num, ok := SetPlayerBlacklistIpRecord(req.IP, req.OpenID) if ok { clog.Errorf("Failed to SetPlayerBlacklistIpRecord err request: %v,num:%v", req, num) return nil, code.LoginError } if account.OpenId != "" { if account.Platform == "on" { mdb.MDB.Collection(constant.CNameAccount).UpdateOne(context.Background(), findFilter, bson.M{"platform": req.Platform}) //统计新注册 SetAppointDailyRecordNewUserRegisterHash(req.Platform, req.Channel, account.UserName, account.JoinIp, mhayaTime.CreateFromTimestamp(account.JoinTime).Unix(), DailyRecordNewRegistered) } return &account, code.OK } devAccountTable := &Account{ UserName: guid.Next(), OpenId: req.OpenID, Platform: req.Platform, Channel: req.Channel, JoinIp: req.IP, JoinTime: mhayaTime.Now().Unix(), } _, err = mdb.MDB.Collection(constant.CNameAccount).InsertOne(context.Background(), devAccountTable) if err != nil { clog.Errorf("Failed to AccountRegisterOrLogin request: %v err = %v", devAccountTable, err.Error()) return nil, code.LoginError } //统计新注册 SetDailyRecordNewUserRegisterHash(req.Platform, req.Channel, devAccountTable.UserName, req.IP, DailyRecordNewRegistered) return devAccountTable, code.OK }