|
@@ -5,6 +5,7 @@ import (
|
|
|
"context"
|
|
|
"encoding/json"
|
|
|
"fmt"
|
|
|
+ "github.com/suyuan32/simple-admin-common/msg/errormsg"
|
|
|
"github.com/zeromicro/go-zero/core/errorx"
|
|
|
"io"
|
|
|
"net/http"
|
|
@@ -29,7 +30,7 @@ func NewGptbotsMessageLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Gp
|
|
|
svcCtx: svcCtx}
|
|
|
}
|
|
|
|
|
|
-func (l *GptbotsMessageLogic) GptbotsMessage(req *types.MessageReq) (resp *types.MessageResp, err error) {
|
|
|
+func (l *GptbotsMessageLogic) GptbotsMessage(req *types.MessageReq) (resp *types.BaseDataInfo, err error) {
|
|
|
apikey := l.svcCtx.Config.Xiaoice.GptbotsAuthorization
|
|
|
conversationId, err := l.GetConversation(apikey, strconv.FormatUint(*req.UserId, 10))
|
|
|
if conversationId == nil || err != nil {
|
|
@@ -38,7 +39,7 @@ func (l *GptbotsMessageLogic) GptbotsMessage(req *types.MessageReq) (resp *types
|
|
|
|
|
|
baseURL, err := url.Parse("https://api.gptbots.ai/v1/conversation/message")
|
|
|
if err != nil {
|
|
|
- return nil, err
|
|
|
+ return nil, errorx.NewDefaultError(fmt.Sprintf("生成内容失败: %+v", err))
|
|
|
}
|
|
|
|
|
|
// 构建请求体
|
|
@@ -49,13 +50,13 @@ func (l *GptbotsMessageLogic) GptbotsMessage(req *types.MessageReq) (resp *types
|
|
|
}
|
|
|
jsonBody, err := json.Marshal(requestBody)
|
|
|
if err != nil {
|
|
|
- return nil, err
|
|
|
+ return nil, errorx.NewDefaultError(fmt.Sprintf("生成内容失败: %+v", err))
|
|
|
}
|
|
|
|
|
|
// 创建HTTP请求
|
|
|
httpReq, err := http.NewRequest("POST", baseURL.String(), bytes.NewBuffer(jsonBody))
|
|
|
if err != nil {
|
|
|
- return nil, err
|
|
|
+ return nil, errorx.NewDefaultError(fmt.Sprintf("生成内容失败: %+v", err))
|
|
|
}
|
|
|
|
|
|
// 添加必要的Header信息
|
|
@@ -66,7 +67,7 @@ func (l *GptbotsMessageLogic) GptbotsMessage(req *types.MessageReq) (resp *types
|
|
|
client := &http.Client{}
|
|
|
response, err := client.Do(httpReq)
|
|
|
if err != nil {
|
|
|
- return nil, err
|
|
|
+ return nil, errorx.NewDefaultError(fmt.Sprintf("生成内容失败: %+v", err))
|
|
|
}
|
|
|
defer func(Body io.ReadCloser) {
|
|
|
err := Body.Close()
|
|
@@ -78,7 +79,7 @@ func (l *GptbotsMessageLogic) GptbotsMessage(req *types.MessageReq) (resp *types
|
|
|
// 读取和输出响应
|
|
|
body, err := io.ReadAll(response.Body)
|
|
|
if err != nil {
|
|
|
- return nil, err
|
|
|
+ return nil, errorx.NewDefaultError(fmt.Sprintf("生成内容失败: %+v", err))
|
|
|
}
|
|
|
|
|
|
// 检查响应状态
|
|
@@ -90,7 +91,7 @@ func (l *GptbotsMessageLogic) GptbotsMessage(req *types.MessageReq) (resp *types
|
|
|
// 解析 JSON 响应
|
|
|
var responseMap types.GptbotsMessageResp
|
|
|
if err := json.Unmarshal(body, &responseMap); err != nil {
|
|
|
- return nil, err
|
|
|
+ return nil, errorx.NewDefaultError(fmt.Sprintf("生成内容失败: %+v", err))
|
|
|
}
|
|
|
|
|
|
data := ""
|
|
@@ -98,7 +99,7 @@ func (l *GptbotsMessageLogic) GptbotsMessage(req *types.MessageReq) (resp *types
|
|
|
data = responseMap.FlowOutput[0].Content
|
|
|
}
|
|
|
|
|
|
- return &types.MessageResp{Data: &data}, nil
|
|
|
+ return &types.BaseDataInfo{Msg: errormsg.Success, Data: data}, nil
|
|
|
}
|
|
|
|
|
|
func (l *GptbotsMessageLogic) GetConversation(apikey string, userId string) (conversationId *string, err error) {
|