增加和平小店关键词固定抢购

This commit is contained in:
yml2213
2026-08-08 18:40:53 +08:00
parent 51e91b0483
commit a4fed8befb
5 changed files with 214 additions and 3 deletions
+21
View File
@@ -21,6 +21,27 @@ func (a *App) StartSeckill(accountProducts map[string]string) types.MessageResul
return types.MessageResult{Success: true, Msg: "抢购已启动"}
}
// StartFixedSeckill 启动和平小店固定次数/间隔抢购。
func (a *App) StartFixedSeckill(req types.FixedSeckillRequest) types.MessageResult {
if !a.getClient().Activity.IsXiaodian {
return types.MessageResult{Error: "固定次数抢购仅支持和平小店"}
}
if len(req.AccountProducts) == 0 {
return types.MessageResult{Error: "请选择账号和商品"}
}
if req.Attempts < 1 || req.Attempts > 100 {
return types.MessageResult{Error: "抢购次数需在 1-100 之间"}
}
if req.DelayMs < 0 || req.DelayMs > 60000 {
return types.MessageResult{Error: "抢购间隔需在 0-60000 毫秒之间"}
}
if !a.scheduler.TryStartRun() {
return types.MessageResult{Error: "抢购任务正在运行,请勿重复提交"}
}
go a.scheduler.RunFixed(utils.CloneStringMap(req.AccountProducts), req.Attempts, req.DelayMs)
return types.MessageResult{Success: true, Msg: "固定次数抢购已启动"}
}
// SetSchedule 设置定时抢购。
func (a *App) SetSchedule(req types.SeckillRequest) (types.ScheduleResult, error) {
return a.scheduler.ScheduleRun(req)
+69
View File
@@ -432,6 +432,75 @@ func (s *Scheduler) RunNow(accountProducts map[string]string) {
len(successAccounts), totalAccounts, len(stoppedAccounts)-len(successAccounts), totalAttempts, utils.RoundDuration(time.Since(startTime)))
}
// RunFixed 按固定轮数和轮间隔抢购,账号成功后不再参与后续轮次。
func (s *Scheduler) RunFixed(accountProducts map[string]string, attempts, delayMs int) {
defer s.finishRun()
s.ClearResults()
settings := s.settingsSnapshot()
if attempts < 1 {
attempts = 1
}
if attempts > 100 {
attempts = 100
}
if delayMs < 0 {
delayMs = 0
}
if delayMs > 60000 {
delayMs = 60000
}
accounts := make([]types.Account, 0, len(accountProducts))
for name, commodityID := range accountProducts {
cookie, ok := s.AccountCookieByName(name)
if !ok {
continue
}
product := s.productForCommodityID(commodityID)
if product.CommodityID == "" {
product.CommodityID = commodityID
}
accounts = append(accounts, types.Account{Name: name, Cookie: cookie})
}
if len(accounts) == 0 {
logger.LogEvent("抢购", "warn", "固定次数抢购没有可执行账号")
return
}
stopped := make(map[string]bool)
start := time.Now()
logger.LogEvent("抢购", "info", "固定次数抢购开始: %d 个账号,次数=%d,间隔=%dms", len(accounts), attempts, delayMs)
for round := 1; round <= attempts; round++ {
roundAttempts := make([]types.ExchangeAttempt, 0, len(accounts))
for _, account := range accounts {
if stopped[account.Name] {
continue
}
commodityID := accountProducts[account.Name]
product := s.productForCommodityID(commodityID)
if product.CommodityID == "" {
product.CommodityID = commodityID
}
roundAttempts = append(roundAttempts, types.ExchangeAttempt{Account: account, Product: product, Index: round})
}
if len(roundAttempts) == 0 {
break
}
if round > 1 && delayMs > 0 {
time.Sleep(time.Duration(delayMs) * time.Millisecond)
}
outcome := s.executeRound(roundAttempts, settings)
for name := range outcome.successAccounts {
stopped[name] = true
}
for name := range outcome.stoppedAccounts {
stopped[name] = true
}
logger.LogEvent("抢购", "info", "固定抢购第%d/%d轮完成: 成功=%d,剩余账号=%d", round, attempts, len(outcome.successAccounts), len(accounts)-len(stopped))
}
logger.LogEvent("抢购", "info", "固定次数抢购结束: 成功=%d/%d,耗时=%v", len(stopped), len(accounts), utils.RoundDuration(time.Since(start)))
}
// filterPendingAttempts 过滤掉已终止账号和已确认无库存的商品。
func (s *Scheduler) filterPendingAttempts(allAttempts []types.ExchangeAttempt, stoppedAccounts, stoppedProducts map[string]string) []types.ExchangeAttempt {
var pending []types.ExchangeAttempt
+7
View File
@@ -106,6 +106,13 @@ type SeckillRequest struct {
TargetTime string `json:"targetTime"`
}
// FixedSeckillRequest 是和平小店固定次数/间隔抢购请求。
type FixedSeckillRequest struct {
AccountProducts map[string]string `json:"accountProducts"`
Attempts int `json:"attempts"`
DelayMs int `json:"delayMs"`
}
// APIResponse 斗鱼 API 通用响应。
type APIResponse struct {
Error int `json:"error"`