From bdb1d7b8e113fe8fc8a3122810cc6fb85d2a4d30 Mon Sep 17 00:00:00 2001 From: liangzai <2440983361@qq.com> Date: Sat, 14 Jun 2025 13:03:41 +0800 Subject: [PATCH] fix logic --- apps/api/internal/logic/COMB/comb298ylogic.go | 39 +++++++++++++++++-- 1 file changed, 35 insertions(+), 4 deletions(-) diff --git a/apps/api/internal/logic/COMB/comb298ylogic.go b/apps/api/internal/logic/COMB/comb298ylogic.go index 2f385b1..7e4032a 100644 --- a/apps/api/internal/logic/COMB/comb298ylogic.go +++ b/apps/api/internal/logic/COMB/comb298ylogic.go @@ -36,7 +36,7 @@ type APIRequest struct { // WestAPIResponse 定义西部API响应结构 type APIResponse struct { SourceId string - Resp []byte + Resp json.RawMessage Success bool } @@ -159,8 +159,20 @@ 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 { - westResp = string(respData) + // 确保返回的是有效的 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) @@ -170,11 +182,24 @@ func (l *COMB298YLogic) COMB298Y(req *types.Request) (resp string, err *errs.App } else { callAPIErr = errs.ErrSystem } + westResp = "{}" // 发生错误时返回空对象 } else { - westResp = string(respData) + // 确保返回的是有效的 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)