1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- package middleware
- import (
- "github.com/gin-gonic/gin"
- "hongze/hongze_yb_en_api/models/english_report_email"
- "hongze/hongze_yb_en_api/models/session"
- "hongze/hongze_yb_en_api/services"
- )
- // OpenToken 开放Token, 可登录可不登录
- func OpenToken() 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 == "" {
- c.Next()
- return
- }
- sessionInfo, err := session.GetTokenByToken(token)
- if err != nil || sessionInfo == nil {
- c.Next()
- return
- }
- emailItem := english_report_email.Email{Id: uint(sessionInfo.UserId)}
- userEmail, err := emailItem.GetById(sessionInfo.UserId)
- if err != nil {
- c.Next()
- return
- }
- if userEmail.Enable == 0 || userEmail.Status == 3 {
- c.Next()
- return
- }
- userInfo := services.UserInfo{
- Email: *userEmail,
- }
- userInfo.LoginToken = token
- c.Set("userInfo", userInfo)
- c.Next()
- }
- }
|