i18n翻译

This commit is contained in:
hu xiaotong
2025-07-17 15:36:24 +08:00
parent 8657bc4eea
commit 7b7f8c31d7
5 changed files with 182 additions and 40 deletions

View File

@@ -2,12 +2,17 @@ package cron
import (
"context"
"epic/internal/dao"
"epic/internal/model/entity"
"epic/internal/service"
"epic/internal/util"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gcron"
"github.com/gogf/gf/v2/os/gtime"
"strings"
"sync"
"time"
)
type Logic struct {
@@ -168,6 +173,13 @@ func (l *Logic) registerDefaultJobs(ctx context.Context) error {
return err
}
// 每30分钟执行一次OSS预签名URL缓存刷新任务
if err := l.AddJob(ctx, "oss_presignurl_refresh", "0 0/30 * * * *", func() {
l.refreshOssPresignUrlCacheJob(ctx)
}); err != nil {
return err
}
return nil
}
@@ -244,3 +256,43 @@ func (l *Logic) refreshCache(ctx context.Context) {
g.Log().Info(ctx, "Cache refresh completed")
}
// 刷新OSS图片预签名URL缓存的定时任务
func (l *Logic) refreshOssPresignUrlCacheJob(ctx context.Context) {
g.Log().Info(ctx, "Starting OSS presigned URL cache refresh...")
// 1. 从数据库读取所有英雄图片地址
var dbHeroes []*entity.EpicHeroInfo
err := dao.EpicHeroInfo.Ctx(ctx).Scan(&dbHeroes)
if err != nil {
g.Log().Error(ctx, "Failed to query hero info for OSS presign refresh:", err)
return
}
// 2. 提取OSS图片key去掉域名和bucket前缀
var keys []string
for _, hero := range dbHeroes {
headImgUrl := hero.HeadImgUrl
if headImgUrl == "" {
continue
}
// 只处理以http开头的图片地址
// 例https://s3.bitiful.net/bucket/epic/hero/xxx.png 或 https://bfoss.htoop.cn/epic/hero/xxx.png
// 目标key: epic/hero/xxx.png
key := ""
if idx := strings.Index(headImgUrl, "/epic/hero/"); idx != -1 {
key = headImgUrl[idx+1:]
}
if key != "" {
keys = append(keys, key)
}
}
expire := 1 * time.Hour // 预签名URL有效期
err = util.RefreshOssPresignedUrlCache(ctx, keys, expire)
if err != nil {
g.Log().Error(ctx, "OSS presigned URL cache refresh failed:", err)
} else {
g.Log().Info(ctx, "OSS presigned URL cache refresh completed")
}
}

View File

@@ -195,6 +195,25 @@ func (t *ThirdPartyDataSync) processAndSaveHeroData(ctx context.Context, data []
//zhAttribute := i18n.GetZh(ctx, hero.Attribute)
fmt.Println(hero.Assets.Image)
// 上传图片到 OSS并获取自定义域名的访问路径
ossObjectKey := fmt.Sprintf("epic/hero/images/%s.png", hero.Code)
ossUrl, err := util.DownloadAndUploadToOSS(ctx, hero.Assets.Icon, ossObjectKey)
if err != nil {
g.Log().Error(ctx, "上传英雄图片到OSS失败:", err)
return err // 直接返回,后续数据库不会插入
}
fmt.Println(ossUrl)
// 替换为自定义域名
customImgUrl := ""
if ossUrl != "" {
prefix := consts.S3Endpoint + "/" + consts.S3Bucket
if len(ossUrl) > len(prefix) && ossUrl[:len(prefix)] == prefix {
customImgUrl = consts.S3CustomDomain + ossUrl[len(prefix):]
} else {
customImgUrl = ossUrl // fallback
}
}
newHero := &entity.EpicHeroInfo{
Id: 0,
HeroName: zhHeroName,
@@ -209,7 +228,7 @@ func (t *ThirdPartyDataSync) processAndSaveHeroData(ctx context.Context, data []
Rarity: strconv.Itoa(hero.Rarity),
Role: hero.Role,
//Zodiac: "",
HeadImgUrl: "",
HeadImgUrl: customImgUrl,
Attribute: hero.Attribute,
Remark: "",
RawJson: heroJson,
@@ -221,6 +240,15 @@ func (t *ThirdPartyDataSync) processAndSaveHeroData(ctx context.Context, data []
continue
}
g.Log().Debug(ctx, "插入新英雄:", hero.Code)
// 新增后立即刷新该图片的OSS预签名URL到缓存
if customImgUrl != "" {
key := ossObjectKey
err := util.RefreshOssPresignedUrlCache(ctx, []string{key}, 24*time.Hour)
if err != nil {
g.Log().Error(ctx, "刷新新英雄图片预签名URL失败:", key, err)
}
}
}
return nil

View File

@@ -27,6 +27,7 @@ func TestMain(m *testing.M) {
* 测试同步英雄数据
*/
func TestSyncHeroData(t *testing.T) {
_ = i18n.RefreshI18n(context.Background())
thirdPartyDataSync := NewThirdPartyDataSync()
err := thirdPartyDataSync.SyncHeroData(context.Background())