refactor(login): 重构登录服务中的会话管理
- 移除 LoginSidInfo 中的 CacheManager 调用,改为使用 session 包 - 更新 BlazingController 中的 SaveSession 调用,使用 session 包 - 删除 LoginService 中的 SaveSessionId 方法,简化会话管理逻辑 - 优化代码结构,提高可维护性和可测试性
This commit is contained in:
44
common/data/session/session.go
Normal file
44
common/data/session/session.go
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
package session
|
||||||
|
|
||||||
|
import (
|
||||||
|
"blazing/cool"
|
||||||
|
"context"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/gogf/gf/v2/os/gctx"
|
||||||
|
)
|
||||||
|
|
||||||
|
var session = "session:"
|
||||||
|
|
||||||
|
// GetSession 通过给定的键从缓存中获取会话数据。
|
||||||
|
// 该函数主要执行以下操作:
|
||||||
|
// 1. 使用 strings.Trim 移除输入字符串 t1 两端的空格,得到 t2。
|
||||||
|
// 2. 调用 cool.CacheManager.Get 方法,在上下文为 context.Background() 的情况下,使用 t2 作为键从缓存中获取数据。
|
||||||
|
// 参数:
|
||||||
|
//
|
||||||
|
// t1 - 用于获取会话数据的键字符串。
|
||||||
|
func GetSession(t1 string) (userid uint32, err error) {
|
||||||
|
// 移除输入键两端的空格,以确保键的格式一致性。
|
||||||
|
t2 := strings.Trim(t1, " ")
|
||||||
|
|
||||||
|
// 从缓存中获取与键 t2 关联的数据。这里假设 cool.CacheManager 已经初始化,并且 Get 方法可用。
|
||||||
|
// 此处不处理 err,可能是因为上层调用者期望处理这个错误,或者在特定上下文中,错误被视为可接受。
|
||||||
|
t, err := cool.CacheManager.Get(context.Background(), session+t2)
|
||||||
|
|
||||||
|
userid = t.Uint32()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
func SaveSession(session string, userid string) error {
|
||||||
|
|
||||||
|
err := cool.CacheManager.Set(gctx.New(), session+strings.Trim(session, " "), userid, time.Hour*24)
|
||||||
|
// gsvc.SetRegistry(etcd.New(`127.0.0.1:2379`))
|
||||||
|
//t, err := cool.CacheManager.Contains(context.Background(), strings.Trim(session, " "))
|
||||||
|
|
||||||
|
// //fmt.Println("前端获取", session, t, err)
|
||||||
|
// if t {
|
||||||
|
// return nil
|
||||||
|
// }
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
@@ -1,12 +1,10 @@
|
|||||||
package login
|
package login
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"blazing/common/data/session"
|
||||||
"blazing/common/socket/handler"
|
"blazing/common/socket/handler"
|
||||||
"blazing/cool"
|
|
||||||
"context"
|
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// LoginSidInfo 登录携带的凭证结构体
|
// LoginSidInfo 登录携带的凭证结构体
|
||||||
@@ -23,10 +21,9 @@ func (l *LoginSidInfo) CheakSession() bool {
|
|||||||
//g.Dump(tt)
|
//g.Dump(tt)
|
||||||
t1 := hex.EncodeToString(l.Sid)
|
t1 := hex.EncodeToString(l.Sid)
|
||||||
|
|
||||||
t2 := strings.Trim(t1, " ")
|
t, err := session.GetSession(t1)
|
||||||
t, err := cool.CacheManager.Get(context.Background(), t2)
|
|
||||||
fmt.Println("后端获取", string(l.Sid), t, err)
|
fmt.Println("后端获取", string(l.Sid), t, err)
|
||||||
if t.Uint32() == l.Head.UserID {
|
if t == l.Head.UserID {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package admin
|
package admin
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"blazing/common/data/session"
|
||||||
"blazing/cool"
|
"blazing/cool"
|
||||||
baseservice "blazing/modules/base/service"
|
baseservice "blazing/modules/base/service"
|
||||||
"blazing/modules/blazing/service"
|
"blazing/modules/blazing/service"
|
||||||
@@ -72,7 +73,7 @@ func (c *BlazingController) GetSession(ctx context.Context, req *SessionReq) (re
|
|||||||
|
|
||||||
res.Session = retsid
|
res.Session = retsid
|
||||||
|
|
||||||
if err := biazing_service.SaveSessionId(sid, gconv.String(accountID)); err != nil {
|
if err := session.SaveSession(sid, gconv.String(accountID)); err != nil {
|
||||||
res.Code = 400
|
res.Code = 400
|
||||||
res.Msg = err.Error()
|
res.Msg = err.Error()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,15 +3,12 @@ package service
|
|||||||
import (
|
import (
|
||||||
"blazing/cool"
|
"blazing/cool"
|
||||||
"blazing/modules/blazing/model"
|
"blazing/modules/blazing/model"
|
||||||
"context"
|
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"fmt"
|
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gogf/gf/v2/os/gctx"
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -27,16 +24,6 @@ func NewLoginServiceService() *LoginService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *LoginService) SaveSessionId(session string, userid string) error {
|
|
||||||
|
|
||||||
cool.CacheManager.Set(gctx.New(), strings.Trim(session, " "), userid, time.Hour*24)
|
|
||||||
// gsvc.SetRegistry(etcd.New(`127.0.0.1:2379`))
|
|
||||||
t, err := cool.CacheManager.Contains(context.Background(), strings.Trim(session, " "))
|
|
||||||
|
|
||||||
fmt.Println("前端获取", session, t, err)
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
func (s *LoginService) GetSessionId(accountID uint) (string, string, error) {
|
func (s *LoginService) GetSessionId(accountID uint) (string, string, error) {
|
||||||
|
|
||||||
t1, _ := uuid.NewV7()
|
t1, _ := uuid.NewV7()
|
||||||
|
|||||||
Reference in New Issue
Block a user