f
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package query
|
package query
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
@@ -16,7 +17,7 @@ import (
|
|||||||
"github.com/zeromicro/go-zero/core/logx"
|
"github.com/zeromicro/go-zero/core/logx"
|
||||||
)
|
)
|
||||||
|
|
||||||
// comboExampleDataItem 组合包示例 Content 解密后的单条结构,与 APIResponseData 一致,便于按模块展开
|
// comboExampleDataItem 组合包示例 Content 解密后的单条结构(已转换格式),与 APIResponseData 一致
|
||||||
type comboExampleDataItem struct {
|
type comboExampleDataItem struct {
|
||||||
ApiID string `json:"apiID"`
|
ApiID string `json:"apiID"`
|
||||||
Data json.RawMessage `json:"data"`
|
Data json.RawMessage `json:"data"`
|
||||||
@@ -24,6 +25,15 @@ type comboExampleDataItem struct {
|
|||||||
Timestamp string `json:"timestamp"`
|
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 {
|
type QueryExampleLogic struct {
|
||||||
logx.Logger
|
logx.Logger
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
@@ -89,7 +99,9 @@ func (l *QueryExampleLogic) QueryExample(req *types.QueryExampleReq) (resp strin
|
|||||||
continue
|
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 strings.HasPrefix(feature.ApiId, "COMB") {
|
||||||
if example.Content == "000" {
|
if example.Content == "000" {
|
||||||
continue
|
continue
|
||||||
@@ -98,26 +110,52 @@ func (l *QueryExampleLogic) QueryExample(req *types.QueryExampleReq) (resp strin
|
|||||||
if decryptErr != nil {
|
if decryptErr != nil {
|
||||||
return "", errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "示例报告, 组合包解密失败: %v", decryptErr)
|
return "", errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "示例报告, 组合包解密失败: %v", decryptErr)
|
||||||
}
|
}
|
||||||
var comboItems []comboExampleDataItem
|
decryptedBytes := decryptedData
|
||||||
if err := sonic.Unmarshal([]byte(decryptedData), &comboItems); err != nil {
|
type expandItem struct {
|
||||||
return "", errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "示例报告, 组合包示例解析失败: %v", err)
|
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)
|
subFeature, subErr := l.svcCtx.FeatureModel.FindOneByApiId(l.ctx, item.ApiID)
|
||||||
featureName := item.ApiID
|
featureName := item.ApiID
|
||||||
if subErr == nil && subFeature != nil {
|
if subErr == nil && subFeature != nil {
|
||||||
featureName = subFeature.Name
|
featureName = subFeature.Name
|
||||||
}
|
}
|
||||||
|
ts := item.Timestamp
|
||||||
|
if ts == "" {
|
||||||
|
ts = "2020-01-01 00:00:00"
|
||||||
|
}
|
||||||
query.QueryData = append(query.QueryData, types.QueryItem{
|
query.QueryData = append(query.QueryData, types.QueryItem{
|
||||||
Feature: map[string]interface{}{
|
Feature: map[string]interface{}{
|
||||||
"featureName": featureName,
|
"featureName": featureName,
|
||||||
"sort": pf.Sort*1000 + int64(i), // 保持组合包整体顺序,子项按索引排
|
"sort": pf.Sort*1000 + int64(i),
|
||||||
},
|
},
|
||||||
Data: map[string]interface{}{
|
Data: map[string]interface{}{
|
||||||
"apiID": item.ApiID,
|
"apiID": item.ApiID,
|
||||||
"data": item.Data,
|
"data": item.Data,
|
||||||
"success": item.Success,
|
"success": item.Success,
|
||||||
"timestamp": item.Timestamp,
|
"timestamp": ts,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user