admin_middleware.go 773 B

123456789101112131415161718192021222324252627282930
  1. package middlewares
  2. import (
  3. "github.com/bytedance/sonic"
  4. "github.com/gin-gonic/gin"
  5. "lot_interlligentControl/app/e"
  6. "lot_interlligentControl/nats"
  7. "time"
  8. )
  9. func AdminMiddleware() gin.HandlerFunc {
  10. return func(c *gin.Context) {
  11. header := c.GetHeader("Authorization")
  12. request, err := nats.Nats.Request("login_token_validation", []byte(header), 3*time.Second)
  13. if err != nil {
  14. e.ResponseWithMsg(c, e.TokenIsInvalid, e.TokenIsInvalid.GetMsg())
  15. } else {
  16. var response UserResponse
  17. sonic.Unmarshal(request.Data, &response)
  18. users := response.User.(map[string]interface{})
  19. role := users["role"].(string)
  20. if role == "admin" {
  21. c.Next()
  22. } else {
  23. e.ResponseWithMsg(c, e.TokenIsInvalid, e.TokenIsInvalid.GetMsg())
  24. c.Abort()
  25. }
  26. }
  27. }
  28. }