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

47
app/service/auth.go Normal file
View File

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

View File

@ -40,7 +40,12 @@ func (this *Hub) Run() {
}
case message := <-this.broadcast:
for _, client := range this.clients {
client.send <- message.ToBytes()
select {
case client.send <- message.ToBytes():
default:
close(client.send)
delete(this.clients, client.ID)
}
}
case iMsg := <-this.emit:
client, has := this.clients[iMsg.ID]