From 9ef6ac9cdb39124890f204845210087ca328e351 Mon Sep 17 00:00:00 2001 From: hu xiaotong <416314413@163.com> Date: Thu, 17 Jul 2025 17:37:13 +0800 Subject: [PATCH] =?UTF-8?q?refactor(internal):=20=E4=BC=98=E5=8C=96=20OSS?= =?UTF-8?q?=20=E9=A2=84=E7=AD=BE=E5=90=8D=20URL=20=E7=BC=93=E5=AD=98?= =?UTF-8?q?=E5=88=B7=E6=96=B0=E4=BB=BB=E5=8A=A1=E5=92=8C=E8=8B=B1=E9=9B=84?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E7=BC=93=E5=AD=98=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 注释掉 OSS预签名 URL 缓存刷新任务的定时执行代码 - 在 hero/hero.go 中增加对 Redis缓存和英雄数据集的非空校验 - 修改 OSS预签名 URL 生成逻辑,自动替换为 CDN 域名 --- internal/logic/cron/third_party_sync.go | 31 +++++++++++++++++++++++++ internal/logic/hero/hero.go | 21 ++++++++++++++--- internal/logic/i18n/i18n.go | 18 +++++++++++--- 3 files changed, 64 insertions(+), 6 deletions(-) diff --git a/internal/logic/cron/third_party_sync.go b/internal/logic/cron/third_party_sync.go index d969530..9416d4b 100644 --- a/internal/logic/cron/third_party_sync.go +++ b/internal/logic/cron/third_party_sync.go @@ -103,6 +103,37 @@ func (t *ThirdPartyDataSync) fetchHeroDataFromAPI(ctx context.Context) ([]byte, return content, nil } +// fetchHeroBuildsFromAPI 通过角色英文名POST请求获取配装数据 +func (t *ThirdPartyDataSync) FetchHeroBuildsFromAPI(ctx context.Context, heroName string) (string, error) { + apiURL := consts.HeroNameURL + + headers := map[string]string{ + "User-Agent": "EpicGameBot/1.0", + "Accept": "application/json", + "Content-Type": "application/json", + } + + // 直接将角色名作为body + //bodyBytes := []byte(heroName) + heroNameEN := i18n.Zh2En(heroName) + fmt.Println(heroNameEN) + + resp, err := t.client.Header(headers).Post(ctx, apiURL, "Argent Waves Hwayoung") + if err != nil { + return "", fmt.Errorf("API请求失败: %v", err) + } + defer resp.Close() + + if resp.StatusCode != 200 { + return "", fmt.Errorf("API响应错误,状态码: %d", resp.StatusCode) + } + + content := resp.ReadAll() + g.Log().Debug(ctx, "配装API响应内容长度:", len(content)) + + return string(content), nil +} + // 从API获取神器数据 func (t *ThirdPartyDataSync) fetchArtifactDataFromAPI(ctx context.Context) (string, error) { // 示例API地址 diff --git a/internal/logic/hero/hero.go b/internal/logic/hero/hero.go index 4545858..0a01996 100644 --- a/internal/logic/hero/hero.go +++ b/internal/logic/hero/hero.go @@ -4,10 +4,12 @@ import ( "context" v1 "epic/api/hero/v1" "epic/internal/dao" + "epic/internal/logic/cron" "epic/internal/model/dto" "epic/internal/model/entity" "epic/internal/service" "epic/internal/util" + "fmt" "github.com/gogf/gf/v2/encoding/gjson" "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/util/gconv" @@ -115,6 +117,7 @@ func (l *Logic) GetHeroDetailByCode(ctx context.Context, code string) (*v1.HeroD return nil, err } + thirdPartySync := cron.NewThirdPartyDataSync() // 优化:先查 RedisCache,再查数据库 cacheKey := "epic_hero_set:" + code jsonContent, err := util.RedisCache.Get(ctx, cacheKey) @@ -134,10 +137,22 @@ func (l *Logic) GetHeroDetailByCode(ctx context.Context, code string) (*v1.HeroD panic("util.RedisCache is nil") } if fribbleHeroSet == nil { - panic("fribbleHeroSet is nil") + // 新增:如果fribbleHeroSet为nil,调用第三方接口获取配装json + //thirdPartySync := cron.NewThirdPartyDataSync() + jsonStr, err := thirdPartySync.FetchHeroBuildsFromAPI(ctx, epicHeroInfo.HeroName) + if err != nil { + return nil, err + } + fmt.Println(jsonStr) + fribbleHeroSet = &entity.FribbleHeroSet{ + HeroCode: code, + JsonContent: jsonStr, + } + } + if fribbleHeroSet.JsonContent != "" { + // 写入 Redis 缓存,永久 + util.RedisCache.Set(ctx, cacheKey, fribbleHeroSet.JsonContent, 0) } - // 写入 Redis 缓存,1小时 - util.RedisCache.Set(ctx, cacheKey, fribbleHeroSet.JsonContent, 0) } // 解析 JsonContent 字段 diff --git a/internal/logic/i18n/i18n.go b/internal/logic/i18n/i18n.go index 5537554..6daa729 100644 --- a/internal/logic/i18n/i18n.go +++ b/internal/logic/i18n/i18n.go @@ -12,7 +12,7 @@ import ( "time" ) -// I18nEnToZh 英文->中文映射表(示例) +// I18nEnToZh 英文->中文映射表(仅做备份,不再作为主数据源) var I18nEnToZh = map[string]string{ "A Little Queen's Huge Crown": "小小女王的巨大王冠", "A Song for Everybody": "献给你和这个世界的歌曲", @@ -646,11 +646,15 @@ func (l *Logic) LoadFromDB(ctx context.Context) error { // 重新构建缓存 l.cache = make(map[string]map[string]string) + // 新增:同步数据库内容到I18nEnToZh备份 for _, m := range mappings { if l.cache[m.Language] == nil { l.cache[m.Language] = make(map[string]string) } l.cache[m.Language][m.KeyName] = m.Value + if m.Language == "zh" { + I18nEnToZh[m.KeyName] = m.Value + } } util.Info(ctx, "i18n缓存加载完成,共", len(mappings), "条记录") @@ -871,16 +875,24 @@ func (l *Logic) StartAutoRefresh(ctx context.Context) { }() } -// En2Zh 英文转中文(静态映射) +// En2Zh 英文转中文(静态映射,仅降级时使用) func En2Zh(s string) string { + if v, ok := GetI18nLogic().cache["zh"][s]; ok { + return v + } + // 仅降级时才用备份 if v, ok := I18nEnToZh[s]; ok { return v } return s } -// Zh2En 中文转英文(静态映射) +// Zh2En 中文转英文(静态映射,仅降级时使用) func Zh2En(s string) string { + if v, ok := GetI18nLogic().cache["en"][s]; ok { + return v + } + // 仅降级时才用备份 if v, ok := I18nZhToEn[s]; ok { return v }