Files
bl/modules/blazing/service/login.go
昔念 dd28d48ca4 ```
feat(build): 更新构建脚本添加资源打包和proto编译

更新build.bat脚本,添加proto文件编译和资源打包功能,调整资源打包顺序。

BREAKING CHANGE: 构建流程发生变化,需要重新生成proto文件和打包资源。

---

refactor(xmlres): 使用gres替换gfile读取资源文件

将xmlres模块中文件读取方式从gfile.GetBytes改为gres.GetContent,
使
2026-01-01 19:57:39 +08:00

96 lines
2.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package service
import (
"blazing/cool"
"blazing/modules/config/model"
"encoding/binary"
"encoding/hex"
"strings"
"github.com/gogf/gf/v2/util/grand"
"github.com/google/uuid"
)
type LoginService struct {
*cool.Service
}
func NewLoginServiceService() *LoginService {
return &LoginService{
&cool.Service{
Model: model.NewServerList(),
},
}
}
// 生成session
// GetSessionId 生成并返回会话ID、UUID字符串及可能的错误
// 会话ID由accountID(4字节) + UUID(16字节) + 随机数(4字节)组成,最终编码为十六进制字符串
func (s *LoginService) GetSessionId(accountID uint) (string, string, error) {
// 生成UUID v7带错误处理UUID生成失败时返回错误
uuidV7, err := uuid.NewV7()
if err != nil {
return "", "", err
}
// 移除UUID中的连字符便于后续处理
uuidStr := strings.ReplaceAll(uuidV7.String(), "-", "")
// 解码UUID字符串为字节数组32位十六进制字符串对应16字节
uuidBytes, err := hex.DecodeString(uuidStr)
if err != nil {
// 理论上UUID生成的字符串不会出现解码错误此处为防御性处理
return "", "", err
}
// 将accountID转换为4字节大端序字节数组
accountBytes := make([]byte, 4)
binary.BigEndian.PutUint32(accountBytes, uint32(accountID))
// 预分配缓冲区总长度4+16+4=24字节减少内存分配
sessionBytes := make([]byte, 0, 24)
sessionBytes = append(sessionBytes, accountBytes...)
sessionBytes = append(sessionBytes, uuidBytes...)
sessionBytes = append(sessionBytes, grand.B(4)...)
// 编码为十六进制字符串作为最终会话ID
sessionID := hex.EncodeToString(sessionBytes)
return sessionID, uuidStr, nil
}
func (s *LoginService) SetServerID(OnlineID uint16, Port uint16) error {
m := cool.DBM(s.Model).Where("online_id", OnlineID)
// record, err := m.One()
// if err != nil {
// return err
// }
// if record == nil {
// //说明是新的服务器
// _, err := m.InsertAndGetId(&model.ServerList{OnlineID: OnlineID, Port: Port})
// return err
// }
var tttt model.ServerList
m.Scan(&tttt)
tttt.Port = Port
tttt.OnlineID = OnlineID
m.Save(tttt)
return nil
}
func (s *LoginService) GetServerID(OnlineID uint16) *model.ServerList {
var tttt *model.ServerList
err := cool.DBM(s.Model).Where("online_id", OnlineID).Scan(&tttt)
if err != nil {
return nil
}
return tttt
}