@@ -7,26 +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"
"net/http"
"strconv"
"strings"
"time"
)
// 这里请求的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 str ing ` json:"id" ` //物料模板的id
LogoUrl string ` json:"logo_url" ` //logo地址
result string ` json:"result" ` //图片base64
}
// 消费渲染需要组装的数据
@@ -40,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!!" )
}
timeSearchBegin := time . Now ( ) . UnixMilli ( )
//获取模板
templateInfo , err := a llm odels. FsProductTemplateV2 . FindOneByProductIdTagIdWithSizeTable ( ctx , parseInfo . RenderData . ProductId , fmt . Sprintf ( "%d" , parseInfo . RenderData . TemplateTagId ) )
rabbitmq := initalize . RabbitMqHandle { }
//获取模板( 产品第一个sku的模板)
templateInfo , err := svcCtx . A llM odels. 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" )
@@ -59,102 +56,25 @@ func (m *MqConsumerRenderAssemble) Run(ctx context.Context, data []byte) error {
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 ,
GuestId : & parseInfo . RenderData . GuestId ,
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" : [ ] int64 { templateInfo . Id } ,
"data" : parseInfo . RenderData . Data ,
}
pyPostBytes , _ := json . Marshal ( pythonPostData )
//post 数据结构
a := ` { "tids":[128,431],"data":[ { "id":"9d35ac5a-81a0-3caf-3246-cdbea9f2ddfe","tag":"MainColor","title":"\u8d34\u56fe2","type":"color","text":"","fill":"#c028b9","fontSize":20,"fontFamily":"Aqum2SmallCaps3","ifBr":false,"ifShow":true,"ifGroup":false,"maxNum":50,"rotation":0,"align":"center","verticalAlign":"middle","material":"","width":1024,"height":1024,"x":0,"y":0,"opacity":1,"optionalColor":[ { "color":"#000000","name":"Black","default":true}],"zIndex":2,"svgPath":"","follow": { "fill":"","ifShow":"","content":""},"group":[],"cameraStand": { "x":0,"y":0,"z":0},"proportion":60,"materialName":"","materialTime":""}, { "id":"c9be653f-dfc1-5659-1eb8-7ab128abe3d5","tag":"Logo","title":"\u8d34\u56fe4","type":"image","text":"","fill":"#c028b9","fontSize":65,"fontFamily":"MontserratBold3","ifBr":true,"ifShow":true,"ifGroup":false,"maxNum":50,"rotation":0,"align":"center","verticalAlign":"middle","material":"","width":312,"height":144.8044172010362,"x":99,"y":406.49999999999875,"opacity":1,"optionalColor":[ { "color":"#000000","name":"Black","default":false}, { "color":"#FFFFFF","name":"White","default":false}, { "name":"MainColor","color":"#c028b9","default":true}],"zIndex":3,"svgPath":"","follow": { "fill":"","ifShow":"","content":""},"group":[],"cameraStand": { "x":0,"y":0,"z":0},"proportion":60,"materialTime":"","materialName":""}, { "id":"e3269e77-b8c2-baec-bb9b-8399915a711c","tag":"Slogan","title":"\u8d34\u56fe5","type":"text","text":"","fill":"","fontSize":16,"fontFamily":"MontserratBold3","ifBr":false,"ifShow":true,"ifGroup":false,"maxNum":50,"rotation":0,"align":"center","verticalAlign":"middle","material":"","width":312,"height":18.999939381668568,"x":99,"y":605.0000538829611,"opacity":1,"optionalColor":[ { "color":"#000000","name":"Black","default":true}],"zIndex":4,"svgPath":"","follow": { "fill":"bfc2b5a3-10af-c95b-fbf1-3016540fffad","ifShow":"","content":""},"group":[],"cameraStand": { "x":0,"y":0,"z":36},"proportion":60,"materialName":"","materialTime":""}, { "id":"bfc2b5a3-10af-c95b-fbf1-3016540fffad","tag":"SecondaryColor","title":"\u8d34\u56fe9","type":"color","text":"","fill":"#FFFFFF","fontSize":20,"fontFamily":"Aqum2SmallCaps3","ifBr":false,"ifShow":true,"ifGroup":false,"maxNum":50,"rotation":0,"align":"center","verticalAlign":"middle","material":"","width":1024,"height":1024,"x":0,"y":0,"opacity":1,"optionalColor":[ { "color":"#000000","name":"Black","default":true}],"zIndex":1,"svgPath":"","follow": { "fill":"","ifShow":"","content":""},"group":[],"cameraStand": { "x":0,"y":0,"z":0},"proportion":60}]} `
url := "http://110.41.19.98:8867/imgRender"
pyRsp , err := http . Post ( url , "application/json;charset=UTF-8" , strings . NewReader ( a ) )
if err != nil {
logx . Error ( "request python render api err:" , err )
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
}
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
combineImage := "" //刀版图
//如果不存在,则请求生成刀版图
if errors . Is ( err , gorm . ErrRecordNotFound ) {
combineImage , err = getCombineImage ( ctx , svcCtx , parseInfo , combineHash )
if err != nil {
return err
}
} else {
combineImage = * resource . ResourceUrl
}
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
}
mapImageData := make ( map [ int64 ] int )
for k , v := range rspInfo . Data {
mapImageData [ v . Tid ] = k
}
//云渲染日志
title = "2-请求->接收python合成刀版图接口"
now = time . Now ( ) . Unix ( )
pyRequestTime := time . Now ( ) . UnixMilli ( ) - pyapiBeginTime
err = allmodels . FsCloudRenderLog . Create ( ctx , & gmodel . FsCloudRenderLog {
UserId : & parseInfo . RenderData . UserId ,
GuestId : & parseInfo . RenderData . GuestId ,
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 ,
GuestId : & parseInfo . RenderData . GuestId ,
PostUrl : & url ,
PostData : & postData ,
Result : & pyRspStr ,
Title : & title ,
Time : & incTime ,
Tag : & parseInfo . RenderId ,
Ctime : & now ,
} )
if err != nil {
logx . Error ( err )
}
timePinjieBegin := time . Now ( ) . UnixMilli ( )
//获取渲染设置信息
element , err := a llm odels. FsProductTemplateElement . FindOneByModelId ( ctx , * templateInfo . ModelId )
element , err := svcCtx . A llM odels. 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 )
@@ -176,15 +96,11 @@ func (m *MqConsumerRenderAssemble) Run(ctx context.Context, data []byte) error {
return err
}
}
baseImage := ""
if index , ok := mapImageData [ templateInfo . Id ] ; ok {
baseImage = constants . H5_URL + "/storage" + rspInfo . Data [ index ] . Imgurl
}
tempData := make ( [ ] map [ string ] interface { } , 0 , 3 )
if element . Base != nil && * element . Base != "" {
tempData = append ( tempData , map [ string ] interface { } {
"name" : "model" ,
"data" : "0," + bas eImage + "," + * element . Base ,
"data" : "0," + combin eImage + "," + * element . Base ,
"type" : "other" ,
"layer" : "0" ,
"is_update" : 1 ,
@@ -223,21 +139,6 @@ func (m *MqConsumerRenderAssemble) Run(ctx context.Context, data []byte) error {
"data" : tempData ,
} ,
}
timePinjie := time . Now ( ) . UnixMilli ( ) - timePinjieBegin
//云渲染日志
title = "接收到python刀版图 -> 3-组装MQ渲染任务队列"
now = time . Now ( ) . Unix ( )
err = allmodels . FsCloudRenderLog . Create ( ctx , & gmodel . FsCloudRenderLog {
UserId : & parseInfo . RenderData . UserId ,
GuestId : & parseInfo . RenderData . GuestId ,
Title : & title ,
Time : & timePinjie ,
Tag : & parseInfo . RenderId ,
Ctime : & now ,
} )
if err != nil {
logx . Error ( err )
}
sendData := map [ string ] interface { } {
"id" : parseInfo . TaskId ,
"order_id" : 0 ,
@@ -250,11 +151,55 @@ func (m *MqConsumerRenderAssemble) Run(ctx context.Context, data []byte) error {
"folder" : "" , //todo 千人千面需要使用
}
b , _ := json . Marshal ( sendData )
rabbitmq := initalize . RabbitMqHandle { }
if err = rabbitmq . SendMsg ( constants . RABBIT_MQ_TO_UNITY , b ) ; err != nil {
logx . Error ( "发送渲染组装数据到rabbitmq失败: " , err )
return err
}
logx . Info ( "发送渲染组装数据到rabbitmq 成功" )
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
}