From ea0804ad63e2e6f0c5f944d92ec09113e2b6a624 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Wed, 9 Aug 2023 18:09:16 +0800 Subject: [PATCH] fix --- .../internal/logic/ws_render_image_logic.go | 31 ++++++++++++++++- utils/curl/http_curl.go | 34 +++++++++++++++++++ utils/websocket_data/render_data.go | 6 ++-- 3 files changed, 67 insertions(+), 4 deletions(-) create mode 100644 utils/curl/http_curl.go diff --git a/server/websocket/internal/logic/ws_render_image_logic.go b/server/websocket/internal/logic/ws_render_image_logic.go index d210c924..65eee011 100644 --- a/server/websocket/internal/logic/ws_render_image_logic.go +++ b/server/websocket/internal/logic/ws_render_image_logic.go @@ -25,16 +25,45 @@ type renderImageControlChanItem struct { // 渲染发送到组装数据组装数据 func (w *wsConnectItem) renderImage(data []byte) { - var renderImageData websocket_data.AssembleRenderData + var renderImageData websocket_data.RenderImageReqMsg if err := json.Unmarshal(data, &renderImageData); err != nil { w.outChan <- w.respondDataFormat(constants.WEBSOCKET_ERR_DATA_FORMAT, "invalid format of websocket render image message:"+string(data)) logx.Error("invalid format of websocket render image message", err) return } logx.Info("收到请求云渲染图片数据:", renderImageData) + if renderImageData.RenderId == "" { + w.outChan <- w.respondDataFormat(constants.WEBSOCKET_ERR_DATA_FORMAT, "invalid format of websocket render image message:render_id is empty") + logx.Error("invalid format of websocket render image message:render_id is empty") + return + } + if renderImageData.RenderData.ProductId <= 0 { + w.outChan <- w.respondDataFormat(constants.WEBSOCKET_ERR_DATA_FORMAT, "invalid format of websocket render image message:product_id ") + logx.Error("invalid format of websocket render image message:product_id") + return + } + if renderImageData.RenderData.TemplateTagId <= 0 { + w.outChan <- w.respondDataFormat(constants.WEBSOCKET_ERR_DATA_FORMAT, "invalid format of websocket render image message:template_tag_id ") + logx.Error("invalid format of websocket render image message:template_tag_id") + return + } + //获取上传最近的logo + userMaterial, err := w.allModels.FsUserMaterial.FindLatestOne(w.ctx, w.userId, w.guestId) + if err != nil { + if !errors.Is(err, gorm.ErrRecordNotFound) { + w.outChan <- w.respondDataFormat(constants.WEBSOCKET_ERR_DATA_FORMAT, "failed to get user logo") + logx.Error("failed to get user logo") + return + } + //使用默认logo(写死一个默认) + renderImageData.RenderData.Logo = "https://s3.us-west-1.amazonaws.com/storage.fusenpack.com/f5ccd11365099fa47a6316b1cd639f6dd6064dcd2d37c8d2fcd0a322160b33cc" + } else { + renderImageData.RenderData.Logo = *userMaterial.ResourceUrl + } //用户id赋值 renderImageData.RenderData.UserId = w.userId renderImageData.RenderData.GuestId = w.guestId + //生成任务id taskId := hash.JsonHashKey(renderImageData.RenderData) //查询有没有缓存的资源,有就返回###################### diff --git a/utils/curl/http_curl.go b/utils/curl/http_curl.go new file mode 100644 index 00000000..8ec5a05b --- /dev/null +++ b/utils/curl/http_curl.go @@ -0,0 +1,34 @@ +package curl + +import ( + "bytes" + "errors" + "net/http" + "strings" + "time" +) + +// 接口请求 +func ApiCall(url, method string, header map[string]string, postData []byte, timeOut time.Duration) (rsp *http.Response, err error) { + method = strings.ToUpper(method) + if method != "GET" && method != "POST" && method != "PUT" && method != "DELETE" { + return nil, errors.New("invalid http method") + } + if url == "" { + return nil, errors.New("request url can`t be empty") + } + client := &http.Client{} + if timeOut <= 0 { + client.Timeout = time.Second * 15 + } else { + client.Timeout = timeOut + } + requestHandle, err := http.NewRequest(method, url, bytes.NewReader(postData)) + if err != nil { + return nil, err + } + for k, v := range header { + requestHandle.Header.Set(k, v) + } + return client.Do(requestHandle) +} diff --git a/utils/websocket_data/render_data.go b/utils/websocket_data/render_data.go index 25fc9dfc..928d1005 100644 --- a/utils/websocket_data/render_data.go +++ b/utils/websocket_data/render_data.go @@ -14,9 +14,9 @@ type RenderImageReqMsg struct { type RenderData struct { TemplateTagId int64 `json:"template_tag_id"` //模板标签id ProductId int64 `json:"product_id"` //产品id - Logo string `json:"logo"` - UserId int64 `json:"user_id"` //用户id(websocket连接建立再赋值) - GuestId int64 `json:"guest_id"` //游客id(websocket连接建立再赋值) + Logo string `json:"logo"` //log资源地址(websocket连接建立再赋值) + UserId int64 `json:"user_id"` //用户id(websocket连接建立再赋值) + GuestId int64 `json:"guest_id"` //游客id(websocket连接建立再赋值) } // websocket发送渲染完的数据