fix
This commit is contained in:
@@ -1,186 +0,0 @@
|
||||
package consumer
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"fusenapi/constants"
|
||||
"fusenapi/initalize"
|
||||
"fusenapi/server/render/internal/svc"
|
||||
"fusenapi/service/repositories"
|
||||
"fusenapi/utils/websocket_data"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"gorm.io/gorm"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// 这里请求的py接口返回数据
|
||||
type pythonApiRsp struct {
|
||||
Id string `json:"id"` //物料模板的id
|
||||
LogoUrl string `json:"logo_url"` //logo地址
|
||||
Result string `json:"result"` //图片base64
|
||||
}
|
||||
|
||||
// 消费渲染需要组装的数据
|
||||
type MqConsumerRenderAssemble struct {
|
||||
}
|
||||
|
||||
func (m *MqConsumerRenderAssemble) Run(ctx context.Context, data []byte) error {
|
||||
defer func() {
|
||||
if err := recover(); err != nil {
|
||||
logx.Error("MqConsumerRenderAssemble panic:", err)
|
||||
}
|
||||
}()
|
||||
logx.Info("收到需要组装的消息:", string(data))
|
||||
var parseInfo websocket_data.AssembleRenderData
|
||||
if err := json.Unmarshal(data, &parseInfo); err != nil {
|
||||
logx.Error("MqConsumerRenderAssemble数据格式错误:", err)
|
||||
return nil //不返回错误就删除消息
|
||||
}
|
||||
val := ctx.Value("svcctx")
|
||||
if val == nil {
|
||||
logx.Error("svcctx is nil")
|
||||
return nil //不返回错误就删除消息
|
||||
}
|
||||
svcCtx, ok := val.(*svc.ServiceContext)
|
||||
if !ok {
|
||||
logx.Error("svcctx is nil!!")
|
||||
return nil //不返回错误就删除消息
|
||||
}
|
||||
rabbitmq := initalize.RabbitMqHandle{}
|
||||
//根据templateTag获取templateTagId(后续模板表的tag改成template_tag后可能就不需要这个步骤了)
|
||||
templateTag, err := svcCtx.AllModels.FsProductTemplateTags.FindOneByTagName(ctx, parseInfo.RenderData.TemplateTag)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
logx.Error("can`t find template tag info by template tag:", parseInfo.RenderData.TemplateTag)
|
||||
return nil
|
||||
}
|
||||
logx.Error("failed to get template tag info")
|
||||
return nil
|
||||
}
|
||||
//获取模板(模板标签下的对一个物料的的模板)
|
||||
productTemplate, err := svcCtx.AllModels.FsProductTemplateV2.FindOneByProductIdTagIdWithSizeTable(ctx, parseInfo.RenderData.ProductId, fmt.Sprintf("%d", templateTag.Id))
|
||||
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 nil //不返回错误就删除消息
|
||||
}
|
||||
//获取刀版图
|
||||
res, err := svcCtx.Repositories.ImageHandle.LogoCombine(ctx, &repositories.LogoCombineReq{
|
||||
UserId: parseInfo.RenderData.UserId,
|
||||
GuestId: parseInfo.RenderData.GuestId,
|
||||
TemplateId: productTemplate.Id,
|
||||
TemplateTag: parseInfo.RenderData.TemplateTag,
|
||||
Website: parseInfo.RenderData.Website,
|
||||
Slogan: parseInfo.RenderData.Slogan,
|
||||
Address: parseInfo.RenderData.Address,
|
||||
Phone: parseInfo.RenderData.Phone,
|
||||
})
|
||||
if err != nil {
|
||||
logx.Error("合成刀版图失败:", err)
|
||||
return nil
|
||||
}
|
||||
combineImage := "" //刀版图
|
||||
if res != nil && res.ResourceUrl != nil {
|
||||
combineImage = *res.ResourceUrl
|
||||
} else {
|
||||
logx.Error("合成刀版图失败,合成的刀版图是空指针:", err)
|
||||
return nil
|
||||
}
|
||||
logx.Info("合成刀版图成功:", *res.ResourceUrl)
|
||||
//获取渲染设置信息
|
||||
element, err := svcCtx.AllModels.FsProductTemplateElement.FindOneByModelId(ctx, *productTemplate.ModelId)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
logx.Error("element info is not found,model_id = ?", *productTemplate.ModelId)
|
||||
return nil //不返回错误就删除消息
|
||||
}
|
||||
logx.Error("failed to get element list,", err)
|
||||
return nil //不返回错误就删除消息
|
||||
}
|
||||
//组装数据
|
||||
refletion := -1
|
||||
if element.Refletion != nil && *element.Refletion != "" {
|
||||
refletion, err = strconv.Atoi(*element.Refletion)
|
||||
}
|
||||
//组装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)
|
||||
return nil //不返回错误就删除消息
|
||||
}
|
||||
}
|
||||
tempData := make([]map[string]interface{}, 0, 3)
|
||||
if element.Base != nil && *element.Base != "" {
|
||||
tempData = append(tempData, map[string]interface{}{
|
||||
"name": "model",
|
||||
"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",
|
||||
"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": parseInfo.RenderData.ProductId,
|
||||
"tid": *element.Title,
|
||||
"rotation": *element.Rotation,
|
||||
"filePath": "", //todo 文件路径,针对千人千面
|
||||
"data": tempData,
|
||||
},
|
||||
}
|
||||
sendData := map[string]interface{}{
|
||||
"id": parseInfo.TaskId,
|
||||
"order_id": 0,
|
||||
"user_id": parseInfo.RenderData.UserId,
|
||||
"guest_id": parseInfo.RenderData.GuestId,
|
||||
"sku_ids": []int64{parseInfo.RenderData.ProductId},
|
||||
"tids": []string{*element.Title},
|
||||
"data": result,
|
||||
"is_thousand_face": 0,
|
||||
"folder": "", //todo 千人千面需要使用
|
||||
}
|
||||
b, _ := json.Marshal(sendData)
|
||||
if err = rabbitmq.SendMsg(constants.RABBIT_MQ_TO_UNITY, b); err != nil {
|
||||
logx.Error("发送渲染组装数据到rabbitmq失败:", err)
|
||||
return nil //不返回错误就删除消息
|
||||
}
|
||||
logx.Info("发送渲染组装数据到unity成功")
|
||||
//模拟发送回去(unity部署后要去掉)
|
||||
h := websocket_data.RenderImageNotify{
|
||||
TaskId: parseInfo.TaskId,
|
||||
Image: "https://fusenh5.kayue.cn:8011/storage/test/final_k8TgMUxauc_temp.png",
|
||||
}
|
||||
d, _ := json.Marshal(h)
|
||||
_ = rabbitmq.SendMsg(constants.RABBIT_MQ_RENDER_RESULT_DATA, d)
|
||||
return nil
|
||||
}
|
||||
@@ -1,35 +0,0 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"reflect"
|
||||
|
||||
"fusenapi/utils/basic"
|
||||
|
||||
"fusenapi/server/render/internal/logic"
|
||||
"fusenapi/server/render/internal/svc"
|
||||
"fusenapi/server/render/internal/types"
|
||||
)
|
||||
|
||||
func RenderNotifyHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
var req types.RenderNotifyReq
|
||||
userinfo, err := basic.RequestParse(w, r, svcCtx, &req)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// 创建一个业务逻辑层实例
|
||||
l := logic.NewRenderNotifyLogic(r.Context(), svcCtx)
|
||||
|
||||
rl := reflect.ValueOf(l)
|
||||
basic.BeforeLogic(w, r, rl)
|
||||
|
||||
resp := l.RenderNotify(&req, userinfo)
|
||||
|
||||
if !basic.AfterLogic(w, r, rl, resp) {
|
||||
basic.NormalAfterLogic(w, r, resp)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -12,11 +12,6 @@ import (
|
||||
func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
|
||||
server.AddRoutes(
|
||||
[]rest.Route{
|
||||
{
|
||||
Method: http.MethodPost,
|
||||
Path: "/api/render/render_notify",
|
||||
Handler: RenderNotifyHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
Method: http.MethodPost,
|
||||
Path: "/api/render/get_face_slice",
|
||||
|
||||
@@ -1,80 +0,0 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fusenapi/constants"
|
||||
"fusenapi/utils/auth"
|
||||
"fusenapi/utils/basic"
|
||||
"fusenapi/utils/file"
|
||||
"fusenapi/utils/websocket_data"
|
||||
|
||||
"fusenapi/server/render/internal/svc"
|
||||
"fusenapi/server/render/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type RenderNotifyLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewRenderNotifyLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RenderNotifyLogic {
|
||||
return &RenderNotifyLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
// 处理进入前逻辑w,r
|
||||
// func (l *RenderNotifyLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) {
|
||||
// }
|
||||
|
||||
// 处理逻辑后 w,r 如:重定向, resp 必须重新处理
|
||||
// func (l *RenderNotifyLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) {
|
||||
// // httpx.OkJsonCtx(r.Context(), w, resp)
|
||||
// }
|
||||
|
||||
func (l *RenderNotifyLogic) RenderNotify(req *types.RenderNotifyReq, userinfo *auth.UserInfo) (resp *basic.Response) {
|
||||
if req.TaskId == "" {
|
||||
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "invalid param task_id")
|
||||
}
|
||||
if req.Image == "" {
|
||||
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "invalid param image")
|
||||
}
|
||||
if req.UserId == 0 && req.GuestId == 0 {
|
||||
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "invalid user_id or guest_id")
|
||||
}
|
||||
// 上传文件
|
||||
var upload = file.Upload{
|
||||
Ctx: l.ctx,
|
||||
MysqlConn: l.svcCtx.MysqlConn,
|
||||
AwsSession: l.svcCtx.AwsSession,
|
||||
}
|
||||
uploadRes, err := upload.UploadFileByBase64(&file.UploadBaseReq{
|
||||
FileHash: req.TaskId,
|
||||
FileData: req.Image,
|
||||
UploadBucket: 1,
|
||||
ApiType: 2,
|
||||
UserId: req.UserId,
|
||||
GuestId: req.GuestId,
|
||||
})
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeFileUploadErr, "failed to upload render resource image")
|
||||
}
|
||||
//发送消息到对应的rabbitmq
|
||||
data := websocket_data.RenderImageNotify{
|
||||
TaskId: req.TaskId,
|
||||
Image: uploadRes.ResourceUrl,
|
||||
}
|
||||
d, _ := json.Marshal(data)
|
||||
if err = l.svcCtx.RabbitMq.SendMsg(constants.RABBIT_MQ_RENDER_RESULT_DATA, d); err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to send data")
|
||||
}
|
||||
return resp.SetStatusWithMessage(basic.CodeOK, "success")
|
||||
}
|
||||
@@ -5,19 +5,6 @@ import (
|
||||
"fusenapi/utils/basic"
|
||||
)
|
||||
|
||||
type RequestToUnity struct {
|
||||
}
|
||||
|
||||
type RequestReadImages struct {
|
||||
}
|
||||
|
||||
type RenderNotifyReq struct {
|
||||
TaskId string `json:"task_id"` //任务id
|
||||
UserId int64 `json:"user_id"`
|
||||
GuestId int64 `json:"guest_id"`
|
||||
Image string `json:"image"`
|
||||
}
|
||||
|
||||
type Request struct {
|
||||
}
|
||||
|
||||
|
||||
@@ -1,11 +1,8 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"flag"
|
||||
"fmt"
|
||||
"fusenapi/constants"
|
||||
"fusenapi/server/render/consumer"
|
||||
"net/http"
|
||||
|
||||
"fusenapi/utils/auth"
|
||||
@@ -30,12 +27,6 @@ func main() {
|
||||
defer server.Stop()
|
||||
|
||||
ctx := svc.NewServiceContext(c)
|
||||
//消费渲染前组装数据队列
|
||||
ctx1 := context.Background()
|
||||
ctx2, cancel := context.WithCancel(ctx1)
|
||||
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)
|
||||
fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port)
|
||||
server.Start()
|
||||
|
||||
Reference in New Issue
Block a user