You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
49 lines
993 B
49 lines
993 B
package middleware
|
|
|
|
import (
|
|
"github.com/gin-gonic/gin"
|
|
"recook/internal/v2/lib/back"
|
|
"recook/internal/v2/lib/cache"
|
|
"recook/internal/v2/model/gys/user"
|
|
"strconv"
|
|
)
|
|
|
|
func GysAuthorize() gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
id, _ := strconv.Atoi(c.Request.Header.Get("X-Recook-GysID"))
|
|
if id <= 0 {
|
|
back.Unauthorized(c)
|
|
c.Abort()
|
|
return
|
|
}
|
|
token := c.Request.Header.Get("X-Recook-GysToken")
|
|
if token == "" {
|
|
back.Unauthorized(c)
|
|
c.Abort()
|
|
return
|
|
}
|
|
cacheToken := cache.GetGysLoginCache(uint(id))
|
|
if len(cacheToken) == 0 {
|
|
// 缓存被清理了
|
|
tokenInfo := (&user.GysUsersTokenModel{}).FindByUserId(uint(id))
|
|
if tokenInfo.Id <= 0 {
|
|
// 账号错误
|
|
back.Unauthorized(c)
|
|
c.Abort()
|
|
return
|
|
}
|
|
cacheToken = tokenInfo.Token
|
|
}
|
|
|
|
if cacheToken == token {
|
|
gysInfo := (&user.GysUsersModel{}).FindById(uint(id))
|
|
c.Set("gysInfo", gysInfo)
|
|
c.Next()
|
|
} else {
|
|
back.Unauthorized(c)
|
|
c.Abort()
|
|
return
|
|
}
|
|
}
|
|
}
|