feat:完善项目信息

This commit is contained in:
henry
2022-01-11 10:41:46 +08:00
parent 6712feec35
commit 7cab0fb354
24 changed files with 188 additions and 169 deletions

50
app/service/auth_token.go Normal file
View 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,
}
}