feat: 优化CDK服务器冠名逻辑与鉴权
Some checks failed
ci/woodpecker/push/my-first-workflow Pipeline failed

This commit is contained in:
xinian
2026-04-08 19:31:44 +08:00
parent 28b6386963
commit 3b35789b47
5 changed files with 248 additions and 184 deletions

View File

@@ -160,7 +160,16 @@ func (s *CdkService) Set(id string) bool {
return true
}
func (s *CdkService) UseServerNamingCDK(ctx context.Context, code string, ownerID, serverID uint32, serverName string) (*model.ServerList, error) {
type ServerNamingCDKResult struct {
ServerID uint32
ServerName string
OwnerID uint32
ServerExpireTime time.Time
CDKExpireTime time.Time
}
// UseServerNamingCDK 使用服务器冠名类型CDK并原子化更新服务器归属和到期时间。
func (s *CdkService) UseServerNamingCDK(ctx context.Context, code string, ownerID, serverID uint32, serverName string) (*ServerNamingCDKResult, error) {
if ctx == nil {
ctx = context.TODO()
}
@@ -190,7 +199,10 @@ func (s *CdkService) UseServerNamingCDK(ctx context.Context, code string, ownerI
if err := tx.Model(model.NewServerList()).Where("online_id", serverID).Scan(&server); err != nil {
return err
}
if !serverService.CanUseDonationName(server, now) {
if server.OnlineID == 0 {
return gerror.New("服务器不存在")
}
if server.Owner != ownerID && !serverService.CanUseDonationName(server, now) {
return gerror.New("服务器不可冠名")
}
@@ -203,19 +215,23 @@ func (s *CdkService) UseServerNamingCDK(ctx context.Context, code string, ownerI
return gerror.New("cdk已被使用")
}
baseTime := now
if server.ExpireTime.After(now) {
baseTime = server.ExpireTime
}
updated = server
updated.Name = serverName
updated.Owner = ownerID
updated.ExpireTime = baseTime.AddDate(0, 1, 0)
if server.Owner != ownerID {
updated.CDKExpireTime = now.AddDate(0, 1, 0)
} else {
baseTime := server.CDKExpireTime
if baseTime.IsZero() {
baseTime = now
}
updated.CDKExpireTime = baseTime.AddDate(0, 1, 0)
}
_, err = tx.Model(model.NewServerList()).Where("online_id", serverID).Data(g.Map{
"name": updated.Name,
"owner": updated.Owner,
"expire_time": updated.ExpireTime,
"name": updated.Name,
"owner": updated.Owner,
"cdk_expire_time": updated.CDKExpireTime,
}).Update()
return err
})
@@ -225,7 +241,13 @@ func (s *CdkService) UseServerNamingCDK(ctx context.Context, code string, ownerI
g.DB(s.Model.GroupName()).GetCore().ClearCache(context.TODO(), s.Model.TableName())
g.DB(model.NewServerList().GroupName()).GetCore().ClearCache(context.TODO(), model.NewServerList().TableName())
return &updated, nil
return &ServerNamingCDKResult{
ServerID: updated.OnlineID,
ServerName: updated.Name,
OwnerID: updated.Owner,
ServerExpireTime: updated.ExpireTime,
CDKExpireTime: updated.CDKExpireTime,
}, nil
}
func (s *CdkService) BatchGenerate(ctx context.Context, count int) (interface{}, error) {