package middleware import ( "github.com/gin-gonic/gin" "hongze/hongze_yb_en_api/controller/resp" "hongze/hongze_yb_en_api/models/english_report_email" "hongze/hongze_yb_en_api/models/session" "hongze/hongze_yb_en_api/services" "hongze/hongze_yb_en_api/utils" ) func Token() gin.HandlerFunc { return func(c *gin.Context) { token := c.Request.Header.Get("Authorization") if token == "" { token = c.DefaultQuery("authorization", "") if token == "" { token = c.DefaultQuery("Authorization", "") } } if token == "" { resp.TokenError(nil, "未登录或非法访问", "未登录或非法访问", c) c.Abort() return } sessionInfo, err := session.GetTokenByToken(token) if err != nil { if err == utils.ErrNoRow { resp.TokenError(nil, "信息已变更,请重新登陆!", "找不到对应session", c) c.Abort() return } resp.TokenError(nil, "网络异常,请稍后重试!", err.Error(), c) c.Abort() return } if sessionInfo == nil { resp.TokenError(nil, "网络异常,请稍后重试1038!", "找不到对应session", c) c.Abort() return } emailItem := english_report_email.Email{Id: uint(sessionInfo.UserId)} userEmail, err := emailItem.GetById(sessionInfo.UserId) if err != nil && err != utils.ErrNoRow { resp.TokenError(nil, "数据异常!", "userID查询用户信息错误", c) c.Abort() return } if userEmail.Enable == 0 || userEmail.Status == 3 { resp.TokenError(nil, "User disabled", "User disabled", c) c.Abort() return } userInfo := services.UserInfo{ Email: *userEmail, } userInfo.LoginToken = token c.Set("userInfo", userInfo) c.Next() } }