抢购后自动核对积分余额(基线在导入账号时获取),发布0.3.5
兑换成功会扣积分,所以"余额确实少了多少"是一个独立于接口返回的确认证据—— 2026-09-30 出现过订单实际已支付、程序却报成失败的情况,只看接口返回不够。 基线的获取时机刻意放在抢购链路之外:轮次/预热期间任何额外请求都会挤占锁单, 账号越多越明显。基线只在三个时机更新: - 导入账号后自动查一次(后台执行,不阻塞导入返回) - 用户手动点"查询积分"时 - 每次抢购核对之后把最新余额存回,下一次抢购的基线就是上一次的终值,差值才准 账号被编辑(改名/换 Cookie)时清掉旧基线,删除账号时一并清理。 - 新增 ScoreBaselineStore(scores.json,与账号库同目录,原子写盘): 记录每个账号上次已知的积分余额与时间;账号库路径为空时退化为纯内存,不写工作目录 - 核对输出示例: "积分已扣减,兑换确认: 账号=a 抢购前=1000 抢购后=800 扣减=200" 并把"|积分-200"附到结果集,账号表里也能直接看到确认信息 - 没有基线时只记录余额并提示"下次抢购即可比对",不会误报"未扣分"; 积分接口失败只记警告,不改变"抢到"这个结论 - 抢购进行中调用导入不会去查询积分(避免与锁单抢连接),提示稍后手动查询 - 预热日志措辞修正:"开始预热: N 个请求" → "N 个账号"(每账号实际不止一个请求) - 新增测试:TestVerifyRunResultsConfirmsScoreDeduction、TestVerifyRunResultsWithoutBaseline、 TestVerifyRunResultsIgnoresScoreFailure、TestScoreBaselineStorePersists、 TestFetchScoreBaselinesRecordsValues
This commit is contained in:
@@ -43,6 +43,9 @@ func (a *App) ImportAccounts(content string) types.AccountMutationResult {
|
||||
return types.AccountMutationResult{Error: err.Error()}
|
||||
}
|
||||
logger.LogEvent("账号", "success", "已从剪贴板导入 %d 个账号", len(entries))
|
||||
// 导入完顺手记一次积分余额作为基线。刻意放在这里而不是预热/抢购链路里:
|
||||
// 账号多的时候,抢购期的任何额外请求都会挤占锁单。后台执行,不阻塞导入返回。
|
||||
a.fetchScoreBaselinesAsync(scoreAccountsFromLines(entries), "导入账号")
|
||||
return types.AccountMutationResult{Success: true, Count: len(entries)}
|
||||
}
|
||||
|
||||
@@ -105,6 +108,11 @@ func (a *App) UpdateAccount(originalName, newName, newCookie string) types.Accou
|
||||
return types.AccountMutationResult{Error: err.Error()}
|
||||
}
|
||||
logger.LogEvent("账号", "success", "账号已更新: %s", newName)
|
||||
// 改过名或换过 Cookie,旧基线都不再适用:改名先删旧键,再按新 Cookie 取一次。
|
||||
if newName != originalName {
|
||||
a.scheduler.ForgetScoreBaselines([]string{originalName})
|
||||
}
|
||||
a.fetchScoreBaselinesAsync([]types.Account{{Name: newName, Cookie: newCookie}}, "更新账号")
|
||||
return types.AccountMutationResult{Success: true, Count: 1}
|
||||
}
|
||||
|
||||
@@ -144,6 +152,11 @@ func (a *App) DeleteAccounts(accountNames []string) types.AccountMutationResult
|
||||
if err := a.saveAccountLines(accountFile, kept); err != nil {
|
||||
return types.AccountMutationResult{Error: err.Error()}
|
||||
}
|
||||
deletedNames := make([]string, 0, deleted)
|
||||
for name := range nameSet {
|
||||
deletedNames = append(deletedNames, name)
|
||||
}
|
||||
a.scheduler.ForgetScoreBaselines(deletedNames)
|
||||
logger.LogEvent("账号", "success", "已删除 %d 个账号", deleted)
|
||||
return types.AccountMutationResult{Success: true, Count: deleted}
|
||||
}
|
||||
@@ -199,3 +212,38 @@ func (a *App) saveAccountLines(accountFile string, lines []accountLine) error {
|
||||
a.scheduler.LoadAccounts()
|
||||
return nil
|
||||
}
|
||||
|
||||
// scoreAccountsFromLines 从账号行里取出"账号名 + Cookie",用于积分基线查询。
|
||||
func scoreAccountsFromLines(lines []accountLine) []types.Account {
|
||||
out := make([]types.Account, 0, len(lines))
|
||||
for _, line := range lines {
|
||||
if line.Comment || line.Name == "" {
|
||||
continue
|
||||
}
|
||||
parts := strings.SplitN(strings.TrimSpace(line.Raw), "----", 3)
|
||||
if len(parts) != 3 {
|
||||
continue
|
||||
}
|
||||
cookie := strings.TrimSpace(parts[2])
|
||||
if cookie == "" {
|
||||
continue
|
||||
}
|
||||
out = append(out, types.Account{Name: line.Name, Cookie: cookie})
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// fetchScoreBaselinesAsync 后台查询积分基线,不阻塞调用方。
|
||||
func (a *App) fetchScoreBaselinesAsync(accounts []types.Account, label string) {
|
||||
if len(accounts) == 0 {
|
||||
return
|
||||
}
|
||||
go func() {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
logger.LogEvent("账号", "warn", "%s后查询积分基线异常: %v", label, r)
|
||||
}
|
||||
}()
|
||||
a.scheduler.FetchScoreBaselines(accounts)
|
||||
}()
|
||||
}
|
||||
|
||||
@@ -34,6 +34,8 @@ func (a *App) QueryScore(accountName string) types.ScoreResult {
|
||||
if err != nil {
|
||||
return types.ScoreResult{Score: "查询失败: " + err.Error()}
|
||||
}
|
||||
// 手动查到的余额同样作为最新基线:抢购结束后用它比对是否扣分。
|
||||
a.scheduler.RecordScoreBaseline(accountName, score)
|
||||
return types.ScoreResult{Score: strconv.Itoa(score)}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user