fusenapi/server/render/consumer/assemble_render_data.go

239 lines
8.0 KiB
Go
Raw Normal View History

2023-08-07 03:44:54 +00:00
package consumer
2023-08-07 05:20:18 +00:00
import (
2023-08-08 04:22:15 +00:00
"bytes"
"context"
2023-08-07 11:13:16 +00:00
"encoding/json"
2023-08-08 04:22:15 +00:00
"errors"
"fmt"
"fusenapi/model/gmodel"
2023-08-07 11:13:16 +00:00
"fusenapi/utils/websocket_data"
2023-08-07 05:20:18 +00:00
"github.com/zeromicro/go-zero/core/logx"
2023-08-08 04:22:15 +00:00
"gorm.io/gorm"
"io/ioutil"
"net/http"
"time"
2023-08-07 05:20:18 +00:00
)
2023-08-07 03:44:54 +00:00
2023-08-08 04:22:15 +00:00
// 这里请求的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"`
}
2023-08-07 03:44:54 +00:00
// 消费渲染需要组装的数据
type MqConsumerRenderAssemble struct {
}
2023-08-08 04:22:15 +00:00
func (m *MqConsumerRenderAssemble) Run(ctx context.Context, data []byte) error {
2023-08-07 05:20:18 +00:00
logx.Info("收到需要组装的消息:", string(data))
2023-08-07 11:13:16 +00:00
var parseInfo websocket_data.AssembleRenderData
if err := json.Unmarshal(data, &parseInfo); err != nil {
logx.Error("MqConsumerRenderAssemble数据格式错误:", err)
return nil //不返回错误就删除消息
}
2023-08-08 04:22:15 +00:00
val := ctx.Value("allmodels")
if val == nil {
return errors.New("allmodels is nil")
}
allmodels, ok := val.(*gmodel.AllModelsGen)
if !ok {
return errors.New("allmodels is nil!!")
}
timeSearchBegin := time.Now().UnixMilli()
//获取模板
templateInfo, err := 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")
return nil
}
logx.Error("failed to get template info:", err)
return err
}
renderLogTime := time.Now().UnixMilli() - timeSearchBegin
now := time.Now().Unix()
title := "1-组装模板数据"
//云渲染日志
err = allmodels.FsCloudRenderLog.Create(ctx, &gmodel.FsCloudRenderLog{
UserId: &parseInfo.RenderData.UserId,
Title: &title,
Time: &renderLogTime,
Tag: &parseInfo.RenderId,
Ctime: &now,
})
if err != nil {
logx.Error(err)
}
pyapiBeginTime := time.Now().UnixMilli()
//这里curl post请求数据。获取处理好的贴图数据用于贴model的贴图
pythonPostData := map[string]interface{}{
"tids": templateInfo.Id,
"data": parseInfo.RenderData.Data,
}
pyPostBytes, _ := json.Marshal(pythonPostData)
url := "http://110.41.19.98:8867/imgRender"
pyRsp, err := http.Post(url, "application/json;charset=UTF-8", bytes.NewReader(pyPostBytes))
if err != nil {
logx.Error("request python render api err:", err)
return err
}
defer pyRsp.Body.Close()
pyRspBytes, err := ioutil.ReadAll(pyRsp.Body)
if err != nil {
logx.Error("failed to read python api rsp body,err=", err)
return err
}
var rspInfo pythonApiRsp
if err = json.Unmarshal(pyRspBytes, &rspInfo); err != nil {
logx.Error("failed to unmarshal python api rsp:", err)
return err
}
if rspInfo.Code != 200 {
logx.Error("python api 接口请求错误:", rspInfo.Msg)
return err
}
if len(rspInfo.Data) == 0 {
logx.Error("python api 接口没有数据:")
return err
}
//云渲染日志
title = "2-请求->接收python合成刀版图接口"
now = time.Now().Unix()
pyRequestTime := time.Now().UnixMilli() - pyapiBeginTime
err = allmodels.FsCloudRenderLog.Create(ctx, &gmodel.FsCloudRenderLog{
UserId: &parseInfo.RenderData.UserId,
Title: &title,
Time: &pyRequestTime,
Tag: &parseInfo.RenderId,
Ctime: &now,
})
if err != nil {
logx.Error(err)
}
incTime := int64(0)
mapCurlData := make(map[int64]int)
for k, v := range rspInfo.Data {
mapCurlData[v.Tid] = k
incTime += v.Costtime
}
//云渲染日志
title = "3-python合成刀版图"
now = time.Now().Unix()
postData := string(pyPostBytes)
pyRspStr := string(pyRspBytes)
err = allmodels.FsCloudRenderLog.Create(ctx, &gmodel.FsCloudRenderLog{
UserId: &parseInfo.RenderData.UserId,
PostUrl: &url,
PostData: &postData,
Result: &pyRspStr,
Title: &title,
Time: &incTime,
Tag: &parseInfo.RenderId,
Ctime: &now,
})
if err != nil {
logx.Error(err)
}
//获取渲染设置信息
//element, err := allmodels.FsProductTemplateElement
/*
$element = ProductTemplateElement::find()
->andFilterWhere(['in', 'product_template_id', $mids])
->asArray()
->all();
$element = array_column($element, null, 'product_template_id');
$elementTitles = array_column($element, 'title');
$result = [];
$time_pinjie_begin = $render->getMillisecond();
foreach ($templates as $key => $val) {
if(!isset($element[$val['model_id']]) || !isset($imageData[$val['id']])){
continue;
}
//数据拼装
$item = [];
$item['light'] = $element[$val['model_id']]['light'];
$item['refletion'] = $element[$val['model_id']]['refletion'] == '' ? -1 : (int)$element[$val['model_id']]['refletion'];
$item['scale'] = $element[$val['model_id']]['scale'];
$item['sku_id'] = $val['product_id'];
$item['tid'] = $element[$val['model_id']]['title'];
$item['rotation'] = $element[$val['model_id']]['rotation'];
$item['filePath'] = '';//todo 文件路径,针对千人千面
//组装data数据
$tempData = [];
//获取材质模式对应关系
$mode = $element[$val['model_id']]['mode'] ? json_decode($element[$val['model_id']]['mode'], true) : [];
// $base_img = (new ImageService())->base64EncodeImageNoHeader(\Yii::$app->params['baseurl'].$imageData[$val['id']]['imgurl']);
$base_img = \Yii::$app->params['h5Url'].'/storage'.$imageData[$val['id']]['imgurl'];
//判断是否包含base数据 即对应建模那边的model
if($element[$val['model_id']]['base']){
$tempData[] = [
'name' => 'model',
'data' => '0,'.$base_img.','.$element[$val['model_id']]['base'],
'type' => 'other',
'layer' => '0',
'is_update' => 1,
'mode' => $mode['model'],
];
}
if($element[$val['model_id']]['shadow']){
$tempData[] = [
'name' => 'shadow',
'data' => $element[$val['model_id']]['shadow'],
'type' => 'other',
'layer' => '0',
'is_update' => 0,
'mode' => $mode['shadow'],
];
}
if($element[$val['model_id']]['model_p']){
$tempData[] = [
'name' => 'model_P',
'data' => '0,'.$element[$val['model_id']]['model_p'],
'type' => 'other',
'layer' => '0',
'is_update' => 0,
'mode' => $mode['model_P'],
];
}
$item['data'] = $tempData;
$result[] = $item;
}
$log = new CloudRenderLog();
$log->title = '接收到python刀版图 -> 3-组装MQ渲染任务队列';
$log->time = $render->getMillisecond() - $time_pinjie_begin;
$log->user_id = $user_id;
$log->post_data = '';
$log->post_url = '';
$log->result = $res;
$log->tag = $inputData['id'];
$log->ctime = time();
$log->save(false);
}
2023-08-07 11:13:16 +00:00
2023-08-08 04:22:15 +00:00
$sendData = [
'id' => $inputData['id'],
'order_id' => 0,
'user_id' => \Yii::$app->user->id,
'sku_ids' => $inputData['sku_ids'],
'tids' => $elementTitles,
'data' => $result,
'is_thousand_face' => 0,
'folder' => '',//todo 千人千面需要使用
];
return $sendData;*/
2023-08-07 03:44:54 +00:00
return nil
}