This commit is contained in:
liangzai 2025-06-14 16:26:35 +08:00
parent 2943bf0320
commit ef286a2466

View File

@ -1,11 +1,9 @@
package COMB package COMB
import ( import (
"bytes"
"context" "context"
"encoding/hex" "encoding/hex"
"encoding/json" "encoding/json"
"strings"
"sync" "sync"
"tianyuan-api/apps/api/internal/common" "tianyuan-api/apps/api/internal/common"
@ -182,48 +180,26 @@ func (l *COMB298YLogic) COMB298Y(req *types.Request) (resp string, err *errs.App
// 4、发送请求到西部 // 4、发送请求到西部
logx.Infof("交易号:%s", transactionID) logx.Infof("交易号:%s", transactionID)
var respData []byte var respData []byte
var err error
if req.SourceId == "G05HZ01" { if req.SourceId == "G05HZ01" {
respData, err = l.svcCtx.WestDexService.CallAPISecond(req.SourceId, req.Request, l.svcCtx.Config.WestConfig.SecretSecondId) respData, callAPIErr = l.svcCtx.WestDexService.CallAPISecond(req.SourceId, req.Request, l.svcCtx.Config.WestConfig.SecretSecondId)
} else { } else {
respData, err = l.svcCtx.WestDexService.CallAPI(req.SourceId, req.Request, l.svcCtx.Config.WestConfig.SecretId) respData, callAPIErr = l.svcCtx.WestDexService.CallAPI(req.SourceId, req.Request, l.svcCtx.Config.WestConfig.SecretId)
} }
if err != nil { if callAPIErr != nil {
// 尝试解析响应数据 if callAPIErr.Code == errs.ErrDataSource.Code {
var respStr string // 数据源错误(如查询无结果)是业务正常情况,记录为 info
if len(respData) > 0 { logx.Infof("西部请求业务状态sourceId:%s, resp:%v", req.SourceId, respData)
// 先尝试解析为 JSON var jsonCheck interface{}
var prettyJSON bytes.Buffer if json.Unmarshal(respData, &jsonCheck) != nil {
if err := json.Indent(&prettyJSON, respData, "", " "); err == nil { westResp = "{}"
respStr = prettyJSON.String()
} else { } else {
// 如果不是 JSON检查是否是字符串形式的数组 westResp = string(respData)
strData := string(respData)
if strings.HasPrefix(strData, "[") && strings.HasSuffix(strData, "]") {
// 尝试解析为数组
var arr []int
if err := json.Unmarshal(respData, &arr); err == nil {
// 如果是数字数组,转换为字符串
var sb strings.Builder
for _, v := range arr {
sb.WriteByte(byte(v))
}
respStr = sb.String()
} else {
respStr = strData
}
} else {
respStr = strData
}
} }
}
logx.Errorf("西部请求失败sourceId:%s,err:%v, resp:%s", req.SourceId, err, respStr)
if appErr, ok := err.(*errs.AppError); ok {
callAPIErr = appErr
} else { } else {
callAPIErr = errs.ErrSystem // 其他业务错误
logx.Errorf("西部请求业务错误sourceId:%s,err:%v, resp:%v", req.SourceId, callAPIErr, respData)
westResp = "{}"
} }
westResp = "{}" // 发生错误时返回空对象
} else { } else {
// 确保返回的是有效的 JSON // 确保返回的是有效的 JSON
if len(respData) == 0 { if len(respData) == 0 {