feat: 添加AI推荐功能及自动刷新机制
新增AI推荐模块,包括后端数据获取接口和前端展示组件 实现自动刷新功能,每5分钟自动更新当前标签页数据 添加手动刷新按钮,优化用户交互体验
This commit is contained in:
@@ -35,6 +35,14 @@ type BidItem struct {
|
||||
UpdatedAt string `json:"updatedAt"`
|
||||
}
|
||||
|
||||
// AiRecommendation AI推荐结构体
|
||||
type AiRecommendation struct {
|
||||
ID string `json:"id"`
|
||||
Title string `json:"title"`
|
||||
Confidence int `json:"confidence"`
|
||||
CreatedAt string `json:"createdAt"`
|
||||
}
|
||||
|
||||
// App struct
|
||||
type App struct {
|
||||
ctx context.Context
|
||||
@@ -212,3 +220,59 @@ func (a *App) GetPinnedBidItems() ([]BidItem, error) {
|
||||
|
||||
return items, nil
|
||||
}
|
||||
|
||||
// GetAiRecommendations 获取 AI 推荐数据
|
||||
func (a *App) GetAiRecommendations() ([]AiRecommendation, error) {
|
||||
dsn := a.GetDatabaseDSN()
|
||||
if dsn == "" {
|
||||
return nil, fmt.Errorf("数据库配置未加载")
|
||||
}
|
||||
|
||||
db, err := sql.Open("mysql", dsn)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("连接数据库失败: %v", err)
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
// 测试连接
|
||||
if err := db.Ping(); err != nil {
|
||||
return nil, fmt.Errorf("数据库连接测试失败: %v", err)
|
||||
}
|
||||
|
||||
// 查询 ai_recommendations 表
|
||||
query := `SELECT id, title, confidence, createdAt
|
||||
FROM ai_recommendations
|
||||
ORDER BY createdAt DESC`
|
||||
|
||||
rows, err := db.Query(query)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("查询失败: %v", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var items []AiRecommendation
|
||||
for rows.Next() {
|
||||
var item AiRecommendation
|
||||
var createdAt time.Time
|
||||
|
||||
err := rows.Scan(
|
||||
&item.ID,
|
||||
&item.Title,
|
||||
&item.Confidence,
|
||||
&createdAt,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("扫描行失败: %v", err)
|
||||
}
|
||||
|
||||
item.CreatedAt = createdAt.Format("2006-01-02 15:04:05")
|
||||
|
||||
items = append(items, item)
|
||||
}
|
||||
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, fmt.Errorf("遍历行失败: %v", err)
|
||||
}
|
||||
|
||||
return items, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user