open_auth.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package middleware
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "hongze/hongze_yb_en_api/models/english_report_email"
  5. "hongze/hongze_yb_en_api/models/session"
  6. "hongze/hongze_yb_en_api/services"
  7. )
  8. // OpenToken 开放Token, 可登录可不登录
  9. func OpenToken() gin.HandlerFunc {
  10. return func(c *gin.Context) {
  11. token := c.Request.Header.Get("Authorization")
  12. if token == "" {
  13. token = c.DefaultQuery("authorization", "")
  14. if token == "" {
  15. token = c.DefaultQuery("Authorization", "")
  16. }
  17. }
  18. if token == "" {
  19. c.Next()
  20. return
  21. }
  22. sessionInfo, err := session.GetTokenByToken(token)
  23. if err != nil || sessionInfo == nil {
  24. c.Next()
  25. return
  26. }
  27. emailItem := english_report_email.Email{Id: uint(sessionInfo.UserId)}
  28. userEmail, err := emailItem.GetById(sessionInfo.UserId)
  29. if err != nil {
  30. c.Next()
  31. return
  32. }
  33. if userEmail.Enable == 0 || userEmail.Status == 3 {
  34. c.Next()
  35. return
  36. }
  37. userInfo := services.UserInfo{
  38. Email: *userEmail,
  39. }
  40. userInfo.LoginToken = token
  41. c.Set("userInfo", userInfo)
  42. c.Next()
  43. }
  44. }