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/ 空目录残留
83 lines
2.2 KiB
Go
83 lines
2.2 KiB
Go
package main
|
|
|
|
import (
|
|
"time"
|
|
|
|
"douyu_go/internal/constants"
|
|
"douyu_go/internal/logger"
|
|
"douyu_go/internal/types"
|
|
"douyu_go/internal/utils"
|
|
)
|
|
|
|
// LoadProducts 刷新商品列表。refresh=true 时从斗鱼实时拉取。
|
|
func (a *App) LoadProducts(refresh bool) types.ProductPayload {
|
|
if refresh {
|
|
products, err := a.getClient().Products()
|
|
if err != nil {
|
|
logger.LogEvent("商品", "error", "商品刷新失败: %v", err)
|
|
return types.ProductPayload{Success: false}
|
|
}
|
|
fetchedAt := time.Now()
|
|
a.setProducts(products, fetchedAt)
|
|
logger.LogEvent("商品", "success", "商品刷新完成: %d 个,刷新时间=%s", len(products), fetchedAt.In(constants.CST).Format("15:04:05.000"))
|
|
return a.productPayload("live")
|
|
}
|
|
return a.productPayload("cache")
|
|
}
|
|
|
|
// GetAccounts 返回账号列表。
|
|
func (a *App) GetAccounts() []types.AccountView {
|
|
accounts := a.scheduler.AccountSnapshot()
|
|
list := make([]types.AccountView, 0, len(accounts))
|
|
for i, account := range accounts {
|
|
list = append(list, types.AccountView{
|
|
Index: i,
|
|
Name: account.Name,
|
|
Cookie: utils.MaskCookie(account.Cookie),
|
|
})
|
|
}
|
|
return list
|
|
}
|
|
|
|
// ReloadAccounts 重新加载账号文件。
|
|
func (a *App) ReloadAccounts() types.ReloadAccountsResult {
|
|
a.scheduler.LoadAccounts()
|
|
return types.ReloadAccountsResult{Success: true, Count: a.scheduler.AccountCount()}
|
|
}
|
|
|
|
// ==================== 内部辅助方法 ====================
|
|
|
|
func (a *App) setProducts(products []types.Product, fetchedAt time.Time) {
|
|
a.productsMu.Lock()
|
|
a.products = products
|
|
a.productsAt = fetchedAt
|
|
a.productsMu.Unlock()
|
|
if a.scheduler != nil {
|
|
a.scheduler.UpdateProducts(products)
|
|
}
|
|
}
|
|
|
|
func (a *App) productPayload(source string) types.ProductPayload {
|
|
a.productsMu.RLock()
|
|
defer a.productsMu.RUnlock()
|
|
|
|
payload := types.ProductPayload{
|
|
Success: true,
|
|
Source: source,
|
|
Products: a.products,
|
|
Count: len(a.products),
|
|
}
|
|
if !a.productsAt.IsZero() {
|
|
age := time.Since(a.productsAt)
|
|
if age < 0 {
|
|
age = 0
|
|
}
|
|
payload.FetchedAt = a.productsAt.In(constants.CST).Format("2006-01-02 15:04:05.000")
|
|
payload.AgeSecond = int(age.Seconds())
|
|
payload.Stale = age > constants.ProductCacheTTL
|
|
} else {
|
|
payload.Stale = true
|
|
}
|
|
return payload
|
|
}
|