优化抢购成功图片生成逻辑

This commit is contained in:
yml2213
2026-06-27 13:20:53 +08:00
parent c54d64b2eb
commit db917698f9
4 changed files with 189 additions and 21 deletions
+11 -2
View File
@@ -23,11 +23,19 @@ func SaveSuccessImage(account types.Account, product types.Product, roleName str
if font == nil {
return
}
if successDir == "" {
successDir = constants.DefaultSuccessDir
}
if err := os.MkdirAll(successDir, 0755); err != nil {
logger.LogEvent("图片", "error", "成功图片目录创建失败: %v", err)
return
}
productName := product.CommodityName
if productName == "" {
productName = product.CommodityID
}
now := time.Now().In(constants.CST)
width, height := 600, 280
img := image.NewRGBA(image.Rect(0, 0, width, height))
@@ -59,7 +67,7 @@ func SaveSuccessImage(account types.Account, product types.Product, roleName str
Color color.Color
}{
{productName, 20, 25, 42, white},
{"兑换时间:" + time.Now().In(constants.CST).Format("2006年01月02日 15:04:05"), 16, 25, 110, black},
{"兑换时间:" + now.Format("2006年01月02日 15:04:05"), 16, 25, 110, black},
{"游戏昵称:" + utils.EmptyDefault(roleName, "未知"), 16, 25, 150, black},
{"兑换账号:" + account.Name, 16, 25, 190, black},
}
@@ -69,7 +77,8 @@ func SaveSuccessImage(account types.Account, product types.Product, roleName str
_, _ = 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()))
ownerName := utils.EmptyDefault(roleName, account.Name)
name := utils.SafeFilename(fmt.Sprintf("%s_%s_%s.png", ownerName, productName, now.Format("20060102_150405.000")))
path := filepath.Join(successDir, name)
file, err := os.Create(path)
if err != nil {