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 }