diff --git a/server/render/consumer/assemble_render_data.go b/server/render/consumer/assemble_render_data.go index 5463ef4a..a3fc6ef0 100644 --- a/server/render/consumer/assemble_render_data.go +++ b/server/render/consumer/assemble_render_data.go @@ -7,22 +7,23 @@ import ( "fmt" "fusenapi/constants" "fusenapi/initalize" - "fusenapi/model/gmodel" + "fusenapi/server/render/internal/svc" + "fusenapi/utils/curl" + "fusenapi/utils/file" + "fusenapi/utils/hash" "fusenapi/utils/websocket_data" "github.com/zeromicro/go-zero/core/logx" "gorm.io/gorm" + "io/ioutil" "strconv" + "strings" ) // 这里请求的py接口返回数据 type pythonApiRsp struct { - Code int `json:"code"` - Msg string `json:"msg"` - Data []struct { - Tid int64 `json:"tid"` - Imgurl string `json:"imgurl"` - Costtime int64 `json:"costtime"` - } `json:"data"` + Id string `json:"id"` //物料模板的id + LogoUrl string `json:"logo_url"` //logo地址 + result string `json:"result"` //图片base64 } // 消费渲染需要组装的数据 @@ -36,17 +37,17 @@ func (m *MqConsumerRenderAssemble) Run(ctx context.Context, data []byte) error { logx.Error("MqConsumerRenderAssemble数据格式错误:", err) return nil //不返回错误就删除消息 } - val := ctx.Value("allmodels") + val := ctx.Value("svcctx") if val == nil { - return errors.New("allmodels is nil") + return errors.New("svcctx is nil") } - allmodels, ok := val.(*gmodel.AllModelsGen) + svcCtx, ok := val.(*svc.ServiceContext) if !ok { - return errors.New("allmodels is nil!!") + return errors.New("svcctx is nil!!") } rabbitmq := initalize.RabbitMqHandle{} //获取模板(产品第一个sku的模板) - templateInfo, err := allmodels.FsProductTemplateV2.FindOneByProductIdTagIdWithSizeTable(ctx, parseInfo.RenderData.ProductId, fmt.Sprintf("%d", parseInfo.RenderData.TemplateTagId)) + templateInfo, err := svcCtx.AllModels.FsProductTemplateV2.FindOneByProductIdTagIdWithSizeTable(ctx, parseInfo.RenderData.ProductId, fmt.Sprintf("%d", parseInfo.RenderData.TemplateTagId)) if err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { logx.Error("template info is not found") @@ -55,13 +56,25 @@ func (m *MqConsumerRenderAssemble) Run(ctx context.Context, data []byte) error { logx.Error("failed to get template info:", err) return err } - // todo 获取sku对应用来合成刀版图的json数据 - - // todo curl请求python获取刀版图 baseImage###### - - baseImage := "" + combineHash := hash.JsonHashKey(parseInfo) //区别于云渲染的taskid,这个用获取刀版图缓存 + //获取该hash值下有没有对应的资源 + resource, err := svcCtx.AllModels.FsResource.FindOneById(ctx, combineHash) + if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) { + logx.Error("failed to get resource :", err) + return err + } + combineImage := "" //刀版图 + //如果不存在,则请求生成刀版图 + if errors.Is(err, gorm.ErrRecordNotFound) { + combineImage, err = getCombineImage(ctx, svcCtx, parseInfo, combineHash) + if err != nil { + return err + } + } else { + combineImage = *resource.ResourceUrl + } //获取渲染设置信息 - element, err := allmodels.FsProductTemplateElement.FindOneByModelId(ctx, *templateInfo.ModelId) + element, err := svcCtx.AllModels.FsProductTemplateElement.FindOneByModelId(ctx, *templateInfo.ModelId) if err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { logx.Error("element info is not found,model_id = ?", *templateInfo.ModelId) @@ -87,7 +100,7 @@ func (m *MqConsumerRenderAssemble) Run(ctx context.Context, data []byte) error { if element.Base != nil && *element.Base != "" { tempData = append(tempData, map[string]interface{}{ "name": "model", - "data": "0," + baseImage + "," + *element.Base, + "data": "0," + combineImage + "," + *element.Base, "type": "other", "layer": "0", "is_update": 1, @@ -145,3 +158,48 @@ func (m *MqConsumerRenderAssemble) Run(ctx context.Context, data []byte) error { logx.Info("发送渲染组装数据到unity成功") return nil } + +// 获取刀版图 +func getCombineImage(ctx context.Context, svcCtx *svc.ServiceContext, parseInfo websocket_data.AssembleRenderData, combineHash string) (image string, err error) { + // todo 获取sku对应用来合成刀版图的json数据 + + url := "http://192.168.1.7:45678/LogoCombine" + header := make(map[string]string) + header["content-type"] = "application/json" + postData := "" // todo 请求数据要查出来 + httpRsp, err := curl.ApiCall(url, "POST", header, strings.NewReader(postData), 20) + if err != nil { + logx.Error("failed to combine logo:", err) + return "", err + } + defer httpRsp.Body.Close() + bytes, err := ioutil.ReadAll(httpRsp.Body) + if err != nil { + logx.Error("failed to read python api rsp body:", err) + return "", err + } + var pythonApiInfo pythonApiRsp + if err = json.Unmarshal(bytes, &pythonApiInfo); err != nil { + logx.Error("failed to parse python api rsp:", err) + return "", err + } + //上传刀版图 + var upload = file.Upload{ + Ctx: ctx, + MysqlConn: svcCtx.MysqlConn, + AwsSession: svcCtx.AwsSession, + } + uploadRes, err := upload.UploadFileByBase64(&file.UploadBaseReq{ + FileHash: combineHash, + FileData: pythonApiInfo.result, + UploadBucket: 1, + ApiType: 2, + UserId: parseInfo.RenderData.UserId, + GuestId: parseInfo.RenderData.GuestId, + }) + if err != nil { + logx.Error("上传刀版图到s3失败:", err) + return "", err + } + return uploadRes.ResourceUrl, nil +} diff --git a/server/render/render.go b/server/render/render.go index acb9d019..f3cdafef 100644 --- a/server/render/render.go +++ b/server/render/render.go @@ -35,7 +35,7 @@ func main() { //消费渲染前组装数据队列 ctx1 := context.Background() ctx2, cancel := context.WithCancel(ctx1) - ctx2 = context.WithValue(ctx2, "allmodels", ctx.AllModels) + ctx2 = context.WithValue(ctx2, "svcctx", ctx) defer cancel() go ctx.RabbitMq.Consume(ctx2, constants.RABBIT_MQ_ASSEMBLE_RENDER_DATA, &consumer.MqConsumerRenderAssemble{}) handler.RegisterHandlers(server, ctx) diff --git a/utils/curl/http_curl.go b/utils/curl/http_curl.go index 8ec5a05b..0fbef756 100644 --- a/utils/curl/http_curl.go +++ b/utils/curl/http_curl.go @@ -1,15 +1,15 @@ package curl import ( - "bytes" "errors" + "io" "net/http" "strings" "time" ) // 接口请求 -func ApiCall(url, method string, header map[string]string, postData []byte, timeOut time.Duration) (rsp *http.Response, err error) { +func ApiCall(url, method string, header map[string]string, body io.Reader, 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") @@ -23,7 +23,7 @@ func ApiCall(url, method string, header map[string]string, postData []byte, time } else { client.Timeout = timeOut } - requestHandle, err := http.NewRequest(method, url, bytes.NewReader(postData)) + requestHandle, err := http.NewRequest(method, url, body) if err != nil { return nil, err }