根目录原 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),元文件更清晰
75 lines
2.1 KiB
Go
75 lines
2.1 KiB
Go
package app
|
|
|
|
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}
|
|
}
|