新增: 壁纸切换(主题/本地图片/Bing/纯色渐变)

This commit is contained in:
2026-05-25 19:54:32 +08:00
parent a804db3579
commit bb1574641f
14 changed files with 868 additions and 395 deletions

82
bing.go Normal file
View File

@@ -0,0 +1,82 @@
package main
import (
"encoding/json"
"io"
"log"
"os"
"path/filepath"
"strings"
"time"
)
const bingAPI = "https://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&mkt=zh-CN"
type bingResponse struct {
Images []struct {
URL string `json:"url"`
URLBase string `json:"urlbase"`
Copyright string `json:"copyright"`
} `json:"images"`
}
func fetchBingWallpaper() {
resp, err := httpClient.Get(bingAPI)
if err != nil {
log.Println("Bing API 请求失败:", err)
return
}
defer resp.Body.Close()
data, err := io.ReadAll(resp.Body)
if err != nil {
return
}
var br bingResponse
if json.Unmarshal(data, &br) != nil || len(br.Images) == 0 {
log.Println("Bing API 解析失败")
return
}
imgURL := br.Images[0].URL
if !strings.HasPrefix(imgURL, "http") {
imgURL = "https://www.bing.com" + imgURL
}
imgResp, err := httpClient.Get(imgURL)
if err != nil {
log.Println("Bing 图片下载失败:", err)
return
}
defer imgResp.Body.Close()
imgData, err := io.ReadAll(imgResp.Body)
if err != nil {
return
}
bingPath := filepath.Join(configDir(), "bing_wallpaper.jpg")
if err := os.WriteFile(bingPath, imgData, 0644); err != nil {
log.Println("Bing 壁纸缓存失败:", err)
return
}
log.Printf("Bing 壁纸已缓存: %s (%d bytes)", bingPath, len(imgData))
reloadWallpaper()
}
func bingWallpaperLoop() {
cfg := loadConfig()
if cfg.WallpaperType == WPBing {
bingPath := filepath.Join(configDir(), "bing_wallpaper.jpg")
if _, err := os.Stat(bingPath); err != nil {
fetchBingWallpaper()
}
}
ticker := time.NewTicker(4 * time.Hour)
for range ticker.C {
cfg := loadConfig()
if cfg.WallpaperType == WPBing {
fetchBingWallpaper()
}
}
}