feat:优化项目结构

This commit is contained in:
henry
2021-11-24 10:50:09 +08:00
parent f007168919
commit 0862142ef0
25 changed files with 306 additions and 203 deletions

View File

@ -1,9 +1,11 @@
package service
import (
"SciencesServer/app/logic"
"SciencesServer/config"
cache2 "SciencesServer/serve/cache"
"SciencesServer/utils"
"encoding/json"
"errors"
"fmt"
"time"
@ -13,31 +15,32 @@ type AuthToken struct {
Token string `json:"token"`
}
func (this *AuthToken) Auth() (*Session, error) {
func (this *AuthToken) Auth(session logic.ISession) error {
tokenInfo := utils.JWTDecrypt(this.Token)
if tokenInfo == nil || len(tokenInfo) <= 0 {
return nil, errors.New("Token无效")
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 nil, errors.New("Token过期")
return errors.New("登陆错误,Token过期")
}
cache, _ := cache2.Cache.HGet(config.RedisKeyForAccount, fmt.Sprintf("%v", tokenInfo[config.TokenForUID]))
if cache == "" {
return nil, errors.New("用户未登录或已退出")
return errors.New("登陆错误,用户未登录或已退出")
}
session := new(Session)
_ = session.UnmarshalBinary([]byte(cache))
if !config.SettingInfo.MultipleLogin && session.Token != this.Token {
return nil, errors.New("登录失效,已在其他地方登录!")
if err := json.Unmarshal([]byte(cache), session); err != nil {
return err
}
return session, nil
if !config.SettingInfo.MultipleLogin && session.GetToken() != this.Token {
return errors.New("登陆错误,已在其他地方登录!")
}
return nil
}
func NewAuthToken(token string) *AuthToken {

View File

@ -6,13 +6,12 @@ import (
)
type Session struct {
UID uint64 `json:"uid"` // 唯一标识ID
Token string `json:"token"` // token
Name string `json:"name"` // 名称
Mobile string `json:"mobile"` // 手机号码
IsAdmin bool `json:"is_admin"` // 是否超管
TenantID uint64 `json:"tenant_id"` // 租户ID
TenantKey string `json:"tenant_key"` // 租户标识,用来区别分库管理
Token string `json:"token"` // token
UID uint64 `json:"uid"` // 唯一标识ID
TenantID uint64 `json:"tenant_id"` // 租户ID
Name string `json:"name"` // 名称
Mobile string `json:"mobile"` // 手机号码
IsAdmin bool `json:"is_admin"` // 是否超管
}
func (this *Session) MarshalBinary() ([]byte, error) {