This commit is contained in:
laodaming 2023-08-14 17:31:57 +08:00
parent 94a36bd98e
commit 24a5bf11a3
5 changed files with 61 additions and 142 deletions

View File

@ -32,3 +32,14 @@ func (pt *FsProductTemplateTagsModel) GetList(ctx context.Context, page, limit i
err = db.Offset(offset).Limit(limit).Find(&resp).Error
return resp, err
}
func (pt *FsProductTemplateTagsModel) GetListByTitles(ctx context.Context, titles []string, limit int, orderBy string) (resp []FsProductTemplateTags, err error) {
if len(titles) == 0 {
return nil, nil
}
db := pt.db.WithContext(ctx).Model(&FsProductTemplateTags{}).Where("`title` in (?) and `status` = ?", titles, 1)
if orderBy != "" {
db = db.Order(orderBy)
}
err = db.Limit(limit).Find(&resp).Error
return resp, err
}

View File

@ -61,12 +61,9 @@ func (m *FsUserMaterialModel) FindLatestOne(ctx context.Context, userId int64, g
if userId == 0 && guestId == 0 {
return FsUserMaterial{}, nil
}
db := m.db.WithContext(ctx).Model(&FsUserMaterial{}).Order("id DESC")
if userId != 0 {
db = db.Where("`user_id` = ?", userId)
} else {
db = db.Where("`guest_id` = ?", guestId)
}
db := m.db.WithContext(ctx).Model(&FsUserMaterial{}).
Where("`user_id` = ? and `guest_id` = ?", userId, guestId).
Order("id DESC")
err = db.Take(&resp).Error
return resp, err
}
@ -75,3 +72,8 @@ func (m *FsUserMaterialModel) FindOneById(ctx context.Context, id int64) (resp *
err = m.db.WithContext(ctx).Model(&FsUserMaterial{}).Where("id = ?", id).Take(&resp).Error
return resp, err
}
func (m *FsUserMaterialModel) GetListByUser(ctx context.Context, userId, guestId int64, limit int) (resp *FsUserMaterial, err error) {
err = m.db.WithContext(ctx).Model(&FsUserMaterial{}).
Where("`user_id` = ? and `guest_id` = ?", userId, guestId).Order("id DESC").Limit(limit).Find(&resp).Error
return resp, err
}

View File

