From d3c6c1d570fa33e48910c87e2fd6145dae0a6a6d Mon Sep 17 00:00:00 2001 From: spark Date: Wed, 8 Jan 2025 15:28:08 +0800 Subject: [PATCH] =?UTF-8?q?feat(app):=20=E4=BC=98=E5=8C=96=E8=82=A1?= =?UTF-8?q?=E7=A5=A8=E7=9B=91=E6=8E=A7=E5=92=8C=E4=BA=A4=E6=98=93=E6=97=B6?= =?UTF-8?q?=E9=97=B4=E5=88=A4=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加了判断是否为交易日和交易时间的函数 - 修改了股票价格更新逻辑,只在交易时间内进行监控 - 优化了股票价格显示,增加了上次当前价格字段 - 更新了前端组件,支持显示股票价格变化动画 --- app.go | 43 ++++++++++++++++++-- backend/data/stock_data_api.go | 67 ++++++++++++++++--------------- frontend/src/components/stock.vue | 15 ++++++- frontend/wailsjs/go/models.ts | 2 + main.go | 2 +- 5 files changed, 89 insertions(+), 40 deletions(-) diff --git a/app.go b/app.go index 8577d06..581f0ab 100644 --- a/app.go +++ b/app.go @@ -53,7 +53,7 @@ func (a *App) domReady(ctx context.Context) { ticker := time.NewTicker(time.Second) defer ticker.Stop() for range ticker.C { - runtime.WindowSetTitle(ctx, "go-stock "+time.Now().Format("2006-01-02 15:04:05")) + runtime.WindowSetTitle(ctx, "go-stock "+time.Now().Format("2006-01-02 15:04")) } }() @@ -62,10 +62,46 @@ func (a *App) domReady(ctx context.Context) { ticker := time.NewTicker(time.Second * 2) defer ticker.Stop() for range ticker.C { - MonitorStockPrices(a) + if isTradingTime(time.Now()) { + MonitorStockPrices(a) + } } }() } + +// isTradingDay 判断是否是交易日 +func isTradingDay(date time.Time) bool { + weekday := date.Weekday() + // 判断是否是周末 + if weekday == time.Saturday || weekday == time.Sunday { + return false + } + // 这里可以添加具体的节假日判断逻辑 + // 例如:判断是否是春节、国庆节等 + return true +} + +// isTradingTime 判断是否是交易时间 +func isTradingTime(date time.Time) bool { + if !isTradingDay(date) { + return false + } + + hour, minute, _ := date.Clock() + + // 判断是否在9:15到11:30之间 + if (hour == 9 && minute >= 15) || (hour == 10) || (hour == 11 && minute <= 30) { + return true + } + + // 判断是否在13:00到15:00之间 + if (hour == 13) || (hour == 14) || (hour == 15 && minute <= 0) { + return true + } + + return false +} + func MonitorStockPrices(a *App) { dest := &[]data.FollowedStock{} db.Dao.Model(&data.FollowedStock{}).Find(dest) @@ -78,18 +114,17 @@ func MonitorStockPrices(a *App) { logger.SugaredLogger.Errorf("get stock code real time data error:%s", err.Error()) return } - price, err := convertor.ToFloat(stockData.Price) if err != nil { return } + stockData.PrePrice = follow.Price if follow.Price != price { runtime.EventsEmit(a.ctx, "stock_price", stockData) go db.Dao.Model(follow).Where("stock_code = ?", stockCode).Updates(map[string]interface{}{ "price": stockData.Price, }) } - }() } } diff --git a/backend/data/stock_data_api.go b/backend/data/stock_data_api.go index af809ec..7ddea77 100644 --- a/backend/data/stock_data_api.go +++ b/backend/data/stock_data_api.go @@ -33,39 +33,40 @@ type StockDataApi struct { } type StockInfo struct { gorm.Model - Date string `json:"日期" gorm:"index"` - Time string `json:"时间" gorm:"index"` - Code string `json:"股票代码" gorm:"index"` - Name string `json:"股票名称" gorm:"index"` - Price string `json:"当前价格"` - Volume string `json:"成交的股票数"` - Amount string `json:"成交金额"` - Open string `json:"今日开盘价"` - PreClose string `json:"昨日收盘价"` - High string `json:"今日最低价"` - Low string `json:"今日最高价"` - Bid string `json:"竞买价"` - Ask string `json:"竞卖价"` - B1P string `json:"买一报价"` - B1V string `json:"买一申报"` - B2P string `json:"买二报价"` - B2V string `json:"买二申报"` - B3P string `json:"买三报价"` - B3V string `json:"买三申报"` - B4P string `json:"买四报价"` - B4V string `json:"买四申报"` - B5P string `json:"买五报价"` - B5V string `json:"买五申报"` - A1P string `json:"卖一报价"` - A1V string `json:"卖一申报"` - A2P string `json:"卖二报价"` - A2V string `json:"卖二申报"` - A3P string `json:"卖三报价"` - A3V string `json:"卖三申报"` - A4P string `json:"卖四报价"` - A4V string `json:"卖四申报"` - A5P string `json:"卖五报价"` - A5V string `json:"卖五申报"` + Date string `json:"日期" gorm:"index"` + Time string `json:"时间" gorm:"index"` + Code string `json:"股票代码" gorm:"index"` + Name string `json:"股票名称" gorm:"index"` + PrePrice float64 `json:"上次当前价格"` + Price string `json:"当前价格"` + Volume string `json:"成交的股票数"` + Amount string `json:"成交金额"` + Open string `json:"今日开盘价"` + PreClose string `json:"昨日收盘价"` + High string `json:"今日最低价"` + Low string `json:"今日最高价"` + Bid string `json:"竞买价"` + Ask string `json:"竞卖价"` + B1P string `json:"买一报价"` + B1V string `json:"买一申报"` + B2P string `json:"买二报价"` + B2V string `json:"买二申报"` + B3P string `json:"买三报价"` + B3V string `json:"买三申报"` + B4P string `json:"买四报价"` + B4V string `json:"买四申报"` + B5P string `json:"买五报价"` + B5V string `json:"买五申报"` + A1P string `json:"卖一报价"` + A1V string `json:"卖一申报"` + A2P string `json:"卖二报价"` + A2V string `json:"卖二申报"` + A3P string `json:"卖三报价"` + A3V string `json:"卖三申报"` + A4P string `json:"卖四报价"` + A4V string `json:"卖四申报"` + A5P string `json:"卖五报价"` + A5V string `json:"卖五申报"` } func (receiver StockInfo) TableName() string { diff --git a/frontend/src/components/stock.vue b/frontend/src/components/stock.vue index fda15e3..0451a4c 100644 --- a/frontend/src/components/stock.vue +++ b/frontend/src/components/stock.vue @@ -194,6 +194,12 @@ async function updateData(result) { let s=(result["当前价格"]-result["昨日收盘价"])*100/result["昨日收盘价"] let roundedNum = s.toFixed(2); // 将数字转换为保留两位小数的字符串形式 + + // let s2=(result["上次当前价格"]-result["昨日收盘价"])*100/result["昨日收盘价"] + // let roundedNum2 = s2.toFixed(2); // 将数字转换为保留两位小数的字符串形式 + + result.rf=roundedNum + // result.rf2=roundedNum2 result.s=roundedNum+"%" result.highRate=((result["今日最高价"]-result["今日开盘价"])*100/result["今日开盘价"]).toFixed(2)+"%" @@ -252,7 +258,7 @@ async function monitor() { let s=(result["当前价格"]-result["昨日收盘价"])*100/result["昨日收盘价"] let roundedNum = s.toFixed(2); // 将数字转换为保留两位小数的字符串形式 result.s=roundedNum+"%" - + result.rf=roundedNum result.highRate=((result["今日最高价"]-result["今日开盘价"])*100/result["今日开盘价"]).toFixed(2)+"%" result.lowRate=((result["今日最低价"]-result["今日开盘价"])*100/result["今日开盘价"]).toFixed(2)+"%" @@ -449,7 +455,12 @@ function getHeight() { - {{result["当前价格"]}}{{ result.s}}  + + + + + % +   {{result.profitAmountToday}} diff --git a/frontend/wailsjs/go/models.ts b/frontend/wailsjs/go/models.ts index e27828a..8a1e2ff 100644 --- a/frontend/wailsjs/go/models.ts +++ b/frontend/wailsjs/go/models.ts @@ -138,6 +138,7 @@ export namespace data { "时间": string; "股票代码": string; "股票名称": string; + "上次当前价格": number; "当前价格": string; "成交的股票数": string; "成交金额": string; @@ -182,6 +183,7 @@ export namespace data { this["时间"] = source["时间"]; this["股票代码"] = source["股票代码"]; this["股票名称"] = source["股票名称"]; + this["上次当前价格"] = source["上次当前价格"]; this["当前价格"] = source["当前价格"]; this["成交的股票数"] = source["成交的股票数"]; this["成交金额"] = source["成交金额"]; diff --git a/main.go b/main.go index 59cfc93..496972c 100644 --- a/main.go +++ b/main.go @@ -44,7 +44,7 @@ func main() { if stocksBin != nil && len(stocksBin) > 0 { go initStockData() } - //updateBasicInfo() + updateBasicInfo() // Create an instance of the app structure app := NewApp()