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