ci(drone): 添加 Go 模块和构建缓存

- 在 restore cache 和 rebuild cache 步骤中添加了 go-mod-cache 和 go
This commit is contained in:
hxt
2025-07-10 21:16:25 +08:00
parent d36a8bec21
commit 9293db3809
5 changed files with 103 additions and 141 deletions

View File

@@ -2,6 +2,7 @@ package cron
import (
"context"
"epic/internal/consts"
"epic/internal/model/dto"
"epic/utility"
"fmt"
@@ -30,7 +31,7 @@ func (t *ThirdPartyDataSync) SyncHeroData(ctx context.Context) error {
// 示例从第三方API获取英雄数据
heroData, err := t.fetchHeroDataFromAPI(ctx)
if err != nil {
if err != nil || heroData == nil {
g.Log().Error(ctx, "获取英雄数据失败:", err)
return err
}
@@ -71,7 +72,7 @@ func (t *ThirdPartyDataSync) SyncArtifactData(ctx context.Context) error {
// fetchHeroDataFromAPI 从API获取英雄数据
func (t *ThirdPartyDataSync) fetchHeroDataFromAPI(ctx context.Context) ([]byte, error) {
// 示例API地址实际使用时需要替换为真实的API
apiURL := "https://api.example.com/heroes"
apiURL := consts.HeroListURL
// 添加请求头
headers := map[string]string{
@@ -128,23 +129,24 @@ func (t *ThirdPartyDataSync) fetchArtifactDataFromAPI(ctx context.Context) (stri
func (t *ThirdPartyDataSync) processAndSaveHeroData(ctx context.Context, data []byte) error {
// 使用 gjson 解析
j := gjson.New(data)
// 检查json对象本身和其内部值并使用 .Var().IsSlice() 这种更可靠的方式判断是否为数组
if j == nil || j.IsNil() || !j.Var().IsSlice() {
return fmt.Errorf("英雄数据格式错误期望是一个JSON数组")
if j == nil || j.IsNil() {
return fmt.Errorf("英雄数据格式错误期望是一个JSON对象")
}
var heroes []*dto.ThirdPartyHeroDTO
// 先解析为 map[string]*ThirdPartyHeroDTO
var heroes map[string]*dto.ThirdPartyHeroDTO
if err := j.Scan(&heroes); err != nil {
return fmt.Errorf("解析英雄数据到DTO失败: %v", err)
}
g.Log().Info(ctx, "解析到", len(heroes), "个英雄数据")
// 批量处理数据
for _, hero := range heroes {
// 遍历 map设置 Name 字段,并保存
for name, hero := range heroes {
hero.Name = name // 将 map 的 key 作为 Name 字段
if err := t.saveHeroData(ctx, hero); err != nil {
g.Log().Error(ctx, "保存英雄数据失败:", err)
// 继续处理其他数据,不中断整个流程
continue
}
}