diff --git a/api/desc/ai/pcm-ai.api b/api/desc/ai/pcm-ai.api index fefc7ea3..8d2ccfe3 100644 --- a/api/desc/ai/pcm-ai.api +++ b/api/desc/ai/pcm-ai.api @@ -1822,8 +1822,11 @@ service AICore-api { type ( ChatReq{ - ApiUrl string `json:"apiUrl,optional"` + ApiUrl string `json:"apiUrl"` Method string `json:"method,optional"` ReqData map[string]interface{} `json:"reqData"` } + ChatResult{ + Resuluts string `json:"results,optional"` + } ) \ No newline at end of file diff --git a/api/internal/handler/ai/proxyapihandler.go b/api/internal/handler/ai/proxyapihandler.go index cbb732e2..b86ce9d2 100644 --- a/api/internal/handler/ai/proxyapihandler.go +++ b/api/internal/handler/ai/proxyapihandler.go @@ -18,7 +18,7 @@ func ProxyApiHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { } l := ai.NewProxyApiLogic(r.Context(), svcCtx) - resp, err := l.ProxyApi(&req, w) + resp, err := l.ProxyApi(&req) result.HttpResult(r, w, resp, err) } } diff --git a/api/internal/logic/ai/proxyapilogic.go b/api/internal/logic/ai/proxyapilogic.go index 1e7235b0..adec665f 100644 --- a/api/internal/logic/ai/proxyapilogic.go +++ b/api/internal/logic/ai/proxyapilogic.go @@ -5,8 +5,8 @@ import ( "context" "crypto/tls" "encoding/json" - "fmt" - tool "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/utils" + "github.com/go-resty/resty/v2" + "github.com/pkg/errors" "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/utils/hws" "net/http" @@ -30,21 +30,28 @@ func NewProxyApiLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ProxyApi } } -type ChatResult struct { - Results string `json:"results"` -} +const ( + XProjectID = "d18190e28e3f45a281ef0b0696ec9d52" + XStage = "RELEASE" + ContentType = "application/json" +) -type ResponseData struct { - Results string `json:"results"` -} - -func (l *ProxyApiLogic) ProxyApi(req *types.ChatReq, w http.ResponseWriter) (resp *types.CommonResp, err error) { +func (l *ProxyApiLogic) ProxyApi(req *types.ChatReq) (resp *types.ChatResult, err error) { + logx.Infof("【开始处理请求,目标URL: %s】", req.ApiUrl) jsonBytes, err := json.Marshal(&req.ReqData) - // 调用第三方接口的 POST 方法 - thirdReq, err := http.NewRequest("POST", req.ApiUrl, bytes.NewBuffer(jsonBytes)) if err != nil { - return + logx.Errorf("【序列化请求数据失败: %v】", err) + return nil, errors.New("请求数据序列化失败") + } + + resp = &types.ChatResult{} + + // 构建 HTTP 请求 + request, err := http.NewRequest("POST", req.ApiUrl, bytes.NewBuffer(jsonBytes)) + if err != nil { + logx.Errorf("【构建 HTTP 请求失败: %v】", err) + return nil, errors.New("网络错误,请稍后重试") } signer := &hws.Signer{ @@ -52,38 +59,33 @@ func (l *ProxyApiLogic) ProxyApi(req *types.ChatReq, w http.ResponseWriter) (res Secret: "JWXCE9qcYbc7RjpSRIWt4WgG3ZKF6Q4lPzkJReX9", } - if err := signer.Sign(thirdReq); err != nil { - return nil, err + if err := signer.Sign(request); err != nil { + logx.Errorf("【接口签名错误: %v】", err) + return nil, errors.New("网络错误,请稍后重试") } - // 设置client信任所有证书 - tr := &http.Transport{ - TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, - } - client := &http.Client{ - Transport: tr, + client := resty.New().SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true}) + + response, err := client.R(). + SetHeader("X-Project-Id", XProjectID). + SetHeader("x-stage", XStage). + SetHeader("Content-Type", ContentType). + SetHeader("Authorization", request.Header.Get(hws.HeaderXAuthorization)). + SetHeader("X-Sdk-Date", request.Header.Get(hws.HeaderXDateTime)). + SetBody(jsonBytes). + SetResult(&resp). + Post(req.ApiUrl) + + if err != nil { + logx.Errorf("【远程调用接口URL:%s, 返回错误: %s】", req.ApiUrl, err.Error()) + return nil, errors.New("网络错误,请稍后重试") } - thirdReq.Header.Set("X-Project-Id", "d18190e28e3f45a281ef0b0696ec9d52") - thirdReq.Header.Set("x-stage", "RELEASE") - thirdReq.Header.Set("Authorization", thirdReq.Header.Get(hws.HeaderXAuthorization)) - thirdReq.Header.Set("X-Sdk-Date", thirdReq.Header.Get(hws.HeaderXDateTime)) - thirdReq.Header.Set("Content-Type", "application/json") - - thirdResp, err := client.Do(thirdReq) - - defer thirdReq.Body.Close() - var responseData ResponseData - decoder := json.NewDecoder(thirdResp.Body) - if err := decoder.Decode(&responseData); err != nil { - fmt.Println("Error decoding response:", err) + if response.StatusCode() != 200 { + logx.Errorf("【远程调用接口URL:%s, 返回错误: %s】", req.ApiUrl, response.Body()) + return nil, errors.New("网络错误,请稍后重试") } - chatResult := &ChatResult{} - tool.Convert(responseData, &chatResult) - return &types.CommonResp{ - Code: thirdResp.StatusCode, - Msg: "success", - Data: chatResult, - }, nil + logx.Infof("【请求处理成功,目标URL: %s】", req.ApiUrl) + return resp, nil } diff --git a/api/internal/types/types.go b/api/internal/types/types.go index ac7263a2..fb682586 100644 --- a/api/internal/types/types.go +++ b/api/internal/types/types.go @@ -2877,11 +2877,15 @@ type AiTask struct { } type ChatReq struct { - ApiUrl string `json:"apiUrl,optional"` + ApiUrl string `json:"apiUrl"` Method string `json:"method,optional"` ReqData map[string]interface{} `json:"reqData"` } +type ChatResult struct { + Resuluts string `json:"results,optional"` +} + type StorageScreenReq struct { }