88 lines
1.8 KiB
Go
88 lines
1.8 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"dypid-client/internal/config"
|
|
"dypid-client/internal/uploader"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/wailsapp/wails/v2/pkg/runtime"
|
|
)
|
|
|
|
// App struct
|
|
type App struct {
|
|
ctx context.Context
|
|
logChan chan string
|
|
uploaderCTX context.Context
|
|
uploaderCancel context.CancelFunc
|
|
isRun bool
|
|
}
|
|
|
|
func NewApp() *App {
|
|
return &App{}
|
|
}
|
|
|
|
// startup 程序初始化
|
|
func (a *App) startup(ctx context.Context) {
|
|
a.ctx = ctx
|
|
|
|
// 后台 goroutine 持续推送日志
|
|
a.logChan = make(chan string, 100)
|
|
go func() {
|
|
for log := range a.logChan {
|
|
runtime.EventsEmit(a.ctx, "log", log)
|
|
time.Sleep(time.Millisecond)
|
|
}
|
|
}()
|
|
|
|
// 后台 goroutine 持续推送运行状态
|
|
go func() {
|
|
for {
|
|
time.Sleep(250 * time.Millisecond)
|
|
runtime.EventsEmit(a.ctx, "is-run", a.isRun)
|
|
}
|
|
}()
|
|
|
|
//在程序启动时运行上传程序
|
|
if config.APPConfig.IsRunOnStart {
|
|
time.Sleep(time.Second)
|
|
a.uploaderCTX, a.uploaderCancel = context.WithCancel(a.ctx)
|
|
go uploader.StartLooking(a.uploaderCTX, &a.logChan, config.APPConfig.CheckDir)
|
|
a.isRun = true
|
|
}
|
|
}
|
|
|
|
// SelectPath 打开选择路径弹框
|
|
func (a *App) SelectPath() string {
|
|
dialog, _ := runtime.OpenDirectoryDialog(a.ctx, runtime.OpenDialogOptions{})
|
|
fmt.Println("选择路径:", dialog)
|
|
return dialog
|
|
}
|
|
|
|
func (a *App) GetConfig() config.Config {
|
|
return config.APPConfig
|
|
}
|
|
|
|
func (a *App) WriteConfig(key string, value any) {
|
|
fmt.Println("写入配置:", key, value)
|
|
config.WriteConfig(key, value)
|
|
}
|
|
|
|
func (a *App) StartUpload() {
|
|
if a.isRun {
|
|
return
|
|
}
|
|
a.uploaderCTX, a.uploaderCancel = context.WithCancel(a.ctx)
|
|
go uploader.StartLooking(a.uploaderCTX, &a.logChan, config.APPConfig.CheckDir)
|
|
a.isRun = true
|
|
}
|
|
|
|
func (a *App) StopUpload() {
|
|
if a.isRun {
|
|
a.uploaderCancel()
|
|
}
|
|
a.isRun = false
|
|
uploader.AddLog(&a.logChan, "上传程序已退出")
|
|
}
|