account.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. "go.mongodb.org/mongo-driver/bson"
  11. )
  12. type Account struct {
  13. UserName string `json:"username" bson:"userName"`
  14. Platform string `json:"platform" bson:"platform"`
  15. Channel string `json:"channel" bson:"channel"`
  16. OpenId string `json:"-" bson:"openId"`
  17. JoinIp string `json:"joinip" bson:"JoinIp"`
  18. JoinTime int64 `json:"jointime" bson:"JoinTime"`
  19. }
  20. func (ac *Account) AccountRegisterOrLogin(req *param.LoginReq) (*Account, int32) {
  21. var account Account
  22. findFilter := bson.M{"openId": req.OpenID}
  23. mdb.MDB.Collection(constant.CNameAccount).FindOne(context.Background(), findFilter).Decode(&account)
  24. if account.OpenId != "" {
  25. return &account, code.OK
  26. }
  27. devAccountTable := &Account{
  28. UserName: guid.Next(),
  29. OpenId: req.OpenID,
  30. Platform: req.Platform,
  31. Channel: req.Channel,
  32. JoinIp: req.IP,
  33. JoinTime: mhayaTime.Now().Unix(),
  34. }
  35. _, err := mdb.MDB.Collection(constant.CNameAccount).InsertOne(context.Background(), devAccountTable)
  36. if err != nil {
  37. return nil, code.LoginError
  38. }
  39. //统计新注册
  40. SetDailyRecord(req.Platform, req.Channel, DailyRecordNewRegistered, 1)
  41. return devAccountTable, code.OK
  42. }