fix(data):优化Edge浏览器调用逻辑修复AI分析使用BUG

- 在 Windows 系统上动态检查 Edge 浏览器安装情况
- 根据系统环境选择合适的 Edge 可执行文件路径
- 优化了 openai_api 和 stock_data_api 中的 Edge 调用逻辑
This commit is contained in:
spark 2025-02-12 12:17:51 +08:00
parent b764c978f1
commit 70ee9df22a
2 changed files with 89 additions and 43 deletions

View File

@ -293,20 +293,28 @@ func GetFinancialReports(stockCode string) *[]string {
// 创建一个 chromedp 上下文 // 创建一个 chromedp 上下文
timeoutCtx, timeoutCtxCancel := context.WithTimeout(context.Background(), 30*time.Second) timeoutCtx, timeoutCtxCancel := context.WithTimeout(context.Background(), 30*time.Second)
defer timeoutCtxCancel() defer timeoutCtxCancel()
var ctx context.Context
edgePath := `C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe` var cancel context.CancelFunc
path, e := checkEdgeOnWindows()
pctx, pcancel := chromedp.NewExecAllocator( if e {
timeoutCtx, pctx, pcancel := chromedp.NewExecAllocator(
chromedp.ExecPath(edgePath), timeoutCtx,
chromedp.Flag("headless", true), chromedp.ExecPath(path),
) chromedp.Flag("headless", true),
defer pcancel() )
ctx, cancel := chromedp.NewContext( defer pcancel()
pctx, ctx, cancel = chromedp.NewContext(
chromedp.WithLogf(logger.SugaredLogger.Infof), pctx,
chromedp.WithErrorf(logger.SugaredLogger.Errorf), chromedp.WithLogf(logger.SugaredLogger.Infof),
) chromedp.WithErrorf(logger.SugaredLogger.Errorf),
)
} else {
ctx, cancel = chromedp.NewContext(
timeoutCtx,
chromedp.WithLogf(logger.SugaredLogger.Infof),
chromedp.WithErrorf(logger.SugaredLogger.Errorf),
)
}
defer cancel() defer cancel()
defer func(ctx context.Context) { defer func(ctx context.Context) {

View File

@ -19,6 +19,7 @@ import (
"github.com/go-resty/resty/v2" "github.com/go-resty/resty/v2"
"go-stock/backend/db" "go-stock/backend/db"
"go-stock/backend/logger" "go-stock/backend/logger"
"golang.org/x/sys/windows/registry"
"golang.org/x/text/encoding/simplifiedchinese" "golang.org/x/text/encoding/simplifiedchinese"
"golang.org/x/text/transform" "golang.org/x/text/transform"
"gorm.io/gorm" "gorm.io/gorm"
@ -517,25 +518,32 @@ func (IndexBasic) TableName() string {
func SearchStockPriceInfo(stockCode string) *[]string { func SearchStockPriceInfo(stockCode string) *[]string {
url := "https://www.cls.cn/stock?code=" + stockCode url := "https://www.cls.cn/stock?code=" + stockCode
// 创建一个 chromedp 上下文
// 创建一个 chromedp 上下文
timeoutCtx, timeoutCtxCancel := context.WithTimeout(context.Background(), 30*time.Second) timeoutCtx, timeoutCtxCancel := context.WithTimeout(context.Background(), 30*time.Second)
defer timeoutCtxCancel() defer timeoutCtxCancel()
var ctx context.Context
edgePath := `C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe` var cancel context.CancelFunc
path, e := checkEdgeOnWindows()
pctx, pcancel := chromedp.NewExecAllocator( if e {
timeoutCtx, pctx, pcancel := chromedp.NewExecAllocator(
chromedp.ExecPath(edgePath), timeoutCtx,
chromedp.Flag("headless", true), chromedp.ExecPath(path),
) chromedp.Flag("headless", true),
defer pcancel() )
ctx, cancel := chromedp.NewContext( defer pcancel()
pctx, ctx, cancel = chromedp.NewContext(
chromedp.WithLogf(logger.SugaredLogger.Infof), pctx,
chromedp.WithErrorf(logger.SugaredLogger.Errorf), chromedp.WithLogf(logger.SugaredLogger.Infof),
) chromedp.WithErrorf(logger.SugaredLogger.Errorf),
)
} else {
ctx, cancel = chromedp.NewContext(
timeoutCtx,
chromedp.WithLogf(logger.SugaredLogger.Infof),
chromedp.WithErrorf(logger.SugaredLogger.Errorf),
)
}
defer cancel() defer cancel()
defer func(ctx context.Context) { defer func(ctx context.Context) {
@ -603,20 +611,30 @@ func SearchStockInfo(stock, msgType string) *[]string {
// 创建一个 chromedp 上下文 // 创建一个 chromedp 上下文
timeoutCtx, timeoutCtxCancel := context.WithTimeout(context.Background(), 30*time.Second) timeoutCtx, timeoutCtxCancel := context.WithTimeout(context.Background(), 30*time.Second)
defer timeoutCtxCancel() defer timeoutCtxCancel()
edgePath := `C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe` var ctx context.Context
var cancel context.CancelFunc
pctx, pcancel := chromedp.NewExecAllocator( path, e := checkEdgeOnWindows()
timeoutCtx, if e {
chromedp.ExecPath(edgePath), pctx, pcancel := chromedp.NewExecAllocator(
chromedp.Flag("headless", true), timeoutCtx,
) chromedp.ExecPath(path),
defer pcancel() chromedp.Flag("headless", true),
ctx, cancel := chromedp.NewContext( )
pctx, defer pcancel()
chromedp.WithLogf(logger.SugaredLogger.Infof), ctx, cancel = chromedp.NewContext(
chromedp.WithErrorf(logger.SugaredLogger.Errorf), pctx,
) chromedp.WithLogf(logger.SugaredLogger.Infof),
chromedp.WithErrorf(logger.SugaredLogger.Errorf),
)
} else {
ctx, cancel = chromedp.NewContext(
timeoutCtx,
chromedp.WithLogf(logger.SugaredLogger.Infof),
chromedp.WithErrorf(logger.SugaredLogger.Errorf),
)
}
defer cancel() defer cancel()
defer func(ctx context.Context) { defer func(ctx context.Context) {
err := chromedp.Cancel(ctx) err := chromedp.Cancel(ctx)
if err != nil { if err != nil {
@ -690,3 +708,23 @@ func SearchStockInfoByCode(stock string) *[]string {
}) })
return &messages return &messages
} }
// checkEdgeOnWindows 在 Windows 系统上检查Edge浏览器是否安装并返回安装路径
func checkEdgeOnWindows() (string, bool) {
key, err := registry.OpenKey(registry.LOCAL_MACHINE, `SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\msedge.exe`, registry.QUERY_VALUE)
if err != nil {
// 尝试在 WOW6432Node 中查找(适用于 64 位系统上的 32 位程序)
key, err = registry.OpenKey(registry.LOCAL_MACHINE, `SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\App Paths\msedge.exe`, registry.QUERY_VALUE)
if err != nil {
return "", false
}
defer key.Close()
}
defer key.Close()
path, _, err := key.GetStringValue("Path")
logger.SugaredLogger.Infof("Edge安装路径%s", path)
if err != nil {
return "", false
}
return path + "\\msedge.exe", true
}