mirror of
https://github.com/ArvinLovegood/go-stock.git
synced 2025-07-19 00:00:09 +08:00
fix(data):优化Edge浏览器调用逻辑修复AI分析使用BUG
- 在 Windows 系统上动态检查 Edge 浏览器安装情况 - 根据系统环境选择合适的 Edge 可执行文件路径 - 优化了 openai_api 和 stock_data_api 中的 Edge 调用逻辑
This commit is contained in:
parent
b764c978f1
commit
70ee9df22a
@ -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()
|
||||||
|
if e {
|
||||||
pctx, pcancel := chromedp.NewExecAllocator(
|
pctx, pcancel := chromedp.NewExecAllocator(
|
||||||
timeoutCtx,
|
timeoutCtx,
|
||||||
chromedp.ExecPath(edgePath),
|
chromedp.ExecPath(path),
|
||||||
chromedp.Flag("headless", true),
|
chromedp.Flag("headless", true),
|
||||||
)
|
)
|
||||||
defer pcancel()
|
defer pcancel()
|
||||||
ctx, cancel := chromedp.NewContext(
|
ctx, cancel = chromedp.NewContext(
|
||||||
pctx,
|
pctx,
|
||||||
chromedp.WithLogf(logger.SugaredLogger.Infof),
|
chromedp.WithLogf(logger.SugaredLogger.Infof),
|
||||||
chromedp.WithErrorf(logger.SugaredLogger.Errorf),
|
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) {
|
||||||
|
@ -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()
|
||||||
|
if e {
|
||||||
pctx, pcancel := chromedp.NewExecAllocator(
|
pctx, pcancel := chromedp.NewExecAllocator(
|
||||||
timeoutCtx,
|
timeoutCtx,
|
||||||
chromedp.ExecPath(edgePath),
|
chromedp.ExecPath(path),
|
||||||
chromedp.Flag("headless", true),
|
chromedp.Flag("headless", true),
|
||||||
)
|
)
|
||||||
defer pcancel()
|
defer pcancel()
|
||||||
ctx, cancel := chromedp.NewContext(
|
ctx, cancel = chromedp.NewContext(
|
||||||
pctx,
|
pctx,
|
||||||
chromedp.WithLogf(logger.SugaredLogger.Infof),
|
chromedp.WithLogf(logger.SugaredLogger.Infof),
|
||||||
chromedp.WithErrorf(logger.SugaredLogger.Errorf),
|
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
|
||||||
|
path, e := checkEdgeOnWindows()
|
||||||
|
if e {
|
||||||
pctx, pcancel := chromedp.NewExecAllocator(
|
pctx, pcancel := chromedp.NewExecAllocator(
|
||||||
timeoutCtx,
|
timeoutCtx,
|
||||||
chromedp.ExecPath(edgePath),
|
chromedp.ExecPath(path),
|
||||||
chromedp.Flag("headless", true),
|
chromedp.Flag("headless", true),
|
||||||
)
|
)
|
||||||
defer pcancel()
|
defer pcancel()
|
||||||
ctx, cancel := chromedp.NewContext(
|
ctx, cancel = chromedp.NewContext(
|
||||||
pctx,
|
pctx,
|
||||||
chromedp.WithLogf(logger.SugaredLogger.Infof),
|
chromedp.WithLogf(logger.SugaredLogger.Infof),
|
||||||
chromedp.WithErrorf(logger.SugaredLogger.Errorf),
|
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
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user