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.
60 lines
1.5 KiB
60 lines
1.5 KiB
package middleware
|
|
|
|
import (
|
|
"github.com/gin-gonic/gin"
|
|
cache2 "recook/internal/cache"
|
|
"recook/internal/v2/lib/back"
|
|
"recook/internal/v2/lib/cache"
|
|
"recook/internal/v2/model/recook/user"
|
|
"strconv"
|
|
)
|
|
|
|
func AppAuthorize() gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
deviceType := cache2.GetDeviceType(c)
|
|
c.Set("deviceType", deviceType)
|
|
id, _ := strconv.Atoi(c.Request.Header.Get("X-Recook-ID"))
|
|
if id <= 0 {
|
|
back.Fail(c, "游客无法使用该功能,请登录")
|
|
c.Abort()
|
|
return
|
|
}
|
|
token := c.Request.Header.Get("X-Recook-Token")
|
|
if token == "" {
|
|
back.Fail(c, "游客无法使用该功能,请登录")
|
|
c.Abort()
|
|
return
|
|
}
|
|
cacheToken := cache.GetUserLoginCache(uint(id), deviceType)
|
|
userId := uint(0)
|
|
if len(cacheToken) == 0 {
|
|
// 缓存被清理了
|
|
recookUserLoginModel := user.RecookUserLoginModel{}
|
|
tokenInfo := recookUserLoginModel.FindByIdAndDeviceType(uint(id), deviceType)
|
|
if tokenInfo.Id <= 0 {
|
|
// 账号错误
|
|
back.Unauthorized(c)
|
|
c.Abort()
|
|
return
|
|
}
|
|
cache.SetUserLoginCache(tokenInfo.Id, tokenInfo.Token, deviceType)
|
|
userId = tokenInfo.UserId
|
|
cacheToken = tokenInfo.Token
|
|
}
|
|
|
|
if cacheToken == token {
|
|
if userId == 0 {
|
|
recookUserLoginModel := user.RecookUserLoginModel{}
|
|
tokenInfo := recookUserLoginModel.FindByIdAndDeviceType(uint(id), deviceType)
|
|
userId = tokenInfo.UserId
|
|
}
|
|
c.Set("appUserId", userId)
|
|
c.Next()
|
|
} else {
|
|
back.Unauthorized(c)
|
|
c.Abort()
|
|
return
|
|
}
|
|
}
|
|
}
|