account.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package models
  2. import (
  3. "context"
  4. mhayaTime "github.com/mhaya/extend/time"
  5. "github.com/mhaya/game/game_cluster/internal/code"
  6. "github.com/mhaya/game/game_cluster/internal/constant"
  7. "github.com/mhaya/game/game_cluster/internal/guid"
  8. "github.com/mhaya/game/game_cluster/internal/mdb"
  9. "github.com/mhaya/game/game_cluster/internal/param"
  10. clog "github.com/mhaya/logger"
  11. "go.mongodb.org/mongo-driver/bson"
  12. )
  13. type Account struct {
  14. UserName string `json:"username" bson:"userName"`
  15. Platform string `json:"platform" bson:"platform"`
  16. Channel string `json:"channel" bson:"channel"`
  17. OpenId string `json:"-" bson:"openId"`
  18. JoinIp string `json:"joinip" bson:"JoinIp"`
  19. JoinTime int64 `json:"jointime" bson:"JoinTime"`
  20. }
  21. func (ac *Account) AccountRegisterOrLogin(req *param.LoginReq) (*Account, int32) {
  22. var account Account
  23. findFilter := bson.M{"openId": req.OpenID}
  24. mdb.MDB.Collection(constant.CNameAccount).FindOne(context.Background(), findFilter).Decode(&account)
  25. if account.OpenId != "" {
  26. return &account, code.OK
  27. }
  28. devAccountTable := &Account{
  29. UserName: guid.Next(),
  30. OpenId: req.OpenID,
  31. Platform: req.Platform,
  32. Channel: req.Channel,
  33. JoinIp: req.IP,
  34. JoinTime: mhayaTime.Now().Unix(),
  35. }
  36. _, err := mdb.MDB.Collection(constant.CNameAccount).InsertOne(context.Background(), devAccountTable)
  37. if err != nil {
  38. clog.Error("Failed to AccountRegisterOrLogin request: %v err = %v", devAccountTable, err.Error())
  39. return nil, code.LoginError
  40. }
  41. //统计新注册
  42. SetDailyRecordNewUserRegisterHash(req.Platform, req.Channel, devAccountTable.UserName, req.IP, DailyRecordNewRegistered)
  43. //设置IP 规则
  44. SetPlayerBlacklistIpRecord(req.IP)
  45. return devAccountTable, code.OK
  46. }