@ -1,8 +1,12 @@
package logic
import (
"encoding/json"
"errors"
"fusenapi/model/gmodel"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"gorm.io/gorm"
"context"
@ -37,9 +41,39 @@ func NewGetProductTemplateTagsLogic(ctx context.Context, svcCtx *svc.ServiceCont
func (l *GetProductTemplateTagsLogic) GetProductTemplateTags(req *types.GetProductTemplateTagsReq, userinfo *auth.UserInfo) (resp *basic.Response) {
if req.Limit <= 0 || req.Limit > 100 {
req.Limit = 4
req.Limit = 5
}
var (
productTemplateTags []gmodel.FsProductTemplateTags
)
//获取用户元数据
userMaterial, err := l.svcCtx.AllModels.FsUserMaterial.GetListByUser(l.ctx, userinfo.UserId, userinfo.GuestId, req.Limit)
if err != nil {
if !errors.Is(err, gorm.ErrRecordNotFound) {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get ai recommend product template tag list")
}
// 返回固定模板标签列表
productTemplateTags, err = l.svcCtx.AllModels.FsProductTemplateTags.GetList(l.ctx, 1, req.Limit, "`id` DESC")
} else {
//元数据是空的
if userMaterial.Metadata == nil || *userMaterial.Metadata == "" {
// 返回固定模板标签列表
productTemplateTags, err = l.svcCtx.AllModels.FsProductTemplateTags.GetList(l.ctx, 1, req.Limit, "`id` DESC")
} else {
//解析元数据
var metaData map[string]interface{}
if err = json.Unmarshal([]byte(*userMaterial.Metadata), &metaData); err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeJsonErr, "failed to parse user metadata")
}
templateTagNameList, ok := metaData["template_tagid"].([]string)
if !ok {
return resp.SetStatusWithMessage(basic.CodeJsonErr, "invalid format of metadata`template_tagid")
}
productTemplateTags, err = l.svcCtx.AllModels.FsProductTemplateTags.GetListByTitles(l.ctx, templateTagNameList, req.Limit, "id DESC")
}
}
productTemplateTags, err := l.svcCtx.AllModels.FsProductTemplateTags.GetList(l.ctx, 1, req.Limit, "`id` DESC")
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get template tags")

View File

@ -1,24 +1,17 @@
package consumer
import (
"bytes"
"context"
"encoding/json"
"errors"
"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"
"strconv"
"time"
)
// 这里请求的py接口返回数据
@ -65,23 +58,8 @@ func (m *MqConsumerRenderAssemble) Run(ctx context.Context, data []byte) error {
logx.Error("failed to get template info:", err)
return nil //不返回错误就删除消息
}
combineImage := "" //刀版图
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 nil //不返回错误就删除消息
}
//如果不存在,则请求生成刀版图
if errors.Is(err, gorm.ErrRecordNotFound) {
combineImage, err = getCombineImage(ctx, svcCtx, parseInfo, productTemplate, combineHash)
if err != nil {
return nil //不返回错误就删除消息
}
} else {
combineImage = *resource.ResourceUrl
}
//获取刀版图
combineImage := "" //刀版图
//获取渲染设置信息
element, err := svcCtx.AllModels.FsProductTemplateElement.FindOneByModelId(ctx, *productTemplate.ModelId)
if err != nil {
@ -167,109 +145,3 @@ func (m *MqConsumerRenderAssemble) Run(ctx context.Context, data []byte) error {
logx.Info("发送渲染组装数据到unity成功")
return nil
}
// 获取刀版图
func getCombineImage(ctx context.Context, svcCtx *svc.ServiceContext, parseInfo websocket_data.AssembleRenderData, productTemplate *gmodel.FsProductTemplateV2, combineHash string) (image string, err error) {
if productTemplate.TemplateInfo == nil || *productTemplate.TemplateInfo == "" {
logx.Error("product template info`template_info is empty")
return "", errors.New("product template info`template_info is empty")
}
//反序列化替换其中一些参数
var moduleInfo interface{}
if err = json.Unmarshal([]byte(*productTemplate.TemplateInfo), &moduleInfo); err != nil {
logx.Error("failed to parse json:template_info:", err)
return "", err
}
//需要替换的参数
paramData := map[string]interface{}{
"logo_url": parseInfo.RenderData.Logo,
"website": parseInfo.RenderData.Website,
"slogan": parseInfo.RenderData.Slogan,
"address": parseInfo.RenderData.Address,
"phone": parseInfo.RenderData.Phone,
"colors": []string{},
"template_tagid": []string{"b1a"},
"is_crop": false,
"shape": "",
"ratio": 0,
"line": "",
"other": "",
"other1": "",
}
//获取用户素材信息
if parseInfo.RenderData.UserMaterialId > 0 {
userMaterial, err := svcCtx.AllModels.FsUserMaterial.FindOneById(ctx, parseInfo.RenderData.UserMaterialId)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
logx.Error("user material not exists:", parseInfo.RenderData.UserMaterialId)
return "", errors.New("user material not exists")
}
logx.Error("err failed to get user material info")
}
if userMaterial.Metadata != nil && *userMaterial.Metadata != "" {
//解析元数据
var materialMetaData map[string]interface{}
if err = json.Unmarshal([]byte(*userMaterial.Metadata), &materialMetaData); err != nil {
logx.Error("failed to parse user material`matadata: ", err)
return "", err
}
//赋值
paramData["colors"] = materialMetaData["colors"]
paramData["logo_url"] = materialMetaData["logo_url"]
paramData["shape"] = materialMetaData["shape"]
paramData["is_crop"] = materialMetaData["is_crop"]
paramData["ratio"] = materialMetaData["ratio"]
paramData["line"] = materialMetaData["line"]
paramData["other"] = materialMetaData["other"]
paramData["other1"] = materialMetaData["other1"]
}
}
combineInfo := map[string]interface{}{
"module_data": moduleInfo,
"param_data": paramData,
}
postData, _ := json.Marshal(combineInfo)
//请求合成图片
url := svcCtx.Config.PythonApi.CombineImageUrl
header := make(map[string]string)
header["content-type"] = "application/json"
/* f, _ := os.Create("a.txt")
defer f.Close()
f.Write(postData)*/
httpRsp, err := curl.ApiCall(url, "POST", header, bytes.NewReader(postData), time.Second*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("请求python合图失败,err:", err, " 请求数据:", string(postData))
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
}
logx.Info("发送渲染组装数据到unity成功")
return uploadRes.ResourceUrl, nil
}

View File

@ -16,12 +16,12 @@ type RenderData struct {
ProductId int64 `json:"product_id"` //产品id
UserMaterialId int64 `json:"user_material_id"` //用户素材id
Logo string `json:"logo"` //log资源地址(websocket连接建立再赋值)
Website string `json:"website"` //网站
/*Website string `json:"website"` //网站
Slogan string `json:"slogan"` //slogan
Address string `json:"address"` //地址
Phone string `json:"phone"` //电话
UserId int64 `json:"user_id"` //用户id(websocket连接建立再赋值)
GuestId int64 `json:"guest_id"` //游客id(websocket连接建立再赋值)
Phone string `json:"phone"` //电话*/
UserId int64 `json:"user_id"` //用户id(websocket连接建立再赋值)
GuestId int64 `json:"guest_id"` //游客id(websocket连接建立再赋值)
}
// websocket发送渲染完的数据