feat:完善项目

This commit is contained in:
henry
2021-11-08 15:52:46 +08:00
parent 1502076841
commit 1cc95fb5ca
16 changed files with 154 additions and 64 deletions

View File

@ -3,13 +3,9 @@ package router
import (
"ArmedPolice/app/service"
"ArmedPolice/config"
cache2 "ArmedPolice/serve/cache"
"ArmedPolice/utils"
"fmt"
"net/http"
"time"
"github.com/gin-gonic/gin"
"net/http"
)
// SkipperURL 跳过验证
@ -40,34 +36,10 @@ func NeedLogin(skipperURL ...SkipperURL) gin.HandlerFunc {
c.Abort()
return
}
tokenInfo := utils.JWTDecrypt(token)
session, err := service.NewAuthToken(token).Auth()
if tokenInfo == nil || len(tokenInfo) <= 0 {
c.JSON(http.StatusUnauthorized, gin.H{"message": "Token无效"})
c.Abort()
return
}
expTimestamp := utils.StringToInt64(fmt.Sprintf("%v", tokenInfo["exp"]))
expTime := time.Unix(expTimestamp, 0)
ok := expTime.After(time.Now())
if !ok {
c.JSON(http.StatusUnauthorized, gin.H{"message": "Token过期"})
c.Abort()
return
}
cache, _ := cache2.Cache.HGet(config.RedisKeyForAccount, fmt.Sprintf("%v", tokenInfo[config.TokenForUID]))
if cache == "" {
c.JSON(http.StatusUnauthorized, gin.H{"message": "用户未登录或已退出"})
c.Abort()
return
}
session := new(service.Session)
_ = session.UnmarshalBinary([]byte(cache))
if !config.SettingInfo.MultipleLogin && session.Token != token {
c.JSON(http.StatusUnauthorized, gin.H{"message": "登录失效,已在其他地方登录!"})
if err != nil {
c.JSON(http.StatusUnauthorized, gin.H{"message": err.Error()})
c.Abort()
return
}