feat:0666账号密码自动登录刷新Token

This commit is contained in:
2026-11-03 12:13:39 +08:00
parent 4dbec33dbd
commit f69cdc6d8e
12 changed files with 741 additions and 11 deletions
+30 -2
View File
@@ -17,17 +17,23 @@ import (
type RockService struct {
db *gorm.DB
client *RockClient
codec *SecretCodec
mu sync.Mutex
tokenMu sync.Mutex
refreshCache sync.Map // key: upstreamTokenID string, value: rockRefreshCacheItem
bindLinkCache sync.Map // key: token ID uint, value: rockBindLinkCacheItem
lastSummary *PurchaseSummary
}
func NewRockService(db *gorm.DB, client *RockClient) *RockService {
return &RockService{
func NewRockService(db *gorm.DB, client *RockClient, codecs ...*SecretCodec) *RockService {
svc := &RockService{
db: db,
client: client,
}
if len(codecs) > 0 {
svc.codec = codecs[0]
}
return svc
}
// RockStatsResponse 看板与库存统计返回
@@ -80,6 +86,8 @@ type UpdateRockConfigReq struct {
AutoPurchaseEnabled *bool `json:"auto_purchase_enabled"`
AutoPurchaseCount *int `json:"auto_purchase_count"`
PurchaseCooldownMinutes *int `json:"purchase_cooldown_minutes"`
LoginUsername *string `json:"login_username"`
LoginPassword *string `json:"login_password"`
}
// GetConfig 获取当前配置,若无则初始化
@@ -104,11 +112,13 @@ func (s *RockService) GetConfig(ctx context.Context) (*model.RockConfig, error)
if createErr := s.db.WithContext(ctx).Create(&cfg).Error; createErr != nil {
return nil, createErr
}
cfg.LoginPasswordSet = strings.TrimSpace(cfg.LoginPasswordCiphertext) != ""
return &cfg, nil
}
if err != nil {
return nil, err
}
cfg.LoginPasswordSet = strings.TrimSpace(cfg.LoginPasswordCiphertext) != ""
return &cfg, nil
}
@@ -159,6 +169,24 @@ func (s *RockService) UpdateConfig(ctx context.Context, req UpdateRockConfigReq)
if req.PurchaseCooldownMinutes != nil {
updates["purchase_cooldown_minutes"] = *req.PurchaseCooldownMinutes
}
if req.LoginUsername != nil {
updates["login_username"] = strings.TrimSpace(*req.LoginUsername)
}
if req.LoginPassword != nil {
password := *req.LoginPassword
if password == "" {
updates["login_password_ciphertext"] = ""
} else {
if s.codec == nil {
return nil, &RockConfigValidationError{Message: "未配置数据加密密钥,无法保存登录密码"}
}
ciphertext, encryptErr := s.codec.Encrypt(password)
if encryptErr != nil {
return nil, &RockConfigValidationError{Message: "加密登录密码失败: " + encryptErr.Error()}
}
updates["login_password_ciphertext"] = ciphertext
}
}
// 使用中超时预警阈值:橙色必须小于红色,否则前端无法区分两级。
warnDays := cfg.UsingWarnDays