初步优化成全部go+wails

This commit is contained in:
yml2213
2026-06-24 19:11:02 +08:00
parent 86374fdd4d
commit 154e55d484
27 changed files with 2222 additions and 1798 deletions
+53
View File
@@ -0,0 +1,53 @@
package image
import (
"errors"
"os"
"github.com/golang/freetype"
"github.com/golang/freetype/truetype"
"golang.org/x/image/font/gofont/goregular"
"douyu_go/internal/logger"
)
// LoadFont 预加载字体,优先使用本地或系统中文字体,缺失时使用内置字体兜底。
func LoadFont(fontFiles []string) *truetype.Font {
source, font, err := parseFirstFont(fontFiles)
if err != nil {
source = "内置 Go Regular 字体"
font, err = freetype.ParseFont(goregular.TTF)
if err != nil {
logger.LogEvent("字体", "error", "内置字体解析失败,成功图片功能将跳过: %v", err)
return nil
}
logger.LogEvent("字体", "warn", "未找到可用系统字体,使用内置字体兜底,中文可能显示不完整")
}
logger.LogEvent("字体", "success", "字体加载完成: %s", source)
return font
}
func parseFirstFont(candidates []string) (string, *truetype.Font, error) {
for _, path := range candidates {
if path == "" {
continue
}
fontBytes, err := os.ReadFile(path)
if err != nil {
if !errors.Is(err, os.ErrNotExist) {
logger.LogEvent("字体", "warn", "字体读取失败,已跳过: %s: %v", path, err)
}
continue
}
if len(fontBytes) == 0 {
continue
}
font, err := freetype.ParseFont(fontBytes)
if err != nil {
logger.LogEvent("字体", "warn", "字体解析失败,已跳过: %s: %v", path, err)
continue
}
return path, font, nil
}
return "", nil, errors.New("没有可用字体")
}
+85
View File
@@ -0,0 +1,85 @@
package image
import (
"fmt"
"image"
"image/color"
"image/png"
"os"
"path/filepath"
"time"
"github.com/golang/freetype"
"github.com/golang/freetype/truetype"
"douyu_go/internal/constants"
"douyu_go/internal/logger"
"douyu_go/internal/types"
"douyu_go/internal/utils"
)
// SaveSuccessImage 保存成功兑换结果图片。
func SaveSuccessImage(account types.Account, product types.Product, roleName string, font *truetype.Font, successDir string) {
if font == nil {
return
}
productName := product.CommodityName
if productName == "" {
productName = product.CommodityID
}
width, height := 600, 280
img := image.NewRGBA(image.Rect(0, 0, width, height))
bgColor := color.RGBA{213, 239, 253, 255}
topColor := color.RGBA{76, 175, 80, 255}
white := color.RGBA{255, 255, 255, 255}
black := color.RGBA{0, 0, 0, 255}
for x := 0; x < width; x++ {
for y := 0; y < height; y++ {
if y < 60 {
img.Set(x, y, topColor)
} else {
img.Set(x, y, bgColor)
}
}
}
ctx := freetype.NewContext()
ctx.SetDPI(72)
ctx.SetFont(font)
ctx.SetClip(img.Bounds())
ctx.SetDst(img)
lines := []struct {
Text string
Size float64
X, Y int
Color color.Color
}{
{productName, 20, 25, 42, white},
{"兑换时间:" + time.Now().In(constants.CST).Format("2006年01月02日 15:04:05"), 16, 25, 110, black},
{"游戏昵称:" + utils.EmptyDefault(roleName, "未知"), 16, 25, 150, black},
{"兑换账号:" + account.Name, 16, 25, 190, black},
}
for _, line := range lines {
ctx.SetFontSize(line.Size)
ctx.SetSrc(image.NewUniform(line.Color))
_, _ = ctx.DrawString(line.Text, freetype.Pt(line.X, line.Y))
}
name := utils.SafeFilename(fmt.Sprintf("%s_%s_%d.png", account.Name, productName, time.Now().Unix()))
path := filepath.Join(successDir, name)
file, err := os.Create(path)
if err != nil {
logger.LogEvent("图片", "error", "成功图片创建失败: %v", err)
return
}
defer file.Close()
if err := png.Encode(file, img); err != nil {
logger.LogEvent("图片", "error", "成功图片保存失败: %v", err)
return
}
logger.LogEvent("图片", "success", "成功图片已保存: %s", path)
}