feat:完善项目信息
This commit is contained in:
50
app/service/auth_token.go
Normal file
50
app/service/auth_token.go
Normal file
@ -0,0 +1,50 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"SciencesServer/app/logic"
|
||||
"SciencesServer/config"
|
||||
cache2 "SciencesServer/serve/cache"
|
||||
"SciencesServer/utils"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
type AuthToken struct {
|
||||
Token string `json:"token"`
|
||||
}
|
||||
|
||||
func (this *AuthToken) Auth(key string, session logic.ISession) error {
|
||||
tokenInfo := utils.JWTDecrypt(this.Token)
|
||||
|
||||
if tokenInfo == nil || len(tokenInfo) <= 0 {
|
||||
return errors.New("登陆错误,Token无效")
|
||||
}
|
||||
expTimestamp := utils.StringToInt64(fmt.Sprintf("%v", tokenInfo["exp"]))
|
||||
expTime := time.Unix(expTimestamp, 0)
|
||||
ok := expTime.After(time.Now())
|
||||
|
||||
if !ok {
|
||||
return errors.New("登陆错误,Token过期")
|
||||
}
|
||||
cache, _ := cache2.Cache.HGet(key, fmt.Sprintf("%v", tokenInfo[config.TokenForUID]))
|
||||
|
||||
if cache == "" {
|
||||
return errors.New("登陆错误,用户未登录或已退出")
|
||||
}
|
||||
|
||||
if err := json.Unmarshal([]byte(cache), session); err != nil {
|
||||
return err
|
||||
}
|
||||
if !config.SettingInfo.MultipleLogin && session.GetToken() != this.Token {
|
||||
return errors.New("登陆错误,已在其他地方登录!")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func NewAuthToken(token string) *AuthToken {
|
||||
return &AuthToken{
|
||||
Token: token,
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user