account.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. "go.mongodb.org/mongo-driver/mongo"
  13. )
  14. type Account struct {
  15. UserName string `json:"username" bson:"userName"`
  16. Platform string `json:"platform" bson:"platform"`
  17. Channel string `json:"channel" bson:"channel"`
  18. OpenId string `json:"openId" bson:"openId"`
  19. JoinIp string `json:"joinip" bson:"JoinIp"`
  20. JoinTime int64 `json:"jointime" bson:"JoinTime"`
  21. }
  22. func (ac *Account) AccountRegisterOrLogin(req *param.LoginReq) (*Account, int32) {
  23. var account Account
  24. findFilter := bson.M{"openId": req.OpenID}
  25. err := mdb.MDB.Collection(constant.CNameAccount).FindOne(context.Background(), findFilter).Decode(&account)
  26. if err != nil && err != mongo.ErrNoDocuments {
  27. clog.Errorf("Failed to AccountRegisterOrLogin select err request: %v err = %v", req, err.Error())
  28. return nil, code.LoginError
  29. }
  30. //设置IP 规则
  31. num, ok := SetPlayerBlacklistIpRecord(req.IP, req.OpenID)
  32. if ok {
  33. clog.Errorf("Failed to SetPlayerBlacklistIpRecord err request: %v,num:%v", req, num)
  34. return nil, code.LoginError
  35. }
  36. if account.OpenId != "" {
  37. if account.Platform == "on" {
  38. mdb.MDB.Collection(constant.CNameAccount).UpdateOne(context.Background(), findFilter, bson.M{"platform": req.Platform})
  39. //统计新注册
  40. SetAppointDailyRecordNewUserRegisterHash(req.Platform, req.Channel, account.UserName, account.JoinIp, mhayaTime.CreateFromTimestamp(account.JoinTime).Unix(), DailyRecordNewRegistered)
  41. }
  42. return &account, code.OK
  43. }
  44. devAccountTable := &Account{
  45. UserName: guid.Next(),
  46. OpenId: req.OpenID,
  47. Platform: req.Platform,
  48. Channel: req.Channel,
  49. JoinIp: req.IP,
  50. JoinTime: mhayaTime.Now().Unix(),
  51. }
  52. _, err = mdb.MDB.Collection(constant.CNameAccount).InsertOne(context.Background(), devAccountTable)
  53. if err != nil {
  54. clog.Errorf("Failed to AccountRegisterOrLogin request: %v err = %v", devAccountTable, err.Error())
  55. return nil, code.LoginError
  56. }
  57. //统计新注册
  58. SetDailyRecordNewUserRegisterHash(req.Platform, req.Channel, devAccountTable.UserName, req.IP, DailyRecordNewRegistered)
  59. return devAccountTable, code.OK
  60. }