重构: 应用代码移入 internal/app 包,根目录只留入口
根目录原 7 个 app*.go 平铺,现收进 internal/app/ 子包: - package main → package app - NewApp → New(导出) - startup/shutdown → Startup/Shutdown(导出,供 main.go 引用) - main.go 改为薄入口,仅导入 internal/app 并装配 Wails 前端同步更新(Wails 绑定路径 go/main → go/app): - window.go.main.App → window.go.app.App(10 处) - env.d.ts 类型声明路径 wailsjs/go/main/App → wailsjs/go/app/App 根目录 Go 文件从 7 个减至 1 个(main.go),元文件更清晰
This commit is contained in:
@@ -0,0 +1,90 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"douyu_go/internal/activity"
|
||||
"douyu_go/internal/client"
|
||||
"douyu_go/internal/config"
|
||||
"douyu_go/internal/constants"
|
||||
"douyu_go/internal/logger"
|
||||
"douyu_go/internal/types"
|
||||
)
|
||||
|
||||
// GetActivities 返回所有已注册的手册活动,供前端下拉选择。
|
||||
func (a *App) GetActivities() []types.ActivityInfo {
|
||||
list := activity.List()
|
||||
out := make([]types.ActivityInfo, 0, len(list))
|
||||
for _, act := range list {
|
||||
out = append(out, types.ActivityInfo{ID: act.ID, DisplayName: act.DisplayName})
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// GetCurrentActivity 返回当前生效的活动。
|
||||
func (a *App) GetCurrentActivity() types.ActivityInfo {
|
||||
cli := a.getClient()
|
||||
act := cli.Activity
|
||||
return types.ActivityInfo{ID: act.ID, DisplayName: act.DisplayName}
|
||||
}
|
||||
|
||||
// SwitchActivity 切换当前活动并持久化到配置;切换后重建客户端与时间基准。
|
||||
func (a *App) SwitchActivity(id string) types.SwitchActivityResult {
|
||||
act := activity.Get(id)
|
||||
if act == nil {
|
||||
return types.SwitchActivityResult{Error: "未知活动: " + id}
|
||||
}
|
||||
cfg := a.getCfg()
|
||||
cfg.Activity = act.ID
|
||||
// 仅当用户未自定义时间校准接口时才回退默认值,避免切换活动静默覆盖用户在设置里填的 timeApiUrl。
|
||||
if cfg.TimeAPIURL == "" {
|
||||
cfg.TimeAPIURL = constants.DefaultTimeAPI
|
||||
}
|
||||
if err := config.SaveConfig(a.paths.ConfigFile, cfg); err != nil {
|
||||
logger.LogEvent("活动", "error", "切换活动配置保存失败: %v", err)
|
||||
return types.SwitchActivityResult{Error: "配置保存失败: " + err.Error()}
|
||||
}
|
||||
a.setCfg(cfg)
|
||||
newClient := client.New(act, cfg.TimeAPIURL, a.trace)
|
||||
a.setClient(newClient)
|
||||
if !a.scheduler.UpdateClient(newClient) {
|
||||
logger.LogEvent("活动", "warn", "抢购任务正在运行,调度器客户端将在下次抢购时生效")
|
||||
}
|
||||
if sample, err := newClient.ServerTimeSample(); err == nil {
|
||||
logger.SetDouyuOffset(sample.Offset)
|
||||
}
|
||||
logger.LogEvent("活动", "success", "已切换到活动: %s", act.DisplayName)
|
||||
return types.SwitchActivityResult{Success: true, Activity: act.ID, DisplayName: act.DisplayName}
|
||||
}
|
||||
|
||||
// GetSettings 返回当前配置。
|
||||
func (a *App) GetSettings() types.Config {
|
||||
return a.getCfg()
|
||||
}
|
||||
|
||||
// SaveSettings 保存配置并运行时生效。
|
||||
func (a *App) SaveSettings(cfg types.Config) types.SimpleResult {
|
||||
// 校验
|
||||
if cfg.Concurrency <= 0 {
|
||||
cfg.Concurrency = constants.DefaultConcurrency
|
||||
}
|
||||
if cfg.Attempts <= 0 {
|
||||
cfg.Attempts = constants.DefaultAttempts
|
||||
}
|
||||
if cfg.LogLevel == "" {
|
||||
cfg.LogLevel = "info"
|
||||
}
|
||||
|
||||
// 写入文件
|
||||
if err := config.SaveConfig(a.paths.ConfigFile, cfg); err != nil {
|
||||
logger.LogEvent("配置", "error", "配置保存失败: %v", err)
|
||||
return types.SimpleResult{}
|
||||
}
|
||||
|
||||
// 运行时生效
|
||||
a.setCfg(cfg)
|
||||
logger.SetMinLevel(cfg.LogLevel)
|
||||
a.scheduler.UpdateSettings(cfg.Concurrency, cfg.Attempts, cfg.RetryStages, cfg.SeckillMode)
|
||||
|
||||
logger.LogEvent("配置", "success", "配置已保存并生效: 日志等级=%s 并发=%d 重试=%d 重试阶段=%d 模式=%d trace=%v",
|
||||
cfg.LogLevel, cfg.Concurrency, cfg.Attempts, len(cfg.RetryStages), cfg.SeckillMode, cfg.TraceEnabled)
|
||||
return types.SimpleResult{Success: true}
|
||||
}
|
||||
Reference in New Issue
Block a user