logon_middeware.go 591 B

1234567891011121314151617181920212223242526272829
  1. package middlewares
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "project_management/app/e"
  5. "project_management/utils"
  6. "strings"
  7. )
  8. func LogonMiddeware() gin.HandlerFunc {
  9. return func(c *gin.Context) {
  10. header := c.GetHeader("Authorization")
  11. split := strings.Split(header, ":")
  12. if header == "" {
  13. e.ResponseWithMsg(c, e.TokenIsInvalid, e.TokenIsInvalid.GetMsg())
  14. c.Abort()
  15. } else {
  16. user, err := utils.ParseToken(split[1])
  17. if err != e.SUCCESS {
  18. e.ResponseWithMsg(c, err, err.GetMsg())
  19. c.Abort()
  20. } else {
  21. c.Set("phone", user.UserName)
  22. c.Next()
  23. }
  24. }
  25. }
  26. }