fix logic

This commit is contained in:
liangzai 2025-06-14 13:03:41 +08:00
parent 5edb069bd5
commit bdb1d7b8e1

View File

@ -36,7 +36,7 @@ type APIRequest struct {
// WestAPIResponse 定义西部API响应结构
type APIResponse struct {
SourceId string
Resp []byte
Resp json.RawMessage
Success bool
}
@ -159,9 +159,21 @@ func (l *COMB298YLogic) COMB298Y(req *types.Request) (resp string, err *errs.App
respData, err := l.svcCtx.WestDexService.CallAPI(req.SourceId, req.Request, l.svcCtx.Config.WestConfig.SecretId)
if err != nil {
callAPIErr = err
westResp = "{}" // 发生错误时返回空对象
} else {
// 确保返回的是有效的 JSON
if len(respData) == 0 {
westResp = "{}"
} else {
// 验证 JSON 是否有效
var jsonCheck interface{}
if json.Unmarshal(respData, &jsonCheck) != nil {
westResp = "{}"
} else {
westResp = string(respData)
}
}
}
case "yushan":
respData, err := l.svcCtx.YushanService.Request(req.SourceId, req.Request)
if err != nil {
@ -170,11 +182,24 @@ func (l *COMB298YLogic) COMB298Y(req *types.Request) (resp string, err *errs.App
} else {
callAPIErr = errs.ErrSystem
}
westResp = "{}" // 发生错误时返回空对象
} else {
// 确保返回的是有效的 JSON
if len(respData) == 0 {
westResp = "{}"
} else {
// 验证 JSON 是否有效
var jsonCheck interface{}
if json.Unmarshal(respData, &jsonCheck) != nil {
westResp = "{}"
} else {
westResp = string(respData)
}
}
}
default:
success = false
westResp = "{}"
logx.Errorf("未知的服务类型:%s", req.Service)
}
@ -182,9 +207,15 @@ func (l *COMB298YLogic) COMB298Y(req *types.Request) (resp string, err *errs.App
success = false
}
// 确保响应是有效的 JSON
var jsonResp json.RawMessage
if err := json.Unmarshal([]byte(westResp), &jsonResp); err != nil {
jsonResp = json.RawMessage("{}")
}
responseChan <- APIResponse{
SourceId: req.ServiceId,
Resp: []byte(westResp),
Resp: jsonResp,
Success: success,
}
}(apiReq)