Commit 27d17e8a authored by Bess严根旺's avatar Bess严根旺

回调请求

parent 9b790c8b
......@@ -7,6 +7,7 @@ import (
"doc-service/domain/entity/serversitemicros/docservercallback"
"doc-service/domain/entity/serversitemicros/docserverproject"
"doc-service/infra/db"
"encoding/json"
"errors"
"fmt"
"github.com/nacos-group/nacos-sdk-go/v2/common/logger"
......@@ -95,6 +96,17 @@ func callbackData(ctx context.Context) int8 {
return 1
}
// Data 结构体定义
type BackData struct {
ID int `json:"id"`
Msg string `json:"msg"`
Status int8 `json:"status"`
}
type CallBackData struct {
ID int
}
// 执行请求
func callBackUrl(c context.Context, projectId int32) (bool, string) {
defer recoverPanicCallBack()
......@@ -121,14 +133,34 @@ func callBackUrl(c context.Context, projectId int32) (bool, string) {
}
}
projectInfo, _ := db.GetServerSiteMicrosDB().DocServerProject.Query().Where(
docserverproject.IDEQ(projectId),
).First(c)
logger.Info(projectInfo)
if res {
//回调
if callInfo.CallbackType == 0 {
body := []byte(callInfo.CallbackData)
// 创建一个 Data 结构体实例
data := &BackData{}
id := 0
if callInfo.CallbackData != "" {
backData := CallBackData{}
body := []byte(callInfo.CallbackData)
_ = json.Unmarshal(body, &backData)
id = backData.ID
}
data.ID = id
data.Msg = projectInfo.CallbackRes
data.Status = 0
// 将结构体转换为 JSON 格式
jsonData, _ := json.Marshal(data)
headers := map[string]string{
"Content-Type": "application/json",
}
code, respBody, errs := sendPostRequest(callInfo.CallbackURL, body, headers)
code, respBody, errs := sendPostRequest(callInfo.CallbackURL, []byte(jsonData), headers)
if code != 200 || errs != nil {
msg = fmt.Sprintf("post回调 : code = %d , 原因:%s", code, errs.Error())
res = false
......@@ -142,7 +174,6 @@ func callBackUrl(c context.Context, projectId int32) (bool, string) {
}
logger.Info("Response Body:", string(respBody))
}
}
return res, msg
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment