// ============================================================================ //go:build !service // poem.go — 随机古诗 // // 主窗口首个 Tab(笔记页)footer 内水平滚动显示一首随机古诗; // 数据来自 poetry.palemoky.com 的随机接口,颜色 / 字号 / 刷新间隔可配置。 // ============================================================================ package main import ( "encoding/json" "fmt" "net/http" "strings" "time" ) const ( PoemAPIURL = "https://poetry.palemoky.com/api/poems/random" // 随机古诗接口 ) // PoemData 一首古诗的完整数据(从接口解析) type PoemData struct { Title string `json:"title"` // 题目 Author string `json:"author"` // 作者 Dynasty string `json:"dynasty"` // 朝代 Content []string `json:"content"` // 每句诗(数组,按行显示) Type string `json:"type"` // 体裁,如 五言律诗 } // poemAPIResponse 接口返回的外层结构 type poemAPIResponse struct { Data struct { ID int `json:"id"` Title string `json:"title"` Content []string `json:"content"` Author struct { ID int `json:"id"` Name string `json:"name"` } `json:"author"` Dynasty struct { ID int `json:"id"` Name string `json:"name"` } `json:"dynasty"` Type struct { ID int `json:"id"` Name string `json:"name"` } `json:"type"` } `json:"data"` Lang string `json:"lang"` } // fetchRandomPoem 从接口拉取一首随机古诗;失败时返回错误 func fetchRandomPoem() (PoemData, error) { client := &http.Client{Timeout: 10 * time.Second} resp, err := client.Get(PoemAPIURL) if err != nil { return PoemData{}, fmt.Errorf("请求古诗接口失败: %w", err) } defer resp.Body.Close() if resp.StatusCode != http.StatusOK { return PoemData{}, fmt.Errorf("古诗接口返回状态 %d", resp.StatusCode) } var out poemAPIResponse if err := json.NewDecoder(resp.Body).Decode(&out); err != nil { return PoemData{}, fmt.Errorf("解析古诗数据失败: %w", err) } p := PoemData{ Title: strings.TrimSpace(out.Data.Title), Author: strings.TrimSpace(out.Data.Author.Name), Dynasty: strings.TrimSpace(out.Data.Dynasty.Name), Content: out.Data.Content, Type: strings.TrimSpace(out.Data.Type.Name), } if p.Title == "" && len(p.Content) == 0 { return PoemData{}, fmt.Errorf("古诗接口返回空数据") } return p, nil } // GetRandomPoem 返回一首随机古诗(供主窗口前端加载 / 手动刷新调用) func (s *SettingsService) GetRandomPoem() (PoemData, error) { return fetchRandomPoem() }