quick_sdk.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package sdk
  2. import (
  3. mhayaGin "github.com/mhaya/components/gin"
  4. cerror "github.com/mhaya/error"
  5. cstring "github.com/mhaya/extend/string"
  6. cfacade "github.com/mhaya/facade"
  7. "github.com/mhaya/game/game_cluster/internal/code"
  8. "github.com/mhaya/game/game_cluster/internal/data"
  9. "github.com/mhaya/game/game_cluster/internal/mdb/models"
  10. "github.com/mhaya/game/game_cluster/internal/param"
  11. sessionKey "github.com/mhaya/game/game_cluster/internal/session_key"
  12. "github.com/mhaya/game/game_cluster/internal/token"
  13. clog "github.com/mhaya/logger"
  14. )
  15. type quickSdk struct {
  16. app cfacade.IApplication
  17. }
  18. func (quickSdk) SdkId() int {
  19. return QuickSDK
  20. }
  21. func (p quickSdk) Login(config *data.SdkConfigRow, params Params, callback Callback) {
  22. openid, found := params.GetString("openid")
  23. if found == false || cstring.IsBlank(openid) {
  24. err := cerror.Error("openid is nil")
  25. callback(code.LoginError, nil, err)
  26. return
  27. }
  28. ip, found := params.GetString("ip")
  29. if found == false || cstring.IsBlank(ip) {
  30. err := cerror.Error("ip is nil")
  31. callback(code.LoginError, nil, err)
  32. return
  33. }
  34. plt, found := params.GetString("platform")
  35. if found == false || cstring.IsBlank(plt) {
  36. err := cerror.Error("platform is nil")
  37. callback(code.LoginError, nil, err)
  38. return
  39. }
  40. channel, found := params.GetString("channel")
  41. if found == false || cstring.IsBlank(channel) {
  42. err := cerror.Error("channel is nil")
  43. callback(code.LoginError, nil, err)
  44. return
  45. }
  46. sign, _ := params.GetString("sign")
  47. req := &param.LoginReq{
  48. OpenID: openid,
  49. IP: ip,
  50. Platform: plt,
  51. Channel: channel,
  52. Sign: sign,
  53. }
  54. acc := &models.Account{}
  55. account, err := acc.AccountRegisterOrLogin(req)
  56. if err > 0 {
  57. callback(code.LoginError, nil)
  58. return
  59. }
  60. nodeId, ok := SetNode(p.app)
  61. if code.IsFail(ok) {
  62. clog.Warnf("[RegisterAccount] openID = %s,nodeID=%v, errCode = %v", openid, nodeId, err)
  63. callback(code.LoginError, nil)
  64. return
  65. }
  66. targetPath := cfacade.NewChildPath(nodeId, "player"+nodeId, account.UserName)
  67. callback(code.OK, map[string]string{
  68. sessionKey.PlayerID: account.UserName, //返回 quick的uid做为 open id
  69. sessionKey.OpenID: openid,
  70. sessionKey.TargetPath: targetPath,
  71. })
  72. }
  73. func (p quickSdk) Reconnect(token *token.Token) (int32, *token.Token) {
  74. list := p.app.Discovery().ListByType("game")
  75. targetPath, err := cfacade.ToActorPath(token.TargetPath)
  76. if err != nil {
  77. clog.Warnf("[Reconnect] Target path error. targetPath = %v, error = %v", token.TargetPath, err)
  78. return code.AccountAuthFail, nil
  79. }
  80. isNew := false
  81. for _, v := range list {
  82. if v.GetNodeId() == targetPath.NodeID {
  83. isNew = true
  84. break
  85. }
  86. }
  87. if !isNew {
  88. nodeId, ok := SetNode(p.app)
  89. if code.IsFail(ok) {
  90. clog.Warnf("[RegisterAccount] openID = %s,nodeID=%v, errCode = %v", token.OpenID, nodeId, err)
  91. return code.AccountAuthFail, nil
  92. }
  93. path := cfacade.NewChildPath(nodeId, "player"+nodeId, token.PlayerID)
  94. token.TargetPath = path
  95. }
  96. return code.OK, token
  97. }
  98. func (p quickSdk) PayCallback(config *data.SdkConfigRow, c *mhayaGin.Context) {
  99. // TODO 这里实现quick sdk 支付回调的逻辑
  100. c.RenderHTML("FAIL")
  101. }