account.go 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. initdata "github.com/mhaya/game/game_cluster/internal/third/tg_sign"
  11. clog "github.com/mhaya/logger"
  12. "go.mongodb.org/mongo-driver/bson"
  13. "go.mongodb.org/mongo-driver/mongo"
  14. )
  15. type Account struct {
  16. UserName string `json:"username" bson:"userName"`
  17. Platform string `json:"platform" bson:"platform"`
  18. Account string `json:"account" bson:"account"`
  19. Channel string `json:"channel" bson:"channel"`
  20. IsPremium bool `json:"isPremium" bson:"isPremium"`
  21. LoginIp string `json:"loginIp" bson:"loginIp"`
  22. OpenId string `json:"openId" bson:"openId"`
  23. JoinIp string `json:"joinip" bson:"JoinIp"`
  24. JoinTime int64 `json:"jointime" bson:"JoinTime"`
  25. }
  26. func (ac *Account) AccountRegisterOrLogin(req *param.LoginReq) (*Account, int32) {
  27. var account Account
  28. findFilter := bson.M{"openId": req.OpenID}
  29. err := mdb.MDB.Collection(constant.CNameAccount).FindOne(context.Background(), findFilter).Decode(&account)
  30. if err != nil && err != mongo.ErrNoDocuments {
  31. clog.Errorf("Failed to AccountRegisterOrLogin select err request: %v err = %v", req, err.Error())
  32. return nil, code.LoginError
  33. }
  34. user := initdata.GetUserInfo(req.Sign)
  35. //设置IP 规则
  36. num, ok := SetPlayerBlacklistIpRecord(req.IP, req.OpenID)
  37. if ok {
  38. clog.Errorf("Failed to SetPlayerBlacklistIpRecord err request: %v,num:%v", req, num)
  39. return nil, code.LoginError
  40. }
  41. if account.OpenId != "" {
  42. if account.Platform == "on" {
  43. //统计新注册
  44. SetAppointDailyRecordNewUserRegisterHash(req.Platform, req.Channel, account.UserName, account.JoinIp, mhayaTime.CreateFromTimestamp(account.JoinTime).Unix(), DailyRecordNewRegistered)
  45. }
  46. if (len(user.Username) > 0 && account.Account != user.Username) || user.IsPremium != account.IsPremium || req.IP != account.LoginIp || account.Platform != req.Platform {
  47. account.Account = user.Username
  48. account.IsPremium = user.IsPremium
  49. account.LoginIp = req.IP
  50. account.Platform = req.Platform
  51. mdb.MDB.Collection(constant.CNameAccount).UpdateOne(context.Background(), findFilter, bson.M{"$set": bson.M{"platform": req.Platform, "account": user.Username, "isPremium": user.IsPremium, "loginIp": req.IP}})
  52. }
  53. return &account, code.OK
  54. }
  55. devAccountTable := &Account{
  56. UserName: guid.Next(),
  57. OpenId: req.OpenID,
  58. Platform: req.Platform,
  59. Channel: req.Channel,
  60. Account: user.Username,
  61. IsPremium: user.IsPremium,
  62. LoginIp: req.IP,
  63. JoinIp: req.IP,
  64. JoinTime: mhayaTime.Now().Unix(),
  65. }
  66. _, err = mdb.MDB.Collection(constant.CNameAccount).InsertOne(context.Background(), devAccountTable)
  67. if err != nil {
  68. clog.Errorf("Failed to AccountRegisterOrLogin request: %v err = %v", devAccountTable, err.Error())
  69. return nil, code.LoginError
  70. }
  71. //统计新注册
  72. SetDailyRecordNewUserRegisterHash(req.Platform, req.Channel, devAccountTable.UserName, req.IP, DailyRecordNewRegistered)
  73. return devAccountTable, code.OK
  74. }