Files
bl/modules/blazing/service/login.go
昔念 2f3ca21165 feat(login): 重构 login 服务启动方式
- 修改 login 服务端口配置,支持动态分配
- 优化 login 服务启动流程,增加信号处理
- 调整 ServerOption 类型,支持 int 类型端口
- 移除 CommendSvrInfo 相关代码,简化结构
- 更新 main 函数,采用新的服务启动方式
2025-07-06 01:49:19 +08:00

74 lines
1.6 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/blazing/model"
"encoding/binary"
"encoding/hex"
"math/rand"
"strings"
"time"
"github.com/google/uuid"
)
type LoginService struct {
*cool.Service
}
func NewLoginServiceService() *LoginService {
return &LoginService{
&cool.Service{
Model: model.NewServerList(),
},
}
}
func (s *LoginService) GetSessionId(accountID uint) (string, string, error) {
t1, _ := uuid.NewV7()
tt := strings.Replace(t1.String(), "-", "", -1)
rand.NewSource(time.Now().UnixNano())
// 生成4个随机字节
bytes := make([]byte, 4)
for i := range bytes {
bytes[i] = byte(rand.Intn(256)) // 生成0-255之间的随机数
}
buf2 := make([]byte, 4) // 创建4字节的缓冲区
binary.BigEndian.PutUint32(buf2, uint32(accountID))
//fmt.Printf("小端序: %v (十六进制: %x)\n", buf2, buf2) // 输出: [252 255 255 255] (0xFCFFFFFF)
ttid, _ := hex.DecodeString(tt)
//fmt.Println(bytes, "随机字节")
ret := append(buf2, ttid...)
ret = append(ret, bytes...)
return hex.EncodeToString(ret), tt, nil
// /t1.
// 以上过程只需全局一次且应在生成ID之前完成。
}
func (s *LoginService) SetServerID(OnlineID uint32, 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
}
_, err = m.Data(&model.ServerList{OnlineID: OnlineID, Port: Port}).Where("online_id", OnlineID).Update()
return nil
}