fusenapi/server/websocket/internal/logic/ws_render_image.go

448 lines
20 KiB
Go
Raw Normal View History

2023-07-26 06:35:29 +00:00
package logic
2023-08-23 07:08:45 +00:00
//处理websocket云渲染任务数据
2023-07-26 06:35:29 +00:00
import (
2023-08-16 04:33:01 +00:00
"bytes"
2023-07-26 06:35:29 +00:00
"encoding/json"
2023-08-09 08:06:40 +00:00
"errors"
2023-08-21 08:19:30 +00:00
"fmt"
2023-07-27 09:33:50 +00:00
"fusenapi/constants"
2023-08-24 10:02:00 +00:00
"fusenapi/model/gmodel"
2023-08-16 04:16:23 +00:00
"fusenapi/service/repositories"
2023-08-16 04:33:01 +00:00
"fusenapi/utils/curl"
2023-08-07 02:48:14 +00:00
"fusenapi/utils/hash"
2023-08-24 06:24:04 +00:00
"fusenapi/utils/websocket_data"
2023-07-26 06:35:29 +00:00
"github.com/zeromicro/go-zero/core/logx"
2023-08-09 08:06:40 +00:00
"gorm.io/gorm"
2023-08-16 04:16:23 +00:00
"strconv"
2023-09-20 06:56:24 +00:00
"strings"
2023-08-16 04:33:01 +00:00
"time"
2023-07-26 06:35:29 +00:00
)
2023-09-07 07:54:24 +00:00
var (
//每个websocket渲染任务缓冲队列长度默认值
2023-09-21 11:07:09 +00:00
renderChanLen = 500
2023-09-22 03:22:32 +00:00
//每个websocket渲染并发数
2023-09-22 03:36:38 +00:00
renderChanConcurrency = 100
2023-09-07 07:54:24 +00:00
)
2023-08-30 09:40:40 +00:00
// 渲染处理器
2023-08-30 09:46:47 +00:00
type renderProcessor struct {
2023-08-30 09:40:40 +00:00
}
2023-07-26 09:06:53 +00:00
// 云渲染属性
2023-08-24 04:04:58 +00:00
type extendRenderProperty struct {
2023-09-22 02:41:38 +00:00
renderChan chan websocket_data.RenderImageReqMsg //渲染消息入口的缓冲队列
colorSelectedIndex int //选择颜色索引
templateTag string //模板标签
2023-08-21 08:19:30 +00:00
}
2023-07-26 09:06:53 +00:00
2023-08-30 10:51:24 +00:00
// 处理分发到这里的数据
2023-08-30 10:31:20 +00:00
func (r *renderProcessor) allocationMessage(w *wsConnectItem, data []byte) {
2023-09-21 09:22:22 +00:00
//logx.Info("开始处理渲染任务消息:", string(data))
2023-09-22 02:41:38 +00:00
var renderImageData websocket_data.RenderImageReqMsg
if err := json.Unmarshal(data, &renderImageData); err != nil {
w.renderErrResponse(renderImageData.RenderId, renderImageData.RenderData.TemplateTag, "", "数据格式错误", renderImageData.RenderData.ProductId, w.userId, w.guestId, 0, 0, 0, 0)
logx.Error("invalid format of websocket render image message", err)
return
}
2023-08-16 08:20:16 +00:00
select {
2023-08-30 10:31:20 +00:00
case <-w.closeChan: //已经关闭
2023-08-16 08:20:16 +00:00
return
2023-09-22 02:41:38 +00:00
case w.extendRenderProperty.renderChan <- renderImageData: //发入到缓冲队列
w.extendRenderProperty.colorSelectedIndex = renderImageData.RenderData.TemplateTagColor.SelectedColorIndex
w.extendRenderProperty.templateTag = renderImageData.RenderData.TemplateTag
2023-08-16 08:20:16 +00:00
return
}
}
2023-08-30 03:13:10 +00:00
// 消费渲染缓冲队列数据
func (w *wsConnectItem) consumeRenderImageData() {
2023-08-29 02:21:11 +00:00
defer func() {
if err := recover(); err != nil {
2023-09-22 03:22:32 +00:00
logx.Error("func consumeRenderImageData err:", err)
2023-08-29 02:21:11 +00:00
}
}()
2023-09-22 03:22:32 +00:00
//限制并发
limitChan := make(chan struct{}, renderChanConcurrency)
defer close(limitChan)
2023-08-16 08:20:16 +00:00
for {
select {
case <-w.closeChan: //已关闭
return
2023-09-22 02:41:38 +00:00
case data := <-w.extendRenderProperty.renderChan: //消费数据
2023-09-22 02:49:07 +00:00
//属性不同则不发送渲染
if data.RenderData.TemplateTag != w.extendRenderProperty.templateTag {
2023-09-22 03:37:44 +00:00
logx.Info("丢弃切换了标签之前的渲染")
2023-09-22 02:49:07 +00:00
continue
2023-09-22 02:41:38 +00:00
}
2023-09-22 02:49:07 +00:00
//属性不同则不发送渲染
if data.RenderData.TemplateTagColor.SelectedColorIndex != w.extendRenderProperty.colorSelectedIndex {
2023-09-22 03:37:44 +00:00
logx.Info("丢弃切换了颜色之前的渲染")
2023-09-22 02:49:07 +00:00
continue
}
2023-09-22 03:22:32 +00:00
limitChan <- struct{}{}
go func(d websocket_data.RenderImageReqMsg) {
defer func() {
if err := recover(); err != nil {
logx.Error("func renderImage err:", err)
}
}()
defer func() {
<-limitChan
}()
w.renderImage(d)
}(data)
2023-08-16 08:20:16 +00:00
}
}
}
2023-08-30 06:18:55 +00:00
// 执行渲染任务
2023-09-22 02:41:38 +00:00
func (w *wsConnectItem) renderImage(renderImageData websocket_data.RenderImageReqMsg) {
2023-09-19 07:09:48 +00:00
if renderImageData.RenderData.Logo == "" {
w.renderErrResponse(renderImageData.RenderId, renderImageData.RenderData.TemplateTag, "", "请传入logo", renderImageData.RenderData.ProductId, w.userId, w.guestId, 0, 0, 0, 0)
2023-09-20 06:56:24 +00:00
return
}
if !strings.Contains(renderImageData.RenderData.Logo, "storage.fusenpack.com") {
w.renderErrResponse(renderImageData.RenderId, renderImageData.RenderData.TemplateTag, "", "logo不是属于fusen的资源", renderImageData.RenderData.ProductId, w.userId, w.guestId, 0, 0, 0, 0)
2023-09-19 07:09:48 +00:00
return
}
2023-09-20 06:11:07 +00:00
lenColor := len(renderImageData.RenderData.TemplateTagColor.Colors)
2023-09-19 06:39:33 +00:00
if lenColor == 0 {
w.renderErrResponse(renderImageData.RenderId, renderImageData.RenderData.TemplateTag, "", "请传入模板标签选择的颜色", renderImageData.RenderData.ProductId, w.userId, w.guestId, 0, 0, 0, 0)
return
}
2023-09-19 08:10:17 +00:00
if renderImageData.RenderData.TemplateTagColor.SelectedColorIndex >= lenColor || renderImageData.RenderData.TemplateTagColor.SelectedColorIndex < 0 {
2023-09-19 06:39:33 +00:00
w.renderErrResponse(renderImageData.RenderId, renderImageData.RenderData.TemplateTag, "", "选择的模板标签颜色索引越界", renderImageData.RenderData.ProductId, w.userId, w.guestId, 0, 0, 0, 0)
return
}
2023-09-08 06:25:47 +00:00
//获取产品信息(部分字段)
productInfo, err := w.logic.svcCtx.AllModels.FsProduct.FindOne(w.logic.ctx, renderImageData.RenderData.ProductId, "id,is_customization")
2023-09-07 10:47:00 +00:00
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
w.renderErrResponse(renderImageData.RenderId, renderImageData.RenderData.TemplateTag, "", "该产品不存在", renderImageData.RenderData.ProductId, w.userId, w.guestId, 0, 0, 0, 0)
return
}
w.renderErrResponse(renderImageData.RenderId, renderImageData.RenderData.TemplateTag, "", "获取产品失败", renderImageData.RenderData.ProductId, w.userId, w.guestId, 0, 0, 0, 0)
logx.Error(err)
2023-09-07 10:48:48 +00:00
return
2023-09-07 10:47:00 +00:00
}
//不可定制
if *productInfo.IsCustomization == 0 {
w.renderErrResponse(renderImageData.RenderId, renderImageData.RenderData.TemplateTag, "", "该产品不可定制", renderImageData.RenderData.ProductId, w.userId, w.guestId, 0, 0, 0, 0)
2023-09-07 10:48:48 +00:00
return
2023-09-07 10:47:00 +00:00
}
2023-08-09 04:28:06 +00:00
//用户id赋值
2023-08-08 04:22:15 +00:00
renderImageData.RenderData.UserId = w.userId
2023-08-09 04:28:06 +00:00
renderImageData.RenderData.GuestId = w.guestId
2023-09-06 02:09:59 +00:00
//获取信息
2023-09-19 10:24:51 +00:00
productSize, productTemplate, model3dInfo, err := w.getProductRelateionInfo(&renderImageData)
2023-09-01 08:10:02 +00:00
if err != nil {
2023-09-06 02:09:59 +00:00
logx.Error(err)
2023-09-01 08:10:02 +00:00
return
2023-08-31 03:50:55 +00:00
}
2023-08-24 10:02:00 +00:00
//获取渲染设置信息
2023-08-29 02:31:25 +00:00
element, err := w.logic.svcCtx.AllModels.FsProductTemplateElement.FindOneByModelId(w.logic.ctx, *productTemplate.ElementModelId)
2023-08-24 10:02:00 +00:00
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
2023-08-31 03:50:55 +00:00
w.renderErrResponse(renderImageData.RenderId, renderImageData.RenderData.TemplateTag, "", "云渲染设置不存在", renderImageData.RenderData.ProductId, w.userId, w.guestId, productTemplate.Id, model3dInfo.Id, productSize.Id, *productTemplate.ElementModelId)
2023-09-07 06:06:29 +00:00
logx.Error("element info is not found,element_model_id = ", *productTemplate.ElementModelId)
2023-08-24 10:02:00 +00:00
return
}
2023-08-31 03:50:55 +00:00
w.renderErrResponse(renderImageData.RenderId, renderImageData.RenderData.TemplateTag, "", "获取云渲染设置失败", renderImageData.RenderData.ProductId, w.userId, w.guestId, productTemplate.Id, model3dInfo.Id, productSize.Id, *productTemplate.ElementModelId)
2023-08-28 10:37:58 +00:00
logx.Error("failed to get element ,", err)
2023-08-24 10:02:00 +00:00
return
}
2023-09-11 04:24:10 +00:00
//获取模板开关信息并且对于没有默认值的给赋值默认值(但凡DIY有一个是空的就要请求默认数据)
2023-09-19 07:09:48 +00:00
/*if renderImageData.RenderData.Website == "" || renderImageData.RenderData.Phone == "" || renderImageData.RenderData.Address == "" || renderImageData.RenderData.Qrcode == "" || renderImageData.RenderData.Slogan == "" {
2023-09-12 09:59:07 +00:00
templateSwitchInfo := template_switch_info.GetTemplateSwitchInfo(productTemplate.Id, productTemplate.TemplateInfo, *productTemplate.MaterialImg)
2023-09-11 06:41:35 +00:00
if renderImageData.RenderData.Website == "" && templateSwitchInfo.MaterialData.Website.IfShow {
2023-09-11 04:24:10 +00:00
renderImageData.RenderData.Website = templateSwitchInfo.MaterialData.Website.DefaultValue
}
2023-09-11 06:41:35 +00:00
if renderImageData.RenderData.Phone == "" && templateSwitchInfo.MaterialData.Phone.IfShow {
2023-09-11 04:24:10 +00:00
renderImageData.RenderData.Phone = templateSwitchInfo.MaterialData.Phone.DefaultValue
}
2023-09-11 06:41:35 +00:00
if renderImageData.RenderData.Address == "" && templateSwitchInfo.MaterialData.Address.IfShow {
2023-09-11 04:24:10 +00:00
renderImageData.RenderData.Address = templateSwitchInfo.MaterialData.Address.DefaultValue
}
2023-09-11 06:41:35 +00:00
if renderImageData.RenderData.Qrcode == "" && templateSwitchInfo.MaterialData.QRcode.IfShow {
2023-09-11 04:24:10 +00:00
renderImageData.RenderData.Qrcode = templateSwitchInfo.MaterialData.QRcode.DefaultValue
}
2023-09-11 07:15:38 +00:00
if renderImageData.RenderData.Slogan == "" && templateSwitchInfo.MaterialData.Slogan.IfShow {
renderImageData.RenderData.Slogan = templateSwitchInfo.MaterialData.Slogan.DefaultValue
}
2023-09-19 07:09:48 +00:00
}*/
2023-08-28 10:16:19 +00:00
//获取刀版图
combineReq := repositories.LogoCombineReq{
2023-09-19 08:03:11 +00:00
UserId: renderImageData.RenderData.UserId,
GuestId: renderImageData.RenderData.GuestId,
ProductTemplateV2Info: productTemplate,
ProductTemplateTagGroups: renderImageData.RenderData.TemplateTagGroups,
TemplateTag: renderImageData.RenderData.TemplateTag,
Website: renderImageData.RenderData.Website,
Slogan: renderImageData.RenderData.Slogan,
Address: renderImageData.RenderData.Address,
Phone: renderImageData.RenderData.Phone,
Qrcode: renderImageData.RenderData.Qrcode,
LogoUrl: renderImageData.RenderData.Logo,
2023-09-19 06:39:33 +00:00
TemplateTagColor: repositories.TemplateTagColor{
2023-09-20 06:11:07 +00:00
Color: renderImageData.RenderData.TemplateTagColor.Colors,
2023-09-19 08:10:17 +00:00
Index: renderImageData.RenderData.TemplateTagColor.SelectedColorIndex,
2023-09-19 06:39:33 +00:00
},
2023-08-28 10:16:19 +00:00
}
res, err := w.logic.svcCtx.Repositories.ImageHandle.LogoCombine(w.logic.ctx, &combineReq)
if err != nil {
2023-08-31 03:50:55 +00:00
w.renderErrResponse(renderImageData.RenderId, renderImageData.RenderData.TemplateTag, "", "合成刀版图失败:"+err.Error(), renderImageData.RenderData.ProductId, w.userId, w.guestId, productTemplate.Id, model3dInfo.Id, productSize.Id, *productTemplate.ElementModelId)
2023-08-28 10:16:19 +00:00
logx.Error("合成刀版图失败,合成请求数据:", combineReq, "错误信息:", err)
return
}
combineImage := "" //刀版图
if res != nil && res.ResourceUrl != nil {
combineImage = *res.ResourceUrl
} else {
2023-08-31 03:50:55 +00:00
w.renderErrResponse(renderImageData.RenderId, renderImageData.RenderData.TemplateTag, "", "合成的刀版图是空的地址", renderImageData.RenderData.ProductId, w.userId, w.guestId, productTemplate.Id, model3dInfo.Id, productSize.Id, *productTemplate.ElementModelId)
2023-08-28 10:16:19 +00:00
logx.Error("合成刀版图失败,合成的刀版图是空指针:", err)
return
}
2023-08-31 02:35:13 +00:00
//发送合图完毕阶段消息
2023-09-06 08:48:19 +00:00
w.sendCombineImageStepResponseMessage(renderImageData.RenderId, combineImage, productSize.Id, model3dInfo.Id, productTemplate.Id, res.DiffTimeLogoCombine, res.DiffTimeUploadFile)
2023-08-24 10:02:00 +00:00
//获取唯一id
2023-09-05 07:08:03 +00:00
taskId := w.genRenderTaskId(combineImage, renderImageData, model3dInfo, productTemplate, element)
2023-09-19 06:39:33 +00:00
//查询有没有缓存的资源,有就返回
2023-08-16 06:09:03 +00:00
resource, err := w.logic.svcCtx.AllModels.FsResource.FindOneById(w.logic.ctx, taskId)
2023-08-09 08:06:40 +00:00
if err != nil {
if !errors.Is(err, gorm.ErrRecordNotFound) {
2023-08-31 03:50:55 +00:00
w.renderErrResponse(renderImageData.RenderId, renderImageData.RenderData.TemplateTag, taskId, "获取unity云渲染缓存失败", renderImageData.RenderData.ProductId, w.userId, w.guestId, productTemplate.Id, model3dInfo.Id, productSize.Id, *productTemplate.ElementModelId)
2023-08-09 08:06:40 +00:00
logx.Error("failed to find render resource:", err)
return
}
} else {
//返回给客户端
2023-08-31 06:38:34 +00:00
w.sendRenderResultData(websocket_data.RenderImageRspMsg{
2023-08-29 07:13:25 +00:00
RenderId: renderImageData.RenderId,
Image: *resource.ResourceUrl,
2023-08-25 04:02:07 +00:00
RenderProcessTime: websocket_data.RenderProcessTime{
UnityRenderTakesTime: "cache",
UploadUnityRenderImageTakesTime: "cache",
},
2023-08-09 08:06:40 +00:00
})
return
}
2023-08-16 04:16:23 +00:00
//组装数据
2023-08-30 10:51:24 +00:00
if err = w.assembleRenderDataToUnity(taskId, combineImage, renderImageData, productTemplate, model3dInfo, element, productSize); err != nil {
2023-08-16 08:20:16 +00:00
logx.Error("组装数据失败:", err)
2023-08-07 02:48:14 +00:00
return
}
2023-08-16 04:16:23 +00:00
}
2023-09-19 10:24:51 +00:00
// 获取模板相关信息
func (w *wsConnectItem) getProductRelateionInfo(renderImageData *websocket_data.RenderImageReqMsg) (productSize *gmodel.FsProductSize, productTemplate *gmodel.FsProductTemplateV2, model3d *gmodel.FsProductModel3d, err error) {
2023-09-12 08:40:19 +00:00
//获取模板
2023-09-12 09:26:44 +00:00
productTemplate, err = w.logic.svcCtx.AllModels.FsProductTemplateV2.FindOneCloudRenderByProductIdTemplateTag(w.logic.ctx, renderImageData.RenderData.ProductId, renderImageData.RenderData.TemplateTag, "sort ASC")
2023-09-12 08:40:19 +00:00
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
2023-09-12 09:05:41 +00:00
w.renderErrResponse(renderImageData.RenderId, renderImageData.RenderData.TemplateTag, "", "找不到对应开启云渲染模板", renderImageData.RenderData.ProductId, w.userId, w.guestId, 0, 0, 0, 0)
return nil, nil, nil, errors.New("找不到对应开启云渲染模板")
2023-09-12 08:40:19 +00:00
}
logx.Error(err)
2023-09-12 09:05:41 +00:00
w.renderErrResponse(renderImageData.RenderId, renderImageData.RenderData.TemplateTag, "", "获取对应开启云渲染模板失败", renderImageData.RenderData.ProductId, w.userId, w.guestId, 0, 0, 0, 0)
return nil, nil, nil, errors.New("获取对应开启云渲染模板失败")
2023-09-12 08:40:19 +00:00
}
//根据模板找到模型
model3d, err = w.logic.svcCtx.AllModels.FsProductModel3d.FindOne(w.logic.ctx, *productTemplate.ModelId)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
w.renderErrResponse(renderImageData.RenderId, renderImageData.RenderData.TemplateTag, "", "找不到对应模型", renderImageData.RenderData.ProductId, w.userId, w.guestId, productTemplate.Id, 0, 0, 0)
return nil, nil, nil, errors.New("找不到对应模型")
}
logx.Error(err)
w.renderErrResponse(renderImageData.RenderId, renderImageData.RenderData.TemplateTag, "", "获取对应模型失败", renderImageData.RenderData.ProductId, w.userId, w.guestId, productTemplate.Id, 0, 0, 0)
return nil, nil, nil, errors.New("获取对应模型失败")
}
//根据模型id获取尺寸信息
productSize, err = w.logic.svcCtx.AllModels.FsProductSize.FindOne(w.logic.ctx, *model3d.SizeId)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
w.renderErrResponse(renderImageData.RenderId, renderImageData.RenderData.TemplateTag, "", "找不到对应尺寸", renderImageData.RenderData.ProductId, w.userId, w.guestId, productTemplate.Id, model3d.Id, 0, 0)
return nil, nil, nil, errors.New("找不到对应尺寸")
}
logx.Error(err)
w.renderErrResponse(renderImageData.RenderId, renderImageData.RenderData.TemplateTag, "", "获取对应尺寸失败", renderImageData.RenderData.ProductId, w.userId, w.guestId, productTemplate.Id, model3d.Id, 0, 0)
return nil, nil, nil, errors.New("获取对应尺寸失败")
}
return
}
2023-08-24 10:02:00 +00:00
// 组装数据发送给unity
2023-08-30 10:51:24 +00:00
func (w *wsConnectItem) assembleRenderDataToUnity(taskId string, combineImage string, info websocket_data.RenderImageReqMsg, productTemplate *gmodel.FsProductTemplateV2, model3dInfo *gmodel.FsProductModel3d, element *gmodel.FsProductTemplateElement, productSize *gmodel.FsProductSize) (err error) {
2023-08-16 04:16:23 +00:00
//组装数据
refletion := -1
if element.Refletion != nil && *element.Refletion != "" {
refletion, err = strconv.Atoi(*element.Refletion)
2023-08-17 06:48:27 +00:00
if err != nil {
2023-09-07 02:17:41 +00:00
logx.Error("err refletion")
2023-08-31 03:50:55 +00:00
w.renderErrResponse(info.RenderId, info.RenderData.TemplateTag, taskId, "解析云渲染设置把Refletion字段值从字符串转数字失败", info.RenderData.ProductId, w.userId, w.guestId, productTemplate.Id, model3dInfo.Id, productSize.Id, *productTemplate.ElementModelId)
2023-08-24 09:23:41 +00:00
return err
2023-08-17 06:48:27 +00:00
}
2023-08-16 04:16:23 +00:00
}
//组装data数据
var mode map[string]interface{}
if element.Mode != nil && *element.Mode != "" {
if err = json.Unmarshal([]byte(*element.Mode), &mode); err != nil {
logx.Error("faile to parse element mode json:", err)
2023-08-31 03:50:55 +00:00
w.renderErrResponse(info.RenderId, info.RenderData.TemplateTag, taskId, "解析云渲染设置字段 mode 为map对象失败", info.RenderData.ProductId, w.userId, w.guestId, productTemplate.Id, model3dInfo.Id, productSize.Id, *productTemplate.ElementModelId)
2023-08-16 08:20:16 +00:00
return err
2023-08-16 04:16:23 +00:00
}
}
tempData := make([]map[string]interface{}, 0, 3)
if element.Base != nil && *element.Base != "" {
tempData = append(tempData, map[string]interface{}{
"name": "model",
2023-09-13 02:42:33 +00:00
"data": "0," + combineImage + "," + *element.Base + " " + fmt.Sprintf("%d", time.Now().UTC().UnixMilli()),
2023-08-16 04:16:23 +00:00
"type": "other",
"layer": "0",
"is_update": 1,
"mode": mode["model"],
})
}
if element.Shadow != nil && *element.Shadow != "" {
tempData = append(tempData, map[string]interface{}{
"name": "shadow",
"data": *element.Shadow,
"type": "other",
"layer": "0",
"is_update": 0,
"mode": mode["shadow"],
})
}
if element.ModelP != nil && *element.ModelP != "" {
tempData = append(tempData, map[string]interface{}{
"name": "model_P",
"data": "0," + *element.ModelP,
"type": "other",
"layer": "0",
"is_update": 0,
"mode": mode["model_P"],
})
}
result := []interface{}{
map[string]interface{}{
"light": *element.Light,
"refletion": refletion,
"scale": *element.Scale,
"sku_id": info.RenderData.ProductId,
"tid": *element.Title,
"rotation": *element.Rotation,
"filePath": "", //todo 文件路径,针对千人千面
"data": tempData,
},
}
2023-08-31 02:04:03 +00:00
//发送运行阶段消息(组装数据)
2023-08-31 02:35:13 +00:00
w.sendAssembleRenderDataStepResponseMessage(info.RenderId)
2023-09-13 05:59:01 +00:00
temId := websocket_data.ToUnityIdStruct{
TaskId: taskId,
Wid: w.uniqueId,
RenderId: info.RenderId,
RenderBeginTime: time.Now().UTC().UnixMilli(),
}
temIdBytes, _ := json.Marshal(temId)
2023-08-16 04:16:23 +00:00
sendData := map[string]interface{}{
2023-09-13 05:59:01 +00:00
"id": string(temIdBytes),
2023-08-16 04:16:23 +00:00
"order_id": 0,
"user_id": info.RenderData.UserId,
"guest_id": info.RenderData.GuestId,
"sku_ids": []int64{info.RenderData.ProductId},
"tids": []string{*element.Title},
"data": result,
"is_thousand_face": 0,
"folder": "", //todo 千人千面需要使用
}
2023-08-16 06:09:03 +00:00
//请求unity接口
2023-08-17 08:07:27 +00:00
url := w.logic.svcCtx.Config.Unity.Host + "/api/render/queue/push"
2023-08-16 04:33:01 +00:00
header := make(map[string]string)
header["content-type"] = "application/json"
postData := map[string]interface{}{
"group": "unity3d",
"source": "home page",
"priority": 1,
2023-08-25 03:06:32 +00:00
"create_at": time.Now().UTC(),
2023-08-16 04:33:01 +00:00
"render_data": sendData,
}
2023-08-21 06:46:39 +00:00
postDataBytes, _ := json.Marshal(postData)
_, err = curl.ApiCall(url, "POST", header, bytes.NewReader(postDataBytes), time.Second*10)
2023-08-16 04:33:01 +00:00
if err != nil {
2023-08-31 03:50:55 +00:00
w.renderErrResponse(info.RenderId, info.RenderData.TemplateTag, taskId, "请求unity接口失败", info.RenderData.ProductId, w.userId, w.guestId, productTemplate.Id, model3dInfo.Id, productSize.Id, *productTemplate.ElementModelId)
2023-08-16 04:33:01 +00:00
logx.Error("failed to send data to unity")
2023-08-16 08:20:16 +00:00
return err
2023-08-16 04:33:01 +00:00
}
2023-08-31 02:04:03 +00:00
//发送运行阶段消息
2023-08-31 02:35:13 +00:00
w.sendRenderDataToUnityStepResponseMessage(info.RenderId)
2023-09-12 02:53:28 +00:00
logx.Info("发送到unity成功,刀版图:", combineImage /*, " 请求unity的数据:", string(postDataBytes)*/)
2023-08-16 08:20:16 +00:00
return nil
2023-08-07 02:48:14 +00:00
}
2023-08-31 02:35:13 +00:00
// 发送合图完毕阶段通知消息
2023-09-06 08:48:19 +00:00
func (w *wsConnectItem) sendCombineImageStepResponseMessage(renderId, combineImage string, sizeId, modelId, templateId, combineTime, uploadTime int64) {
2023-09-05 06:58:37 +00:00
combineTakesTime := "cache"
uploadCombineImageTakesTime := "cache"
if combineTime > 0 {
combineTakesTime = fmt.Sprintf("%dms", combineTime)
}
if uploadTime > 0 {
uploadCombineImageTakesTime = fmt.Sprintf("%dms", uploadTime)
}
2023-08-31 02:35:13 +00:00
w.sendToOutChan(w.respondDataFormat(constants.WEBSOCKET_COMBINE_IMAGE, websocket_data.CombineImageRspMsg{
RenderId: renderId,
CombineImage: combineImage,
2023-09-06 02:14:45 +00:00
SizeId: sizeId,
2023-09-06 08:48:19 +00:00
ModelId: modelId,
TemplateId: templateId,
2023-08-31 02:35:13 +00:00
CombineProcessTime: websocket_data.CombineProcessTime{
2023-09-05 06:58:37 +00:00
CombineTakesTime: combineTakesTime,
UploadCombineImageTakesTime: uploadCombineImageTakesTime,
2023-08-31 02:35:13 +00:00
},
}))
}
// 发送组装unity渲染数据完毕阶段通知消息
func (w *wsConnectItem) sendAssembleRenderDataStepResponseMessage(renderId string) {
w.sendToOutChan(w.respondDataFormat(constants.WEBSOCKET_ASSEMBLE_RENDER_DATA, websocket_data.ToUnityRspMsg{RenderId: renderId}))
}
// 发送组装unity渲染数据完毕阶段通知消息
func (w *wsConnectItem) sendRenderDataToUnityStepResponseMessage(renderId string) {
w.sendToOutChan(w.respondDataFormat(constants.WEBSOCKET_SEND_DATA_TO_UNITY, websocket_data.AssembleRenderDataRspMsg{RenderId: renderId}))
}
2023-08-31 06:38:34 +00:00
// 发送渲染最终结果数据到前端
func (w *wsConnectItem) sendRenderResultData(data websocket_data.RenderImageRspMsg) {
w.sendToOutChan(w.respondDataFormat(constants.WEBSOCKET_RENDER_IMAGE, data))
}
2023-08-29 10:22:15 +00:00
// 组装渲染任务id
2023-09-05 07:08:03 +00:00
func (w *wsConnectItem) genRenderTaskId(combineImage string, renderImageData websocket_data.RenderImageReqMsg, model3dInfo *gmodel.FsProductModel3d, productTemplate *gmodel.FsProductTemplateV2, element *gmodel.FsProductTemplateElement) string {
2023-08-29 10:22:15 +00:00
//生成任务id(需要把user_id,guest_id设为0)
incomeHashParam := renderImageData.RenderData
incomeHashParam.UserId = 0 //设为0(渲染跟用户id无关)
incomeHashParam.GuestId = 0 //设为0(渲染跟用户id无关)
incomeHashBytes, _ := json.Marshal(incomeHashParam)
modelHashStr := ""
templateHashStr := ""
if model3dInfo.ModelInfo != nil {
modelHashStr = *model3dInfo.ModelInfo
}
if productTemplate.TemplateInfo != nil {
templateHashStr = *productTemplate.TemplateInfo
}
elementHashBytes, _ := json.Marshal(element)
hashMap := map[string]interface{}{
"income_param": incomeHashBytes,
"model_info": modelHashStr,
"template_info": templateHashStr,
"material_image": *productTemplate.MaterialImg,
"render_element": elementHashBytes,
2023-09-05 07:08:03 +00:00
"combine_image": combineImage,
2023-08-29 10:22:15 +00:00
}
return hash.JsonHashKey(hashMap)
}