From 9fae9fc03468b528db5723abcafeefb11f5c91fc Mon Sep 17 00:00:00 2001 From: spark Date: Mon, 6 Jan 2025 16:44:22 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=20Linux=20=E5=B9=B3?= =?UTF-8?q?=E5=8F=B0=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增 app_linux.go 文件,实现 Linux 平台下的应用逻辑 - 添加缓存功能,用于限制钉钉消息发送频率- 实现股票数据相关功能,包括获取股票列表、关注股票等 - 添加应用启动、关闭等生命周期方法 --- app.go | 2 + app_linux.go | 120 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 122 insertions(+) create mode 100644 app_linux.go diff --git a/app.go b/app.go index 82ce23d..dff3240 100644 --- a/app.go +++ b/app.go @@ -1,3 +1,5 @@ +//go:build windows + package main import ( diff --git a/app_linux.go b/app_linux.go new file mode 100644 index 0000000..92490b7 --- /dev/null +++ b/app_linux.go @@ -0,0 +1,120 @@ +//go:build linux +// +build linux + +package main + +import ( + "context" + "github.com/coocood/freecache" + "github.com/wailsapp/wails/v2/pkg/runtime" + "go-stock/backend/data" + "go-stock/backend/logger" +) + +// App struct +type App struct { + ctx context.Context + cache *freecache.Cache +} + +// NewApp creates a new App application struct +func NewApp() *App { + cacheSize := 512 * 1024 + cache := freecache.NewCache(cacheSize) + return &App{ + cache: cache, + } +} + +// startup is called at application startup +func (a *App) startup(ctx context.Context) { + // Perform your setup here + a.ctx = ctx + +} + +// domReady is called after front-end resources have been loaded +func (a *App) domReady(ctx context.Context) { + // Add your action here + //ticker := time.NewTicker(time.Second) + //defer ticker.Stop() + ////定时更新数据 + //go func() { + // for range ticker.C { + // runtime.WindowSetTitle(ctx, "go-stock "+time.Now().Format("2006-01-02 15:04:05")) + // } + //}() +} + +// beforeClose is called when the application is about to quit, +// either by clicking the window close button or calling runtime.Quit. +// Returning true will cause the application to continue, false will continue shutdown as normal. +func (a *App) beforeClose(ctx context.Context) (prevent bool) { + + dialog, err := runtime.MessageDialog(ctx, runtime.MessageDialogOptions{ + Type: runtime.QuestionDialog, + Title: "go-stock", + Message: "确定关闭吗?", + Buttons: []string{"确定"}, + Icon: icon, + CancelButton: "取消", + }) + + if err != nil { + return false + } + logger.SugaredLogger.Debugf("dialog:%s", dialog) + if dialog == "No" { + return true + } + return false +} + +// shutdown is called at application termination +func (a *App) shutdown(ctx context.Context) { + // Perform your teardown here +} + +// Greet returns a greeting for the given name +func (a *App) Greet(name string) *data.StockInfo { + stockInfo, _ := data.NewStockDataApi().GetStockCodeRealTimeData(name) + return stockInfo +} + +func (a *App) Follow(stockCode string) string { + return data.NewStockDataApi().Follow(stockCode) +} + +func (a *App) UnFollow(stockCode string) string { + return data.NewStockDataApi().UnFollow(stockCode) +} + +func (a *App) GetFollowList() []data.FollowedStock { + return data.NewStockDataApi().GetFollowList() +} + +func (a *App) GetStockList(key string) []data.StockBasic { + return data.NewStockDataApi().GetStockList(key) +} + +func (a *App) SetCostPriceAndVolume(stockCode string, price float64, volume int64) string { + return data.NewStockDataApi().SetCostPriceAndVolume(price, volume, stockCode) +} + +func (a *App) SetAlarmChangePercent(val, alarmPrice float64, stockCode string) string { + return data.NewStockDataApi().SetAlarmChangePercent(val, alarmPrice, stockCode) +} + +func (a *App) SendDingDingMessage(message string, stockCode string) string { + ttl, _ := a.cache.TTL([]byte(stockCode)) + logger.SugaredLogger.Infof("stockCode %s ttl:%d", stockCode, ttl) + if ttl > 0 { + return "" + } + err := a.cache.Set([]byte(stockCode), []byte("1"), 60*5) + if err != nil { + logger.SugaredLogger.Errorf("set cache error:%s", err.Error()) + return "" + } + return data.NewDingDingAPI().SendDingDingMessage(message) +}