middleware.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package middleware
  2. import (
  3. "context"
  4. "encoding/json"
  5. "net/url"
  6. context2 "git.ikuban.com/server/kratos-utils/http/context"
  7. "github.com/go-kratos/kratos/v2/middleware"
  8. "google.golang.org/grpc/metadata"
  9. )
  10. //GetUserID 为userid
  11. func GetUserID(c context.Context) string {
  12. userID := c.Value("user_id")
  13. if _, ok := userID.(string); ok {
  14. return userID.(string)
  15. }
  16. return ""
  17. }
  18. func GetAccountID(c context.Context) string {
  19. accountID := c.Value("account_id")
  20. if _, ok := accountID.(string); ok {
  21. return accountID.(string)
  22. }
  23. return ""
  24. }
  25. func GetAuthToken(c context.Context) string {
  26. token := c.Value("auth_token")
  27. if _, ok := token.(string); ok {
  28. return token.(string)
  29. }
  30. return ""
  31. }
  32. func GetJwtClaims(c context.Context) json.RawMessage {
  33. claim := c.Value("jwt_claims")
  34. if _, ok := claim.(json.RawMessage); ok {
  35. return claim.(json.RawMessage)
  36. }
  37. return []byte{}
  38. }
  39. func GetPageOffset(c context.Context) (string, int64) {
  40. nextId := c.Value("nextId")
  41. offset := c.Value("offset")
  42. _nextId := ""
  43. _offset := int64(0)
  44. if _, ok := nextId.(string); ok {
  45. _nextId = nextId.(string)
  46. }
  47. if _, ok := offset.(int64); ok {
  48. _offset = offset.(int64)
  49. }
  50. return _nextId, _offset
  51. }
  52. func GrpcValue(handler middleware.Handler) middleware.Handler {
  53. return func(ctx context.Context, req interface{}) (reply interface{}, err error) {
  54. if md, ok := metadata.FromIncomingContext(ctx); ok {
  55. userID := md.Get("user_id")
  56. if len(userID) > 0 {
  57. ctx = context2.AppendToContext(ctx, "userId", userID[0])
  58. }
  59. accountID := md.Get("account_id")
  60. if len(accountID) > 0 {
  61. ctx = context2.AppendToContext(ctx, "accountId", accountID[0])
  62. }
  63. claimMap := md.Get("jwt_claims")
  64. if len(claimMap) > 0 {
  65. _claimMap := json.RawMessage(claimMap[0])
  66. ctx = context2.AppendToContext(ctx, "jwt_claims", _claimMap)
  67. }
  68. token := md.Get("auth_token")
  69. if len(token) > 0 {
  70. ctx = context2.AppendToContext(ctx, "auth_token", token[0])
  71. }
  72. //nextId := md.Get("nextId")
  73. //if len(nextId) > 0 {
  74. // ctx = context2.AppendToContext(ctx, "nextId", nextId[0])
  75. //}
  76. //
  77. //offset := md.Get("offset")
  78. //if len(offset) > 0 {
  79. // _offset, _ := strconv.ParseInt(offset[0], 10, 64)
  80. // ctx = context2.AppendToContext(ctx, "offset", _offset)
  81. //}
  82. query := md.Get("query")
  83. if len(query) > 0 {
  84. _query, _ := url.ParseQuery(query[0])
  85. ctx = context2.AppendToContext(ctx, "query", _query)
  86. }
  87. referer := md.Get("referer")
  88. if len(referer) > 0 {
  89. ctx = context2.AppendToContext(ctx, "referer", referer[0])
  90. }
  91. uri := md.Get("uri")
  92. if len(uri) > 0 {
  93. ctx = context2.AppendToContext(ctx, "uri", uri[0])
  94. }
  95. userAgent := md.Get("user_agent")
  96. if len(uri) > 0 {
  97. ctx = context2.AppendToContext(ctx, "userAgent", userAgent[0])
  98. }
  99. }
  100. return handler(ctx, req)
  101. }
  102. }