app.go (768行) 拆分为 6 个文件: - app.go: App struct/辅助方法/NewApp/startup/shutdown/GetVersion - app_products.go: 商品加载/账号查询 - app_seckill.go: 抢购控制/日志/时间 - app_verify.go: 积分查询/兑换记录复验 - app_export.go: 账号导出/整理 - app_activity.go: 活动切换/配置保存 scheduler.go (1141行) 拆分为 4 个文件: - scheduler.go: struct/常量/设置/RunNow主循环/executeRound/辅助函数 - attempt.go: runAttempt/runAttemptFast/激进模式缓存 - warmup.go: 预热管理/warmupAttempts - schedule_timer.go: 定时抢购/时间校准/定时器管理 其他清理: - CollectFontCandidates 从 config 包移到 image 包(职责归属) - 删除 logger 死代码: ParseLogLine/ParseLogMeta/DetectLogLevel/ IsLogLevel/LogMeta/LogDebug/cutBracket 等反解析函数及对应测试 (日志结构化后不再有生产调用方) - 删除 static/ 空目录残留
75 lines
2.1 KiB
Go
75 lines
2.1 KiB
Go
package main
|
|
|
|
import (
|
|
"time"
|
|
|
|
"douyu_go/internal/constants"
|
|
"douyu_go/internal/logger"
|
|
"douyu_go/internal/types"
|
|
"douyu_go/internal/utils"
|
|
)
|
|
|
|
// StartSeckill 立即启动抢购。
|
|
func (a *App) StartSeckill(accountProducts map[string]string) types.MessageResult {
|
|
if len(accountProducts) == 0 {
|
|
return types.MessageResult{Error: "请选择账号和商品"}
|
|
}
|
|
if !a.scheduler.TryStartRun() {
|
|
return types.MessageResult{Error: "抢购任务正在运行,请勿重复提交"}
|
|
}
|
|
go a.scheduler.RunNow(utils.CloneStringMap(accountProducts))
|
|
return types.MessageResult{Success: true, Msg: "抢购已启动"}
|
|
}
|
|
|
|
// SetSchedule 设置定时抢购。
|
|
func (a *App) SetSchedule(req types.SeckillRequest) (types.ScheduleResult, error) {
|
|
return a.scheduler.ScheduleRun(req)
|
|
}
|
|
|
|
// CancelSchedule 取消未触发的定时抢购。
|
|
func (a *App) CancelSchedule() types.CancelScheduleResult {
|
|
stopped := a.scheduler.CancelSchedule()
|
|
return types.CancelScheduleResult{Success: true, Stopped: stopped}
|
|
}
|
|
|
|
// GetResults 返回当前抢购结果。
|
|
func (a *App) GetResults() map[string]string {
|
|
return a.scheduler.Results()
|
|
}
|
|
|
|
// GetLogs 返回后端内存日志,支持增量获取。
|
|
func (a *App) GetLogs(since int64) types.LogsResult {
|
|
entries := a.logStore.Since(since)
|
|
nextSince := since
|
|
if len(entries) > 0 {
|
|
nextSince = entries[len(entries)-1].ID
|
|
}
|
|
return types.LogsResult{
|
|
Success: true,
|
|
Logs: entries,
|
|
Since: nextSince,
|
|
File: a.logStore.FilePath(),
|
|
}
|
|
}
|
|
|
|
// GetTime 返回服务端时间和本地时间。
|
|
func (a *App) GetTime() types.TimeResult {
|
|
serverTime, err := a.getClient().ServerTime()
|
|
source := "currentTime"
|
|
if err != nil {
|
|
source = "localFallback"
|
|
logger.LogEvent("时间", "warn", "时间接口降级: %v", err)
|
|
}
|
|
return types.TimeResult{
|
|
ServerTime: serverTime.In(constants.CST).Format("2006-01-02 15:04:05.000"),
|
|
LocalTime: time.Now().In(constants.CST).Format("2006-01-02 15:04:05.000"),
|
|
Source: source,
|
|
}
|
|
}
|
|
|
|
// ClearResults 清空抢购结果。
|
|
func (a *App) ClearResults() types.SimpleResult {
|
|
a.scheduler.ClearResults()
|
|
return types.SimpleResult{Success: true}
|
|
}
|