disconnectlogic.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package logic
  2. import (
  3. "context"
  4. "github.com/golang-jwt/jwt/v4"
  5. "github.com/pkg/errors"
  6. "ylink/comm/globalkey"
  7. "ylink/comm/jwtkey"
  8. "ylink/comm/result"
  9. "ylink/core/inner/rpc/inner"
  10. "ylink/flowsrv/rpc/internal/mgr"
  11. "ylink/flowsrv/rpc/internal/svc"
  12. "ylink/flowsrv/rpc/pb"
  13. "github.com/zeromicro/go-zero/core/logx"
  14. )
  15. type DisconnectLogic struct {
  16. ctx context.Context
  17. svcCtx *svc.ServiceContext
  18. logx.Logger
  19. }
  20. func NewDisconnectLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DisconnectLogic {
  21. return &DisconnectLogic{
  22. ctx: ctx,
  23. svcCtx: svcCtx,
  24. Logger: logx.WithContext(ctx),
  25. }
  26. }
  27. func (l *DisconnectLogic) Disconnect(in *pb.CommandReq) (*pb.CommandResp, error) {
  28. uid, gameId, err := l.checkAuth(in)
  29. if err != nil {
  30. return &pb.CommandResp{
  31. Code: result.TokenParseError,
  32. Msg: err.Error(),
  33. Data: nil,
  34. }, err
  35. }
  36. _, err = l.svcCtx.InnerRpc.NotifyUserOffline(l.ctx, &inner.NotifyUserStatusReq{
  37. Type: in.Type,
  38. Uid: uid,
  39. GameId: gameId,
  40. })
  41. if err != nil {
  42. return &pb.CommandResp{
  43. Code: result.ServerCommonError,
  44. Msg: err.Error(),
  45. Data: nil,
  46. }, err
  47. }
  48. var flowId string
  49. if in.Type == globalkey.CONNECT_TYPE_PLAYER {
  50. flowId = gameId + "_" + uid
  51. } else {
  52. flowId = uid
  53. }
  54. mgr.GetFlowMgrInstance().UnRegister(flowId)
  55. return &pb.CommandResp{
  56. Code: result.Ok,
  57. Msg: "success",
  58. Data: nil,
  59. }, nil
  60. }
  61. func (l *DisconnectLogic) checkAuth(in *pb.CommandReq) (string, string, error) {
  62. token, err := jwt.Parse(in.AccessToken, func(token *jwt.Token) (i interface{}, err error) {
  63. return []byte(l.svcCtx.Config.JwtAuth.AccessSecret), nil
  64. })
  65. uid := ""
  66. gameId := ""
  67. if token.Valid {
  68. //将获取的token中的Claims强转为MapClaims
  69. claims, _ := token.Claims.(jwt.MapClaims)
  70. if in.Type == globalkey.CONNECT_TYPE_PLAYER {
  71. uid = claims[jwtkey.PlayerId].(string)
  72. gameId = claims[jwtkey.GameId].(string)
  73. } else {
  74. uid = claims[jwtkey.CsId].(string)
  75. }
  76. return uid, gameId, nil
  77. } else if ve, ok := err.(*jwt.ValidationError); ok {
  78. if ve.Errors&jwt.ValidationErrorMalformed != 0 {
  79. return uid, gameId, errors.Wrap(result.NewErrCode(result.TokenParseError), "")
  80. } else if ve.Errors&(jwt.ValidationErrorExpired|jwt.ValidationErrorNotValidYet) != 0 {
  81. // Token is either expired or not active yet
  82. return uid, gameId, errors.Wrap(result.NewErrCode(result.TokenExpireError), "")
  83. } else {
  84. return uid, gameId, errors.Wrap(result.NewErrCode(result.TokenParseError), "")
  85. }
  86. } else {
  87. return uid, gameId, errors.Wrap(result.NewErrCode(result.TokenParseError), "")
  88. }
  89. }