forked from JointCloud/pcm-coordinator
90 lines
2.2 KiB
Go
90 lines
2.2 KiB
Go
package ai
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"crypto/tls"
|
|
"encoding/json"
|
|
"fmt"
|
|
tool "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/utils"
|
|
"gitlink.org.cn/JointCloud/pcm-coordinator/pkg/utils/hws"
|
|
"net/http"
|
|
|
|
"gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/svc"
|
|
"gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/types"
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
)
|
|
|
|
type ProxyApiLogic struct {
|
|
logx.Logger
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
}
|
|
|
|
func NewProxyApiLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ProxyApiLogic {
|
|
return &ProxyApiLogic{
|
|
Logger: logx.WithContext(ctx),
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
}
|
|
}
|
|
|
|
type ChatResult struct {
|
|
Results string `json:"results"`
|
|
}
|
|
|
|
type ResponseData struct {
|
|
Results string `json:"results"`
|
|
}
|
|
|
|
func (l *ProxyApiLogic) ProxyApi(req *types.ChatReq, w http.ResponseWriter) (resp *types.CommonResp, err error) {
|
|
|
|
jsonBytes, err := json.Marshal(&req.ReqData)
|
|
// 调用第三方接口的 POST 方法
|
|
thirdReq, err := http.NewRequest("POST", req.ApiUrl, bytes.NewBuffer(jsonBytes))
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
signer := &hws.Signer{
|
|
Key: "UNEHPHO4Z7YSNPKRXFE4",
|
|
Secret: "JWXCE9qcYbc7RjpSRIWt4WgG3ZKF6Q4lPzkJReX9",
|
|
}
|
|
|
|
if err := signer.Sign(thirdReq); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// 设置client信任所有证书
|
|
tr := &http.Transport{
|
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
|
}
|
|
client := &http.Client{
|
|
Transport: tr,
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
chatResult := &ChatResult{}
|
|
tool.Convert(responseData, &chatResult)
|
|
return &types.CommonResp{
|
|
Code: thirdResp.StatusCode,
|
|
Msg: "success",
|
|
Data: chatResult,
|
|
}, nil
|
|
}
|