mirror of
https://github.com/ArvinLovegood/go-stock.git
synced 2025-07-19 00:00:09 +08:00
refactor(app):重构主程序和优化股票查询功能
- 重构主程序循环,使用 goroutine 启动 systray.Run - 注释掉 onExit 函数中的退出操作 - 优化股票查询功能,增加实时数据获取和处理 - 改进模板替换逻辑,支持多种格式
This commit is contained in:
parent
65060a91ce
commit
bac10a2a04
6
app.go
6
app.go
@ -95,7 +95,7 @@ func (a *App) startup(ctx context.Context) {
|
|||||||
runtime.WindowReloadApp(ctx)
|
runtime.WindowReloadApp(ctx)
|
||||||
|
|
||||||
})
|
})
|
||||||
systray.RunWithExternalLoop(func() {
|
go systray.Run(func() {
|
||||||
onReady(a)
|
onReady(a)
|
||||||
}, func() {
|
}, func() {
|
||||||
onExit(a)
|
onExit(a)
|
||||||
@ -797,8 +797,8 @@ func getMsgTypeName(msgType int) string {
|
|||||||
func onExit(a *App) {
|
func onExit(a *App) {
|
||||||
// 清理操作
|
// 清理操作
|
||||||
logger.SugaredLogger.Infof("onExit")
|
logger.SugaredLogger.Infof("onExit")
|
||||||
systray.Quit()
|
//systray.Quit()
|
||||||
runtime.Quit(a.ctx)
|
//runtime.Quit(a.ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
func onReady(a *App) {
|
func onReady(a *App) {
|
||||||
|
@ -143,29 +143,42 @@ func (o OpenAi) NewChatStream(stock, stockCode, userQuestion string, sysPromptId
|
|||||||
"content": "当前本地时间是:" + time.Now().Format("2006-01-02 15:04:05"),
|
"content": "当前本地时间是:" + time.Now().Format("2006-01-02 15:04:05"),
|
||||||
})
|
})
|
||||||
|
|
||||||
question := ""
|
|
||||||
if userQuestion == "" {
|
|
||||||
replaceTemplates := map[string]string{
|
replaceTemplates := map[string]string{
|
||||||
"{{stockName}}": RemoveAllBlankChar(stock),
|
"{{stockName}}": RemoveAllBlankChar(stock),
|
||||||
"{{stockCode}}": RemoveAllBlankChar(stockCode),
|
"{{stockCode}}": RemoveAllBlankChar(stockCode),
|
||||||
|
"{stockName}": RemoveAllBlankChar(stock),
|
||||||
|
"{stockCode}": RemoveAllBlankChar(stockCode),
|
||||||
|
"stockName": RemoveAllBlankChar(stock),
|
||||||
|
"stockCode": RemoveAllBlankChar(stockCode),
|
||||||
|
}
|
||||||
|
followedStock := NewStockDataApi().GetFollowedStockByStockCode(stockCode)
|
||||||
|
stockData, err := NewStockDataApi().GetStockCodeRealTimeData(stockCode)
|
||||||
|
if err == nil && len(*stockData) > 0 {
|
||||||
|
msg = append(msg, map[string]interface{}{
|
||||||
|
"role": "user",
|
||||||
|
"content": fmt.Sprintf("当前%s[%s]价格是多少?", stock, stockCode),
|
||||||
|
})
|
||||||
|
msg = append(msg, map[string]interface{}{
|
||||||
|
"role": "assistant",
|
||||||
|
"content": fmt.Sprintf("截止到%s,当前%s[%s]价格是%s", (*stockData)[0].Date+" "+(*stockData)[0].Time, stock, stockCode, (*stockData)[0].Price),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
if followedStock.CostPrice > 0 {
|
||||||
|
replaceTemplates["{{costPrice}}"] = convertor.ToString(followedStock.CostPrice)
|
||||||
|
replaceTemplates["{costPrice}"] = convertor.ToString(followedStock.CostPrice)
|
||||||
|
replaceTemplates["costPrice"] = convertor.ToString(followedStock.CostPrice)
|
||||||
}
|
}
|
||||||
|
|
||||||
followedStock := &FollowedStock{
|
question := ""
|
||||||
StockCode: stockCode,
|
if userQuestion == "" {
|
||||||
}
|
|
||||||
db.Dao.Model(&followedStock).Where("stock_code = ?", stockCode).First(followedStock)
|
|
||||||
if followedStock.CostPrice > 0 {
|
|
||||||
replaceTemplates["{{costPrice}}"] = fmt.Sprintf("%.2f", followedStock.CostPrice)
|
|
||||||
}
|
|
||||||
question = strutil.ReplaceWithMap(o.QuestionTemplate, replaceTemplates)
|
question = strutil.ReplaceWithMap(o.QuestionTemplate, replaceTemplates)
|
||||||
} else {
|
} else {
|
||||||
question = userQuestion
|
question = userQuestion
|
||||||
|
question = strutil.ReplaceWithMap(userQuestion, replaceTemplates)
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.SugaredLogger.Infof("NewChatStream stock:%s stockCode:%s", stock, stockCode)
|
logger.SugaredLogger.Infof("NewChatStream stock:%s stockCode:%s", stock, stockCode)
|
||||||
logger.SugaredLogger.Infof("Prompt:%s", sysPrompt)
|
logger.SugaredLogger.Infof("Prompt:%s", sysPrompt)
|
||||||
logger.SugaredLogger.Infof("User Prompt config:%v", o.QuestionTemplate)
|
|
||||||
logger.SugaredLogger.Infof("User question:%s", userQuestion)
|
|
||||||
logger.SugaredLogger.Infof("final question:%s", question)
|
logger.SugaredLogger.Infof("final question:%s", question)
|
||||||
|
|
||||||
wg := &sync.WaitGroup{}
|
wg := &sync.WaitGroup{}
|
||||||
@ -413,7 +426,7 @@ func (o OpenAi) NewChatStream(stock, stockCode, userQuestion string, sysPromptId
|
|||||||
})
|
})
|
||||||
|
|
||||||
//reqJson, _ := json.Marshal(msg)
|
//reqJson, _ := json.Marshal(msg)
|
||||||
//logger.SugaredLogger.Errorf("Stream request: \n%s", reqJson)
|
//logger.SugaredLogger.Errorf("Stream request: \n%s\n", reqJson)
|
||||||
|
|
||||||
client := resty.New()
|
client := resty.New()
|
||||||
client.SetBaseURL(strutil.Trim(o.BaseUrl))
|
client.SetBaseURL(strutil.Trim(o.BaseUrl))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user