From 636dc9f96c6d027110346a3ac790e15727919476 Mon Sep 17 00:00:00 2001 From: liangzai <2440983361@qq.com> Date: Mon, 9 Feb 2026 14:48:13 +0800 Subject: [PATCH] f --- .../internal/logic/query/queryexamplelogic.go | 54 ++++++++++++++++--- 1 file changed, 46 insertions(+), 8 deletions(-) diff --git a/app/main/api/internal/logic/query/queryexamplelogic.go b/app/main/api/internal/logic/query/queryexamplelogic.go index 902fb9b..a5e00e9 100644 --- a/app/main/api/internal/logic/query/queryexamplelogic.go +++ b/app/main/api/internal/logic/query/queryexamplelogic.go @@ -1,6 +1,7 @@ package query import ( + "bytes" "context" "encoding/hex" "encoding/json" @@ -16,7 +17,7 @@ import ( "github.com/zeromicro/go-zero/core/logx" ) -// comboExampleDataItem 组合包示例 Content 解密后的单条结构,与 APIResponseData 一致,便于按模块展开 +// comboExampleDataItem 组合包示例 Content 解密后的单条结构(已转换格式),与 APIResponseData 一致 type comboExampleDataItem struct { ApiID string `json:"apiID"` Data json.RawMessage `json:"data"` @@ -24,6 +25,15 @@ type comboExampleDataItem struct { Timestamp string `json:"timestamp"` } +// comboExampleRawResponse 组合包数据源原始返回格式:{ "responses": [ { "api_code", "success", "data" } ] } +type comboExampleRawResponse struct { + Responses []struct { + ApiCode string `json:"api_code"` + Success bool `json:"success"` + Data json.RawMessage `json:"data"` + } `json:"responses"` +} + type QueryExampleLogic struct { logx.Logger ctx context.Context @@ -89,7 +99,9 @@ func (l *QueryExampleLogic) QueryExample(req *types.QueryExampleReq) (resp strin continue } - // 组合包(api_id 前四位为 COMB):示例 Content 为多条 APIResponseData 的 JSON 数组,按子模块展开为多个 Tab + // 组合包(api_id 前四位为 COMB):示例 Content 支持两种格式,按子模块展开为多个 Tab + // 格式1:[]comboExampleDataItem 即 [ { "apiID", "data", "success", "timestamp" }, ... ] + // 格式2:数据源原始格式 { "responses": [ { "api_code", "success", "data" }, ... ] } if strings.HasPrefix(feature.ApiId, "COMB") { if example.Content == "000" { continue @@ -98,26 +110,52 @@ func (l *QueryExampleLogic) QueryExample(req *types.QueryExampleReq) (resp strin if decryptErr != nil { return "", errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "示例报告, 组合包解密失败: %v", decryptErr) } - var comboItems []comboExampleDataItem - if err := sonic.Unmarshal([]byte(decryptedData), &comboItems); err != nil { - return "", errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "示例报告, 组合包示例解析失败: %v", err) + decryptedBytes := decryptedData + type expandItem struct { + ApiID string + Data json.RawMessage + Success bool + Timestamp string } - for i, item := range comboItems { + var itemsToExpand []expandItem + trimmed := bytes.TrimSpace(decryptedBytes) + if len(trimmed) > 0 && trimmed[0] == '[' { + var comboItems []comboExampleDataItem + if err := sonic.Unmarshal(decryptedBytes, &comboItems); err != nil { + return "", errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "示例报告, 组合包示例解析失败: %v", err) + } + for _, item := range comboItems { + itemsToExpand = append(itemsToExpand, expandItem{item.ApiID, item.Data, item.Success, item.Timestamp}) + } + } else { + var raw comboExampleRawResponse + if err := sonic.Unmarshal(decryptedBytes, &raw); err != nil { + return "", errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "示例报告, 组合包示例解析失败(非数组且非responses格式): %v", err) + } + for _, r := range raw.Responses { + itemsToExpand = append(itemsToExpand, expandItem{r.ApiCode, r.Data, r.Success, ""}) + } + } + for i, item := range itemsToExpand { subFeature, subErr := l.svcCtx.FeatureModel.FindOneByApiId(l.ctx, item.ApiID) featureName := item.ApiID if subErr == nil && subFeature != nil { featureName = subFeature.Name } + ts := item.Timestamp + if ts == "" { + ts = "2020-01-01 00:00:00" + } query.QueryData = append(query.QueryData, types.QueryItem{ Feature: map[string]interface{}{ "featureName": featureName, - "sort": pf.Sort*1000 + int64(i), // 保持组合包整体顺序,子项按索引排 + "sort": pf.Sort*1000 + int64(i), }, Data: map[string]interface{}{ "apiID": item.ApiID, "data": item.Data, "success": item.Success, - "timestamp": item.Timestamp, + "timestamp": ts, }, }) }