auth.go 713 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. package param
  2. import (
  3. "context"
  4. "encoding/json"
  5. )
  6. // GetUserID 为userid
  7. func GetUserID(c context.Context) int64 {
  8. userID := c.Value("user_id")
  9. if _, ok := userID.(int64); ok {
  10. return userID.(int64)
  11. }
  12. return 0
  13. }
  14. func GetAccountID(c context.Context) int64 {
  15. accountID := c.Value("account_id")
  16. if _, ok := accountID.(int64); ok {
  17. return accountID.(int64)
  18. }
  19. return 0
  20. }
  21. func GetAuthToken(c context.Context) string {
  22. token := c.Value("token")
  23. if _, ok := token.(string); ok {
  24. return token.(string)
  25. }
  26. return ""
  27. }
  28. func GetJwtClaims(c context.Context) json.RawMessage {
  29. claim := c.Value("claim")
  30. if _, ok := claim.(json.RawMessage); ok {
  31. return claim.(json.RawMessage)
  32. }
  33. return []byte{}
  34. }