Merge branch 'develop' of gitee.com:fusenpack/fusenapi into develop

This commit is contained in:
momo 2023-09-08 17:36:14 +08:00
commit dfd3720068
3 changed files with 26 additions and 28 deletions

View File

@ -2,8 +2,12 @@ package gmodel
import "context"
func (p *FsProductModel) FindOne(ctx context.Context, id int64) (resp *FsProduct, err error) {
err = p.db.WithContext(ctx).Model(&FsProduct{}).Where("`id` = ? ", id).First(&resp).Error
func (p *FsProductModel) FindOne(ctx context.Context, id int64, fields ...string) (resp *FsProduct, err error) {
db := p.db.WithContext(ctx).Model(&FsProduct{}).Where("`id` = ? ", id)
if len(fields) != 0 {
db = db.Select(fields[0])
}
err = db.Take(&resp).Error
return resp, err
}
func (p *FsProductModel) FindOneBySn(ctx context.Context, sn string, fields ...string) (resp *FsProduct, err error) {

View File

@ -195,6 +195,7 @@ func (l *DataTransferLogic) setConnPool(conn *websocket.Conn, userInfo *auth.Use
renderImageTask: make(map[string]*renderTask),
renderImageTaskCtlChan: make(chan renderImageControlChanItem, renderImageTaskCtlChanLen),
renderChan: make(chan []byte, renderChanLen),
renderConsumeTickTime: 1, //默认1纳秒后面需要根据不同用户不同触发速度
},
}
//保存连接

View File

@ -35,6 +35,7 @@ type extendRenderProperty struct {
renderImageTask map[string]*renderTask //需要渲染的图片任务 key是taskId val 是renderId
renderImageTaskCtlChan chan renderImageControlChanItem //渲染任务新增/回调结果移除任务/更新渲染耗时属性的控制通道由于任务map无法读写并发
renderChan chan []byte //渲染消息入口的缓冲队列
renderConsumeTickTime time.Duration //消费渲染消息时钟间隔(纳秒),用于后期控制不同类型用户渲染速度限制
}
// 渲染任务新增移除的控制通道的数据
@ -74,15 +75,15 @@ func (w *wsConnectItem) consumeRenderImageData() {
logx.Error("func renderImage err:", err)
}
}()
tick := time.Tick(w.extendRenderProperty.renderConsumeTickTime)
for {
select {
case <-w.closeChan: //已关闭
return
case data := <-w.extendRenderProperty.renderChan:
w.renderImage(data)
case <-tick: //消费数据
w.renderImage(<-w.extendRenderProperty.renderChan)
}
}
}
// 执行渲染任务
@ -94,8 +95,8 @@ func (w *wsConnectItem) renderImage(data []byte) {
logx.Error("invalid format of websocket render image message", err)
return
}
//获取产品信息
productInfo, err := w.logic.svcCtx.AllModels.FsProduct.FindOne(w.logic.ctx, renderImageData.RenderData.ProductId)
//获取产品信息(部分字段)
productInfo, err := w.logic.svcCtx.AllModels.FsProduct.FindOne(w.logic.ctx, renderImageData.RenderData.ProductId, "id,is_customization")
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
w.renderErrResponse(renderImageData.RenderId, renderImageData.RenderData.TemplateTag, "", "该产品不存在", renderImageData.RenderData.ProductId, w.userId, w.guestId, 0, 0, 0, 0)
@ -110,29 +111,21 @@ func (w *wsConnectItem) renderImage(data []byte) {
w.renderErrResponse(renderImageData.RenderId, renderImageData.RenderData.TemplateTag, "", "该产品不可定制", renderImageData.RenderData.ProductId, w.userId, w.guestId, 0, 0, 0, 0)
return
}
//获取上传最近的logo
userMaterial, err := w.logic.svcCtx.AllModels.FsUserMaterial.FindLatestOne(w.logic.ctx, w.userId, w.guestId)
//获取用户需要渲染logo
logoInfo, err := w.logic.svcCtx.Repositories.ImageHandle.LogoInfo(w.logic.ctx, &repositories.LogoInfoReq{
UserId: w.userId,
GuestId: w.guestId,
})
if err != nil {
if !errors.Is(err, gorm.ErrRecordNotFound) {
w.renderErrResponse(renderImageData.RenderId, renderImageData.RenderData.TemplateTag, "", "获取用户上传logo素材失败", renderImageData.RenderData.ProductId, w.userId, w.guestId, 0, 0, 0, 0)
logx.Error("failed to get user logo")
return
}
//使用默认logo(id=0)
userMaterialDefault, err := w.logic.svcCtx.AllModels.FsUserMaterial.FindOneById(w.logic.ctx, 0)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
w.renderErrResponse(renderImageData.RenderId, renderImageData.RenderData.TemplateTag, "", "默认logo不存在", renderImageData.RenderData.ProductId, w.userId, w.guestId, 0, 0, 0, 0)
return
}
w.renderErrResponse(renderImageData.RenderId, renderImageData.RenderData.TemplateTag, "", "获取默认logo失败", renderImageData.RenderData.ProductId, w.userId, w.guestId, 0, 0, 0, 0)
logx.Error("default logo is not exists")
return
}
renderImageData.RenderData.Logo = *userMaterialDefault.ResourceUrl
} else {
renderImageData.RenderData.Logo = *userMaterial.ResourceUrl
w.renderErrResponse(renderImageData.RenderId, renderImageData.RenderData.TemplateTag, "", "获取用户logo素材错误", renderImageData.RenderData.ProductId, w.userId, w.guestId, 0, 0, 0, 0)
logx.Error(err)
return
}
if logoInfo == nil || logoInfo.LogoUrl == nil {
w.renderErrResponse(renderImageData.RenderId, renderImageData.RenderData.TemplateTag, "", "用户logo素材url是空的", renderImageData.RenderData.ProductId, w.userId, w.guestId, 0, 0, 0, 0)
return
}
renderImageData.RenderData.Logo = *logoInfo.LogoUrl
//用户id赋值
renderImageData.RenderData.UserId = w.userId
renderImageData.RenderData.GuestId = w.guestId