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

585 lines
25 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-09-27 10:35:20 +00:00
"context"
2023-07-26 06:35:29 +00:00
"encoding/json"
2023-08-09 08:06:40 +00:00
"errors"
2023-10-26 02:59:56 +00:00
"fmt"
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-11-23 02:35:34 +00:00
"fusenapi/utils/template_switch_info"
2023-08-24 06:24:04 +00:00
"fusenapi/utils/websocket_data"
2023-08-16 04:16:23 +00:00
"strconv"
2023-08-16 04:33:01 +00:00
"time"
2023-11-22 07:01:19 +00:00
"github.com/zeromicro/go-zero/core/logx"
"gorm.io/gorm"
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渲染并发数
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-27 10:35:20 +00:00
renderChan chan websocket_data.RenderImageReqMsg //渲染消息入口的缓冲队列
2023-10-10 09:49:47 +00:00
renderCtx context.Context //渲染控制上下文(用于切换模板标签/颜色/logo取消之前发送的不相同的任务
2023-09-27 10:35:20 +00:00
renderCtxCancelFunc context.CancelFunc //渲染控制上下文取消方法
2023-10-10 09:49:47 +00:00
selectColorIndex int //选择的颜色索引(用于标记连接当前连接选择的颜色)
2023-11-23 03:14:16 +00:00
templateTag string //模板标签 (用于标记连接当前连接选择的模板标签)
2023-10-10 09:49:47 +00:00
Logo string //logo地址 用于标记连接当前连接选择的logo
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-10-08 08:27:25 +00:00
//logx.Info("收到渲染任务消息")
2023-09-22 02:41:38 +00:00
var renderImageData websocket_data.RenderImageReqMsg
if err := json.Unmarshal(data, &renderImageData); err != nil {
2023-10-27 09:46:24 +00:00
w.renderErrResponse(renderImageData.RequestId, renderImageData.RenderData.TemplateTag, "", "数据格式错误", renderImageData.RenderData.ProductId, w.userId, w.guestId, 0, 0, 0, 0)
2023-09-22 02:41:38 +00:00
logx.Error("invalid format of websocket render image message", err)
return
}
2023-11-01 10:31:14 +00:00
//颜色/模板标签/logo变更
ifCancelOldCtx := false
2023-11-23 03:14:16 +00:00
if renderImageData.RenderData.TemplateTag != w.extendRenderProperty.templateTag {
ifCancelOldCtx = true
}
2023-10-10 09:49:47 +00:00
if renderImageData.RenderData.TemplateTagColor.SelectedColorIndex != w.extendRenderProperty.selectColorIndex {
ifCancelOldCtx = true
}
if renderImageData.RenderData.Logo != w.extendRenderProperty.Logo {
ifCancelOldCtx = true
}
if ifCancelOldCtx {
2023-10-10 09:50:56 +00:00
//赋值新的
2023-11-23 03:14:16 +00:00
w.extendRenderProperty.templateTag = renderImageData.RenderData.TemplateTag
2023-09-27 10:35:20 +00:00
w.extendRenderProperty.selectColorIndex = renderImageData.RenderData.TemplateTagColor.SelectedColorIndex
2023-10-10 09:49:47 +00:00
w.extendRenderProperty.Logo = renderImageData.RenderData.Logo
2023-09-27 10:35:20 +00:00
//让之前的失效
w.extendRenderProperty.renderCtxCancelFunc()
//重新赋值
w.extendRenderProperty.renderCtx, w.extendRenderProperty.renderCtxCancelFunc = context.WithCancel(w.logic.ctx)
2023-11-01 10:31:14 +00:00
}
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: //发入到缓冲队列
2023-08-16 08:20:16 +00:00
return
2023-09-22 07:52:32 +00:00
case <-time.After(time.Millisecond * 50): //超过50毫秒丢弃
2023-10-27 09:46:24 +00:00
w.renderErrResponse(renderImageData.RequestId, renderImageData.RenderData.TemplateTag, "", "渲染队列溢出,请稍后再发", renderImageData.RenderData.ProductId, w.userId, w.guestId, 0, 0, 0, 0)
2023-09-22 07:19:41 +00:00
return
2023-08-16 08:20:16 +00:00
}
}
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 07:12:44 +00:00
logx.Error("func consumeRenderImageData panic:", err)
2023-11-02 03:32:04 +00:00
//如果是上下文取消渲染的异常
if isCancelRenderPanic(err) {
//通知unity取消任务
sendCancelRenderMsgToUnity(w.wid, time.Now().UTC().Unix())
}
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 03:22:32 +00:00
limitChan <- struct{}{}
2023-09-22 07:12:44 +00:00
go func(d websocket_data.RenderImageReqMsg) {
2023-10-20 06:23:41 +00:00
defer func() {
2023-09-22 03:22:32 +00:00
if err := recover(); err != nil {
2023-10-07 07:09:49 +00:00
logx.Error("func renderImage main panic:", err)
2023-09-22 03:22:32 +00:00
}
2023-10-20 06:23:41 +00:00
}()
2023-10-07 07:15:16 +00:00
//临时chan用select io多路复用去判断携程退出
tmpChan := make(chan struct{}, 1)
defer close(tmpChan)
2023-09-22 03:22:32 +00:00
defer func() {
<-limitChan
}()
2023-11-23 03:14:16 +00:00
//如果不是无视上下文切换取消的(后面再开启)
/*if !d.IgnoreContextCancel {
go func() {
defer func() {
if err := recover(); err != nil {
logx.Error("func renderImage panic:", err)
}
}()
select {
case <-w.extendRenderProperty.renderCtx.Done():
//抛出取消渲染异常
cancelRenderPanic()
case <-tmpChan:
return
2023-10-07 07:09:49 +00:00
}
}()
2023-11-23 03:14:16 +00:00
}*/
2023-09-22 03:22:32 +00:00
w.renderImage(d)
2023-09-22 07:12:44 +00:00
}(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-11-27 03:40:44 +00:00
defer func() {
if err := recover(); err != nil {
logx.Error("render image panic!!!!!!")
}
}()
2023-10-31 04:22:04 +00:00
if renderImageData.RenderData.Logo == "" {
w.renderErrResponse(renderImageData.RequestId, renderImageData.RenderData.TemplateTag, "", "请传入logo", renderImageData.RenderData.ProductId, w.userId, w.guestId, 0, 0, 0, 0)
2023-09-19 07:09:48 +00:00
return
}
2023-10-08 08:07:55 +00:00
//没传分辨率
if renderImageData.RenderData.Resolution == "" {
2023-10-27 09:46:24 +00:00
w.renderErrResponse(renderImageData.RequestId, renderImageData.RenderData.TemplateTag, "", "请传入合图分辨率", renderImageData.RenderData.ProductId, w.userId, w.guestId, 0, 0, 0, 0)
2023-10-08 07:53:53 +00:00
return
2023-10-08 08:07:55 +00:00
}
//分辨率校验
resolution, err := strconv.Atoi(renderImageData.RenderData.Resolution)
if err != nil {
2023-10-27 09:46:24 +00:00
w.renderErrResponse(renderImageData.RequestId, renderImageData.RenderData.TemplateTag, "", "请传入正确的合图分辨率格式", renderImageData.RenderData.ProductId, w.userId, w.guestId, 0, 0, 0, 0)
2023-10-08 08:07:55 +00:00
return
}
2023-10-30 10:06:56 +00:00
if resolution < 100 || resolution > 800 {
w.renderErrResponse(renderImageData.RequestId, renderImageData.RenderData.TemplateTag, "", "请传入正确的合图分辨率范围值(100~800)", renderImageData.RenderData.ProductId, w.userId, w.guestId, 0, 0, 0, 0)
2023-10-08 08:07:55 +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 {
2023-10-27 09:46:24 +00:00
w.renderErrResponse(renderImageData.RequestId, renderImageData.RenderData.TemplateTag, "", "请传入模板标签选择的颜色", renderImageData.RenderData.ProductId, w.userId, w.guestId, 0, 0, 0, 0)
2023-09-19 06:39:33 +00:00
return
}
2023-09-19 08:10:17 +00:00
if renderImageData.RenderData.TemplateTagColor.SelectedColorIndex >= lenColor || renderImageData.RenderData.TemplateTagColor.SelectedColorIndex < 0 {
2023-10-27 09:46:24 +00:00
w.renderErrResponse(renderImageData.RequestId, renderImageData.RenderData.TemplateTag, "", "选择的模板标签颜色索引越界", renderImageData.RenderData.ProductId, w.userId, w.guestId, 0, 0, 0, 0)
2023-09-19 06:39:33 +00:00
return
}
2023-09-06 02:09:59 +00:00
//获取信息
2023-11-23 02:35:34 +00:00
productSize, productTemplate, model3dInfo, switchInfo, err := w.getProductRelationInfo(&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-10-27 09:46:24 +00:00
w.renderErrResponse(renderImageData.RequestId, 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-10-27 09:46:24 +00:00
w.renderErrResponse(renderImageData.RequestId, 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-10-20 06:01:09 +00:00
//累增websocket请求合图数
2023-10-31 06:23:08 +00:00
increaseCombineRequestCount(w.userId, w.guestId)
2023-10-20 06:01:09 +00:00
//请求完要释放
2023-10-31 06:23:08 +00:00
defer decreaseCombineRequestCount(w.userId, w.guestId)
2023-08-28 10:16:19 +00:00
//获取刀版图
combineReq := repositories.LogoCombineReq{
2023-11-23 04:33:29 +00:00
UserId: w.userId,
GuestId: w.guestId,
2023-09-19 08:03:11 +00:00
ProductTemplateV2Info: productTemplate,
ProductTemplateTagGroups: renderImageData.RenderData.TemplateTagGroups,
TemplateTag: renderImageData.RenderData.TemplateTag,
2023-11-22 10:02:05 +00:00
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-10-08 07:53:53 +00:00
Resolution: renderImageData.RenderData.Resolution,
2023-10-18 03:03:07 +00:00
Debug: w.debug,
2023-08-28 10:16:19 +00:00
}
2023-11-23 02:35:34 +00:00
//qrcode不需要依赖合图组开关
2023-11-23 04:24:12 +00:00
if switchInfo.SwitchInfo.QRcode.IfShow && switchInfo.SwitchInfo.QRcode.UserDisabled {
2023-11-23 07:04:17 +00:00
if renderImageData.RenderData.Qrcode == "" {
combineReq.Qrcode = &switchInfo.SwitchInfo.QRcode.Text
} else {
combineReq.Qrcode = &renderImageData.RenderData.Qrcode
}
2023-11-23 02:35:34 +00:00
}
//合图组开关开启
if switchInfo.CombineIsVisible {
//开启slogan
2023-11-23 04:24:12 +00:00
if switchInfo.SwitchInfo.Slogan.IfShow && switchInfo.SwitchInfo.Slogan.UserDisabled {
2023-11-23 02:35:34 +00:00
if renderImageData.RenderData.Slogan == "" {
2023-11-23 07:04:17 +00:00
combineReq.Slogan = &switchInfo.SwitchInfo.Slogan.Text
2023-11-23 02:35:34 +00:00
} else {
combineReq.Slogan = &renderImageData.RenderData.Slogan
}
}
//开启website
2023-11-23 04:24:12 +00:00
if switchInfo.SwitchInfo.Website.IfShow && switchInfo.SwitchInfo.Website.UserDisabled {
2023-11-23 02:35:34 +00:00
if renderImageData.RenderData.Website == "" {
2023-11-23 07:04:17 +00:00
combineReq.Website = &switchInfo.SwitchInfo.Website.Text
2023-11-23 02:35:34 +00:00
} else {
combineReq.Website = &renderImageData.RenderData.Website
}
}
//开启address
2023-11-23 04:24:12 +00:00
if switchInfo.SwitchInfo.Address.IfShow && switchInfo.SwitchInfo.Address.UserDisabled {
2023-11-23 02:35:34 +00:00
if renderImageData.RenderData.Address == "" {
2023-11-23 07:04:17 +00:00
combineReq.Address = &switchInfo.SwitchInfo.Address.Text
2023-11-23 02:35:34 +00:00
} else {
combineReq.Address = &renderImageData.RenderData.Address
}
}
//开启Phone
2023-11-23 04:24:12 +00:00
if switchInfo.SwitchInfo.Phone.IfShow && switchInfo.SwitchInfo.Phone.UserDisabled {
2023-11-23 02:35:34 +00:00
if renderImageData.RenderData.Phone == "" {
2023-11-23 07:04:17 +00:00
combineReq.Phone = &switchInfo.SwitchInfo.Phone.Text
2023-11-23 02:35:34 +00:00
} else {
combineReq.Phone = &renderImageData.RenderData.Phone
}
}
}
2023-09-22 07:52:32 +00:00
res, err := w.logic.svcCtx.Repositories.ImageHandle.LogoCombine(w.logic.ctx, &combineReq)
if err != nil {
2023-11-10 02:23:26 +00:00
//发送渲染错误消息
2023-10-27 09:46:24 +00:00
w.renderErrResponse(renderImageData.RequestId, renderImageData.RenderData.TemplateTag, "", "合成刀版图失败:"+err.Error(), renderImageData.RenderData.ProductId, w.userId, w.guestId, productTemplate.Id, model3dInfo.Id, productSize.Id, *productTemplate.ElementModelId)
2023-11-10 02:23:26 +00:00
//发送给前端重发消息
w.requestResendRenderResponse(websocket_data.RequestBrowserResendRenderEvent{
RequestId: renderImageData.RequestId,
Description: "combine require resend",
})
2023-11-01 06:35:37 +00:00
//统计合图失败数
increaseCombineRequestErrorCount(w.userId, w.guestId)
2023-09-22 07:52:32 +00:00
logx.Error("合成刀版图失败,合成请求数据:", combineReq, "错误信息:", err)
return
2023-08-28 10:16:19 +00:00
}
combineImage := "" //刀版图
if res != nil && res.ResourceUrl != nil {
combineImage = *res.ResourceUrl
} else {
2023-10-27 09:46:24 +00:00
w.renderErrResponse(renderImageData.RequestId, 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-10-27 09:46:24 +00:00
w.sendCombineImageStepResponseMessage(renderImageData.RequestId, combineImage, productSize.Id, model3dInfo.Id, productTemplate.Id, res.DebugData)
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-11-27 03:46:04 +00:00
switch err {
case nil: //有缓存
2023-10-18 03:03:07 +00:00
//如果没有debug或者debug模式下开启了缓存则返回缓存
if w.debug == nil || w.debug.IsCache == 1 {
//返回给客户端
w.sendRenderResultData(websocket_data.RenderImageRspMsg{
RequestId: renderImageData.RequestId,
Image: *resource.ResourceUrl,
2023-11-02 02:10:22 +00:00
RenderProcessTime: &websocket_data.RenderProcessTime{
2023-10-18 03:03:07 +00:00
UnityRenderTakesTime: "cache",
UploadUnityRenderImageTakesTime: "cache",
2023-11-22 07:01:19 +00:00
UnityRealTakesTime: "cache",
2023-10-18 03:03:07 +00:00
},
})
return
}
//否则继续去unity
2023-11-27 03:46:04 +00:00
default: //无缓存
if !errors.Is(err, gorm.ErrRecordNotFound) {
w.renderErrResponse(renderImageData.RequestId, renderImageData.RenderData.TemplateTag, taskId, "获取unity云渲染缓存失败", renderImageData.RenderData.ProductId, w.userId, w.guestId, productTemplate.Id, model3dInfo.Id, productSize.Id, *productTemplate.ElementModelId)
logx.Error("failed to find render resource:", err)
return
}
2023-08-09 08:06:40 +00:00
}
2023-08-16 04:16:23 +00:00
//组装数据
2023-10-30 09:42:39 +00:00
if err = w.assembleRenderDataToUnity(taskId, resolution, 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
// 获取模板相关信息
2023-11-23 02:35:34 +00:00
func (w *wsConnectItem) getProductRelationInfo(renderImageData *websocket_data.RenderImageReqMsg) (productSize *gmodel.FsProductSize, productTemplate *gmodel.FsProductTemplateV2, model3d *gmodel.FsProductModel3d, switchInfo template_switch_info.GetTemplateSwitchInfoRsp, 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-10-27 09:46:24 +00:00
w.renderErrResponse(renderImageData.RequestId, renderImageData.RenderData.TemplateTag, "", "找不到对应开启云渲染模板", renderImageData.RenderData.ProductId, w.userId, w.guestId, 0, 0, 0, 0)
2023-11-23 02:35:34 +00:00
return nil, nil, nil, template_switch_info.GetTemplateSwitchInfoRsp{}, errors.New("找不到对应开启云渲染模板")
2023-09-12 08:40:19 +00:00
}
logx.Error(err)
2023-10-27 09:46:24 +00:00
w.renderErrResponse(renderImageData.RequestId, renderImageData.RenderData.TemplateTag, "", "获取对应开启云渲染模板失败", renderImageData.RenderData.ProductId, w.userId, w.guestId, 0, 0, 0, 0)
2023-11-23 02:35:34 +00:00
return nil, nil, nil, template_switch_info.GetTemplateSwitchInfoRsp{}, errors.New("获取对应开启云渲染模板失败")
2023-09-12 08:40:19 +00:00
}
2023-10-26 02:57:09 +00:00
//判断设计信息是否为空
2023-10-09 03:20:38 +00:00
if productTemplate.TemplateInfo == nil || *productTemplate.TemplateInfo == "" {
2023-10-27 09:46:24 +00:00
w.renderErrResponse(renderImageData.RequestId, renderImageData.RenderData.TemplateTag, "", "模板设计信息是空的", renderImageData.RenderData.ProductId, w.userId, w.guestId, productTemplate.Id, 0, 0, 0)
2023-11-23 02:35:34 +00:00
return nil, nil, nil, template_switch_info.GetTemplateSwitchInfoRsp{}, errors.New("模板设计信息是空的")
2023-10-09 03:20:38 +00:00
}
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) {
2023-10-27 09:46:24 +00:00
w.renderErrResponse(renderImageData.RequestId, renderImageData.RenderData.TemplateTag, "", "找不到对应模型", renderImageData.RenderData.ProductId, w.userId, w.guestId, productTemplate.Id, 0, 0, 0)
2023-11-23 02:35:34 +00:00
return nil, nil, nil, template_switch_info.GetTemplateSwitchInfoRsp{}, errors.New("找不到对应模型")
2023-09-12 08:40:19 +00:00
}
logx.Error(err)
2023-10-27 09:46:24 +00:00
w.renderErrResponse(renderImageData.RequestId, renderImageData.RenderData.TemplateTag, "", "获取对应模型失败", renderImageData.RenderData.ProductId, w.userId, w.guestId, productTemplate.Id, 0, 0, 0)
2023-11-23 02:35:34 +00:00
return nil, nil, nil, template_switch_info.GetTemplateSwitchInfoRsp{}, errors.New("获取对应模型失败")
2023-09-12 08:40:19 +00:00
}
2023-10-26 02:57:09 +00:00
//判断设计信息是否为空
2023-10-09 03:20:38 +00:00
if model3d.ModelInfo == nil || *model3d.ModelInfo == "" {
2023-10-27 09:46:24 +00:00
w.renderErrResponse(renderImageData.RequestId, renderImageData.RenderData.TemplateTag, "", "模型设计信息是空的", renderImageData.RenderData.ProductId, w.userId, w.guestId, productTemplate.Id, model3d.Id, 0, 0)
2023-11-23 02:35:34 +00:00
return nil, nil, nil, template_switch_info.GetTemplateSwitchInfoRsp{}, errors.New("模型设计信息是空的")
2023-10-09 03:20:38 +00:00
}
2023-09-12 08:40:19 +00:00
//根据模型id获取尺寸信息
productSize, err = w.logic.svcCtx.AllModels.FsProductSize.FindOne(w.logic.ctx, *model3d.SizeId)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
2023-10-27 09:46:24 +00:00
w.renderErrResponse(renderImageData.RequestId, renderImageData.RenderData.TemplateTag, "", "找不到对应尺寸", renderImageData.RenderData.ProductId, w.userId, w.guestId, productTemplate.Id, model3d.Id, 0, 0)
2023-11-23 02:35:34 +00:00
return nil, nil, nil, template_switch_info.GetTemplateSwitchInfoRsp{}, errors.New("找不到对应尺寸")
2023-09-12 08:40:19 +00:00
}
logx.Error(err)
2023-10-27 09:46:24 +00:00
w.renderErrResponse(renderImageData.RequestId, renderImageData.RenderData.TemplateTag, "", "获取对应尺寸失败", renderImageData.RenderData.ProductId, w.userId, w.guestId, productTemplate.Id, model3d.Id, 0, 0)
2023-11-23 02:35:34 +00:00
return nil, nil, nil, template_switch_info.GetTemplateSwitchInfoRsp{}, errors.New("获取对应尺寸失败")
2023-09-12 08:40:19 +00:00
}
2023-11-23 02:35:34 +00:00
switchInfo = template_switch_info.GetTemplateSwitchInfo(productTemplate.Id, productTemplate.TemplateInfo, *productTemplate.MaterialImg)
2023-09-12 08:40:19 +00:00
return
}
2023-08-24 10:02:00 +00:00
// 组装数据发送给unity
2023-10-30 09:42:39 +00:00
func (w *wsConnectItem) assembleRenderDataToUnity(taskId string, resolution int, combineImage string, info websocket_data.RenderImageReqMsg, productTemplate *gmodel.FsProductTemplateV2, model3dInfo *gmodel.FsProductModel3d, element *gmodel.FsProductTemplateElement, productSize *gmodel.FsProductSize) (err error) {
2023-11-27 07:55:31 +00:00
if w.userId != 127 {
w.renderErrResponse(info.RequestId, info.RenderData.TemplateTag, taskId, "测试拦截,稍后开放", info.RenderData.ProductId, w.userId, w.guestId, productTemplate.Id, model3dInfo.Id, productSize.Id, *productTemplate.ElementModelId)
return err
}
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-10-27 09:46:24 +00:00
w.renderErrResponse(info.RequestId, 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
}
2023-11-23 03:14:16 +00:00
//组装data数据
2023-08-16 04:16:23 +00:00
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-10-27 09:46:24 +00:00
w.renderErrResponse(info.RequestId, 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-10-16 09:09:15 +00:00
"data": "0," + combineImage + "," + *element.Base,
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"],
})
}
2023-11-27 07:45:45 +00:00
//×××××××××××××××××××××××××××××××××××××××××××××××××××××
2023-11-27 07:49:22 +00:00
if w.userId == 127 && info.RenderData.ProductId == 32 {
2023-11-27 07:45:45 +00:00
if element.Base != nil && *element.Base != "" {
tempData = append(tempData, map[string]interface{}{
"name": "model_1",
"data": "0," + combineImage + "," + *element.Base,
"type": "other",
"layer": "0",
"is_update": 1,
"mode": mode["model"],
})
}
if element.Shadow != nil && *element.Shadow != "" {
tempData = append(tempData, map[string]interface{}{
"name": "shadow_1",
"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_1",
2023-11-27 08:10:20 +00:00
"data": "0,",
2023-11-27 07:45:45 +00:00
"type": "other",
"layer": "0",
"is_update": 0,
"mode": mode["model_P_1"],
})
}
if element.Base != nil && *element.Base != "" {
tempData = append(tempData, map[string]interface{}{
"name": "model_2",
"data": "0," + combineImage + "," + *element.Base,
"type": "other",
"layer": "0",
"is_update": 1,
"mode": mode["model"],
})
}
if element.Shadow != nil && *element.Shadow != "" {
tempData = append(tempData, map[string]interface{}{
"name": "shadow_2",
"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_2",
"data": "0," + *element.ModelP,
"type": "other",
"layer": "0",
"is_update": 0,
"mode": mode["model_P"],
})
}
}
//××××××××××××××××××××××××××××××××××××××××××××××××××××
2023-08-16 04:16:23 +00:00
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-11-27 07:45:45 +00:00
//**************************
2023-11-27 07:49:22 +00:00
if w.userId == 127 && info.RenderData.ProductId == 32 {
2023-11-27 07:45:45 +00:00
result = []interface{}{
map[string]interface{}{
"light": 22,
"refletion": refletion,
"scale": *element.Scale,
"sku_id": info.RenderData.ProductId,
2023-11-27 08:03:01 +00:00
"tid": "ceshi",
2023-11-27 07:45:45 +00:00
"rotation": *element.Rotation,
"filePath": "", //todo 文件路径,针对千人千面
"data": tempData,
},
}
}
//*******************************
2023-08-31 02:04:03 +00:00
//发送运行阶段消息(组装数据)
2023-10-27 09:46:24 +00:00
w.sendAssembleRenderDataStepResponseMessage(info.RequestId)
2023-09-13 05:59:01 +00:00
temId := websocket_data.ToUnityIdStruct{
TaskId: taskId,
2023-11-02 03:32:04 +00:00
Wid: w.wid,
2023-10-11 06:19:22 +00:00
RequestId: info.RequestId,
2023-09-13 05:59:01 +00:00
RenderBeginTime: time.Now().UTC().UnixMilli(),
2023-10-08 09:04:37 +00:00
TemplateTag: info.RenderData.TemplateTag,
2023-11-02 08:00:49 +00:00
UserId: w.userId,
GuestId: w.guestId,
2023-09-13 05:59:01 +00:00
}
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,
2023-11-23 04:33:29 +00:00
"user_id": w.userId,
"guest_id": w.guestId,
2023-08-16 04:16:23 +00:00
"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",
2023-11-03 06:58:09 +00:00
"source": "product list",
2023-08-16 04:33:01 +00:00
"priority": 1,
2023-08-25 03:06:32 +00:00
"create_at": time.Now().UTC(),
2023-10-30 09:42:39 +00:00
"resolution": resolution,
2023-08-16 04:33:01 +00:00
"render_data": sendData,
}
2023-08-21 06:46:39 +00:00
postDataBytes, _ := json.Marshal(postData)
2023-10-26 02:59:56 +00:00
beginPostTime := time.Now().UTC().UnixMilli()
2023-08-21 06:46:39 +00:00
_, err = curl.ApiCall(url, "POST", header, bytes.NewReader(postDataBytes), time.Second*10)
2023-08-16 04:33:01 +00:00
if err != nil {
2023-10-27 09:46:24 +00:00
w.renderErrResponse(info.RequestId, 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-10-26 03:02:14 +00:00
logx.Info(fmt.Sprintf("发送unity post数据耗时:%dms", time.Now().UTC().UnixMilli()-beginPostTime))
2023-08-31 02:04:03 +00:00
//发送运行阶段消息
2023-10-27 09:46:24 +00:00
w.sendRenderDataToUnityStepResponseMessage(info.RequestId)
2023-09-26 10:38:36 +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-10-16 07:26:45 +00:00
// 组装渲染任务id
func (w *wsConnectItem) genRenderTaskId(combineImage string, renderImageData websocket_data.RenderImageReqMsg, model3dInfo *gmodel.FsProductModel3d, productTemplate *gmodel.FsProductTemplateV2, element *gmodel.FsProductTemplateElement) string {
2023-11-23 04:33:29 +00:00
incomeHashBytes, _ := json.Marshal(renderImageData.RenderData)
2023-10-16 07:26:45 +00:00
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,
"combine_image": combineImage,
}
return hash.JsonHashKey(hashMap)
}