diff --git a/model/gmodel/fs_product_template_tags_logic.go b/model/gmodel/fs_product_template_tags_logic.go index d0492bd2..b2529fcf 100755 --- a/model/gmodel/fs_product_template_tags_logic.go +++ b/model/gmodel/fs_product_template_tags_logic.go @@ -23,8 +23,8 @@ func (pt *FsProductTemplateTagsModel) FindOne(ctx context.Context, id int64, fie err = db.Take(&resp).Error return resp, err } -func (pt *FsProductTemplateTagsModel) GetList(ctx context.Context, page, limit int, isHide, status int64, orderBy string) (resp []FsProductTemplateTags, err error) { - db := pt.db.WithContext(ctx).Model(&FsProductTemplateTags{}).Where("`is_hide` = ? and `status` = ?", isHide, status) +func (pt *FsProductTemplateTagsModel) GetList(ctx context.Context, page, limit int, status int64, orderBy string) (resp []FsProductTemplateTags, err error) { + db := pt.db.WithContext(ctx).Model(&FsProductTemplateTags{}).Where(" `status` = ?", status) if orderBy != "" { db = db.Order(orderBy) } @@ -32,11 +32,11 @@ 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) GetListByTagNames(ctx context.Context, tagNames []string, limit int, isHide, status int64, orderBy string) (resp []FsProductTemplateTags, err error) { +func (pt *FsProductTemplateTagsModel) GetListByTagNames(ctx context.Context, tagNames []string, limit int, status int64, orderBy string) (resp []FsProductTemplateTags, err error) { if len(tagNames) == 0 { return nil, nil } - db := pt.db.WithContext(ctx).Model(&FsProductTemplateTags{}).Where("`template_tag` in (?) and `is_hide` = ? and `status` = ?", tagNames, isHide, status) + db := pt.db.WithContext(ctx).Model(&FsProductTemplateTags{}).Where("`template_tag` in (?) and `status` = ?", tagNames, status) if orderBy != "" { db = db.Order(orderBy) } diff --git a/model/gmodel/fs_user_logic.go b/model/gmodel/fs_user_logic.go index 440eeaac..e938690c 100644 --- a/model/gmodel/fs_user_logic.go +++ b/model/gmodel/fs_user_logic.go @@ -5,8 +5,8 @@ import ( "encoding/json" "fmt" - "fusenapi/server/fssql" "fusenapi/utils/auth" + "fusenapi/utils/fssql" "time" "github.com/zeromicro/go-zero/core/logx" @@ -204,7 +204,7 @@ func (u *FsUserModel) RegisterByFusen(ctx context.Context, token *auth.RegisterT // txUserInfo.Where("user_id = ?", user.Id).Row().Err() - now := time.Now() + now := time.Now().UTC() uinfo := &FsUserInfo{ Module: FsString("profile"), UserId: &user.Id, @@ -218,21 +218,17 @@ func (u *FsUserModel) RegisterByFusen(ctx context.Context, token *auth.RegisterT // txUserInfo.Statement.Table if err != nil { - if err == gorm.ErrRecordNotFound { err = txUserInfo.Create(uinfo).Error if err == gorm.ErrRecordNotFound { return nil } } - } else { - - err = fssql.MetadataPATCH(txUserInfo, "profile", FsUser{}, metadata, "user_id = ?", *uinfo.UserId) + err = fssql.MetadataModulePATCH(txUserInfo, "profile", FsUserInfo{}, metadata, "user_id = ?", *uinfo.UserId) if err != nil { return err } - } return err }) diff --git a/server/auth/internal/logic/usergoogleloginlogic.go b/server/auth/internal/logic/usergoogleloginlogic.go index 56a8e498..c9808571 100644 --- a/server/auth/internal/logic/usergoogleloginlogic.go +++ b/server/auth/internal/logic/usergoogleloginlogic.go @@ -89,10 +89,11 @@ func (l *UserGoogleLoginLogic) UserGoogleLogin(req *types.RequestGoogleLogin, us logx.Error(err) return resp.SetStatus(basic.CodeOK) } - + gemail := gresult.Get("email").String() l.registerInfo = &auth.RegisterToken{ Password: base64.RawURLEncoding.EncodeToString(nonce), Platform: string(auth.PLATFORM_GOOGLE), + Email: gemail, OperateType: auth.OpTypeRegister, TraceId: uuid.NewString(), CreateAt: time.Now().UTC(), @@ -110,8 +111,7 @@ func (l *UserGoogleLoginLogic) UserGoogleLogin(req *types.RequestGoogleLogin, us return resp.SetStatus(basic.CodeOAuthRegisterTokenErr) } l.registerToken = token - l.defaultEmail = gresult.Get("email").String() - + l.defaultEmail = gemail return resp.SetStatus(basic.CodeOK) } @@ -142,7 +142,7 @@ func (l *UserGoogleLoginLogic) AfterLogic(w http.ResponseWriter, r *http.Request l.token, l.isRegistered, l.registerToken, - l.registerInfo.Email, + l.defaultEmail, ) html := fmt.Sprintf(` diff --git a/server/product-template-tag/internal/logic/getproducttemplatetagslogic.go b/server/product-template-tag/internal/logic/getproducttemplatetagslogic.go index 184de958..8211d783 100644 --- a/server/product-template-tag/internal/logic/getproducttemplatetagslogic.go +++ b/server/product-template-tag/internal/logic/getproducttemplatetagslogic.go @@ -50,7 +50,7 @@ func (l *GetProductTemplateTagsLogic) GetProductTemplateTags(req *types.GetProdu //白板用户 if !userinfo.IsUser() && !userinfo.IsGuest() { // 返回固定模板标签列表 - productTemplateTags, err = l.svcCtx.AllModels.FsProductTemplateTags.GetList(l.ctx, 1, req.Limit, 0, 1, "`id` DESC") + productTemplateTags, err = l.svcCtx.AllModels.FsProductTemplateTags.GetListByTagNames(l.ctx, []string{"A1a"}, req.Limit, 1, "`id` DESC") if err != nil { logx.Error(err) return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get template tags") @@ -63,8 +63,8 @@ func (l *GetProductTemplateTagsLogic) GetProductTemplateTags(req *types.GetProdu 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, 0, 1, "`id` DESC") + // 返回固定模板A1a + productTemplateTags, err = l.svcCtx.AllModels.FsProductTemplateTags.GetListByTagNames(l.ctx, []string{"A1a"}, req.Limit, 1, "`id` DESC") if err != nil { logx.Error(err) return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get template tags") @@ -72,8 +72,8 @@ func (l *GetProductTemplateTagsLogic) GetProductTemplateTags(req *types.GetProdu } else { //元数据是空的 if userMaterial.Metadata == nil { - // 返回固定模板标签列表 - productTemplateTags, err = l.svcCtx.AllModels.FsProductTemplateTags.GetList(l.ctx, 1, req.Limit, 0, 1, "`id` DESC") + // 返回固定模板A1a + productTemplateTags, err = l.svcCtx.AllModels.FsProductTemplateTags.GetListByTagNames(l.ctx, []string{"A1a"}, req.Limit, 1, "`id` DESC") if err != nil { logx.Error(err) return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get template tags") @@ -81,7 +81,7 @@ func (l *GetProductTemplateTagsLogic) GetProductTemplateTags(req *types.GetProdu } else { //解析元数据 var metaData map[string]interface{} - if err = json.Unmarshal([]byte(*userMaterial.Metadata), &metaData); err != nil { + if err = json.Unmarshal(*userMaterial.Metadata, &metaData); err != nil { logx.Error(err) return resp.SetStatusWithMessage(basic.CodeJsonErr, "failed to parse user metadata") } @@ -91,7 +91,7 @@ func (l *GetProductTemplateTagsLogic) GetProductTemplateTags(req *types.GetProdu logx.Error(err) return resp.SetStatusWithMessage(basic.CodeJsonErr, "invalid format of metadata`s template_tagid") } - productTemplateTags, err = l.svcCtx.AllModels.FsProductTemplateTags.GetListByTagNames(l.ctx, templateTagNameList, req.Limit, 0, 1, "id DESC") + productTemplateTags, err = l.svcCtx.AllModels.FsProductTemplateTags.GetListByTagNames(l.ctx, templateTagNameList, req.Limit, 1, "id DESC") if err != nil { logx.Error(err) return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get template tags") diff --git a/server/product/internal/logic/gettemplatebypidlogic.go b/server/product/internal/logic/gettemplatebypidlogic.go index 7764fcc7..16e9fff8 100644 --- a/server/product/internal/logic/gettemplatebypidlogic.go +++ b/server/product/internal/logic/gettemplatebypidlogic.go @@ -105,14 +105,13 @@ func (l *GetTemplateByPidLogic) GetTemplateByPid(req *types.GetTemplateByPidReq, logx.Error(err) return resp.SetStatusWithMessage(basic.CodeJsonErr, fmt.Sprintf("failed to parse json product template info(may be old data):%d", templateInfo.Id)) } - //后台隐藏/显示信息(现在下面是写死了) - /*var switchInfo interface{} - if templateInfo.SwitchInfo != nil && *templateInfo.SwitchInfo != "" { - _ = json.Unmarshal([]byte(*templateInfo.SwitchInfo), &switchInfo) - }*/ modelInfo := modelList[modelIndex] mapKey := fmt.Sprintf("_%d", *modelInfo.SizeId) - rsp[mapKey] = template_switch_info.GetTemplateSwitchInfo(templateInfo.Id, *templateInfo.MaterialImg) + switchInfo, err := template_switch_info.GetTemplateSwitchInfo(templateInfo.Id, templateInfo.TemplateInfo, *templateInfo.MaterialImg) + if err != nil { + return resp.SetStatusWithMessage(basic.CodeServiceErr, err.Error()) + } + rsp[mapKey] = switchInfo } return resp.SetStatusWithMessage(basic.CodeOK, "success", rsp) } diff --git a/server/websocket/internal/logic/ws_render_image.go b/server/websocket/internal/logic/ws_render_image.go index 6560a00c..524d766c 100644 --- a/server/websocket/internal/logic/ws_render_image.go +++ b/server/websocket/internal/logic/ws_render_image.go @@ -152,19 +152,26 @@ func (w *wsConnectItem) renderImage(data []byte) { logx.Error("failed to get element ,", err) return } - //获取模板开关信息并且对于没有默认值的给赋值默认值 - templateSwitchInfo := template_switch_info.GetTemplateSwitchInfo(productTemplate.Id, *productTemplate.MaterialImg) - if renderImageData.RenderData.Website == "" { - renderImageData.RenderData.Website = templateSwitchInfo.MaterialData.Website.DefaultValue - } - if renderImageData.RenderData.Phone == "" { - renderImageData.RenderData.Phone = templateSwitchInfo.MaterialData.Phone.DefaultValue - } - if renderImageData.RenderData.Address == "" { - renderImageData.RenderData.Address = templateSwitchInfo.MaterialData.Address.DefaultValue - } - if renderImageData.RenderData.Qrcode == "" { - renderImageData.RenderData.Qrcode = templateSwitchInfo.MaterialData.QRcode.DefaultValue + //获取模板开关信息并且对于没有默认值的给赋值默认值(但凡DIY有一个是空的就要请求默认数据) + if renderImageData.RenderData.Website == "" || renderImageData.RenderData.Phone == "" || renderImageData.RenderData.Address == "" || renderImageData.RenderData.Qrcode == "" { + templateSwitchInfo, err := template_switch_info.GetTemplateSwitchInfo(productTemplate.Id, productTemplate.TemplateInfo, *productTemplate.MaterialImg) + if err != nil { + logx.Error(err) + w.renderErrResponse(renderImageData.RenderId, renderImageData.RenderData.TemplateTag, "", err.Error(), renderImageData.RenderData.ProductId, w.userId, w.guestId, productTemplate.Id, model3dInfo.Id, productSize.Id, *productTemplate.ElementModelId) + return + } + if renderImageData.RenderData.Website == "" { + renderImageData.RenderData.Website = templateSwitchInfo.MaterialData.Website.DefaultValue + } + if renderImageData.RenderData.Phone == "" { + renderImageData.RenderData.Phone = templateSwitchInfo.MaterialData.Phone.DefaultValue + } + if renderImageData.RenderData.Address == "" { + renderImageData.RenderData.Address = templateSwitchInfo.MaterialData.Address.DefaultValue + } + if renderImageData.RenderData.Qrcode == "" { + renderImageData.RenderData.Qrcode = templateSwitchInfo.MaterialData.QRcode.DefaultValue + } } //获取刀版图 combineReq := repositories.LogoCombineReq{ @@ -278,11 +285,11 @@ func (w *wsConnectItem) getProductRelateionInfo(renderImageData websocket_data.R productTemplate, err = w.logic.svcCtx.AllModels.FsProductTemplateV2.FindOneCloudRenderByProductIdModelIdTemplateTag(w.logic.ctx, renderImageData.RenderData.ProductId, model3d.Id, renderImageData.RenderData.TemplateTag) if err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { - w.renderErrResponse(renderImageData.RenderId, renderImageData.RenderData.TemplateTag, "", "找不到对应开启云渲染的模板", renderImageData.RenderData.ProductId, w.userId, w.guestId, 0, model3d.Id, productSize.Id, 0) + w.renderErrResponse(renderImageData.RenderId, renderImageData.RenderData.TemplateTag, "", "找不到对应的模板", renderImageData.RenderData.ProductId, w.userId, w.guestId, 0, model3d.Id, productSize.Id, 0) logx.Error("template info is not found") return nil, nil, nil, err } - w.renderErrResponse(renderImageData.RenderId, renderImageData.RenderData.TemplateTag, "", "获取对应开启云渲染模板失败", renderImageData.RenderData.ProductId, w.userId, w.guestId, 0, model3d.Id, productSize.Id, 0) + w.renderErrResponse(renderImageData.RenderId, renderImageData.RenderData.TemplateTag, "", "获取对应模板失败", renderImageData.RenderData.ProductId, w.userId, w.guestId, 0, model3d.Id, productSize.Id, 0) logx.Error("failed to get template info:", err) return nil, nil, nil, err } diff --git a/server/fssql/metadata.go b/utils/fssql/metadata.go similarity index 53% rename from server/fssql/metadata.go rename to utils/fssql/metadata.go index bc97da9d..143354c0 100644 --- a/server/fssql/metadata.go +++ b/utils/fssql/metadata.go @@ -47,7 +47,7 @@ import ( // } // tableStructPointer 表结构 updateMetadata 可以是json的[]byte也可以结构体 -func MetadataPATCH(tx *gorm.DB, module string, tableStructPointer any, updateMetadata any, WhereKeysCond string, value ...any) error { +func MetadataModulePATCH(tx *gorm.DB, module string, tableStructPointer any, updateMetadata any, WhereKeysCond string, values ...any) error { stype := reflect.TypeOf(tableStructPointer) if stype.Kind() == reflect.Pointer { stype = stype.Elem() @@ -75,14 +75,73 @@ func MetadataPATCH(tx *gorm.DB, module string, tableStructPointer any, updateMet } } - var values []any + var args []any - values = append(values, metadata, metadata) - values = append(values, value...) + args = append(args, metadata, metadata) + args = append(args, values...) updatesql = fmt.Sprintf(updatesql, tx.NamingStrategy.TableName(stype.Name()), module, WhereKeysCond) // logx.Error(updatesql) - err = tx.Exec(updatesql, values).Error + err = tx.Exec(updatesql, args...).Error + if err != nil { + return err + } + + return nil +} + +func MetadataResourcePATCH(tx *gorm.DB, rid string, updateMetadata any, values ...any) error { + // stype := reflect.TypeOf(tableStructPointer) + // if stype.Kind() == reflect.Pointer { + // stype = stype.Elem() + // } + + var updatesql string + + if len(values) == 0 { + updatesql = `UPDATE fs_resource + SET metadata = CASE + WHEN metadata IS NULL THEN ? + ELSE JSON_MERGE_PATCH(metadata, ?) + END + WHERE resource_id = '%s';` + } else { + cond, ok := values[0].(string) + if !ok { + return fmt.Errorf("values[0] must be wherecond") + } + updatesql = `UPDATE fs_resource + SET metadata = CASE + WHEN metadata IS NULL THEN ? + ELSE JSON_MERGE_PATCH(metadata, ?) + END + cond, ok := values[0].(string) + WHERE resource_id = '%s'` + " and " + cond + `;` + } + + var err error + var metadata []byte + + switch mdata := updateMetadata.(type) { + case []byte: + metadata = mdata + case string: + metadata = []byte(mdata) + default: + metadata, err = json.Marshal(updateMetadata) + if err != nil { + return err + } + } + + var args []any + + args = append(args, metadata, metadata) + args = append(args, values...) + + updatesql = fmt.Sprintf(updatesql, rid) + // logx.Error(updatesql) + err = tx.Exec(updatesql, args...).Error if err != nil { return err } diff --git a/utils/fssql/metadata_test.go b/utils/fssql/metadata_test.go new file mode 100644 index 00000000..4c12011d --- /dev/null +++ b/utils/fssql/metadata_test.go @@ -0,0 +1,28 @@ +package fssql_test + +import ( + "fusenapi/initalize" + "fusenapi/model/gmodel" + "fusenapi/utils/fssql" + "testing" +) + +func TestCase1(t *testing.T) { + u := gmodel.UserProfile{ + FirstName: "h", + LastName: "sm", + } + + conn := initalize.InitMysql("fsreaderwriter:XErSYmLELKMnf3Dh@tcp(fusen.cdmigcvz3rle.us-east-2.rds.amazonaws.com:3306)/fusen") + err := fssql.MetadataModulePATCH(conn, "logo", gmodel.FsChangeCode{}, u, "id = ?", 6) + if err != nil { + panic(err) + } + + err = fssql.MetadataResourcePATCH(conn, + "f8932c0379fa5aa3397dc0a963696ca90536cc273ea10119b0137fd15ecfe673", u) + + if err != nil { + panic(err) + } +} diff --git a/utils/template_switch_info/template_switch.go b/utils/template_switch_info/template_switch.go index c262baa6..6e903ab3 100644 --- a/utils/template_switch_info/template_switch.go +++ b/utils/template_switch_info/template_switch.go @@ -1,5 +1,11 @@ package template_switch_info +import ( + "encoding/json" + "errors" + "github.com/zeromicro/go-zero/core/logx" +) + type GetTemplateSwitchInfoRsp struct { Id int64 `json:"id"` Material string `json:"material"` @@ -36,36 +42,72 @@ type Logo struct { Material string `json:"material"` } +// 模板开关信息简单结构 +type TemplateSimpleParseInfo struct { + MaterialList []MaterialItem `json:"materialList"` +} +type MaterialItem struct { + Type string `json:"type"` + Tag string `json:"tag"` + Visible bool `json:"visible"` + Text string `json:"text"` +} + // 获取模板开关信息(目前写死,以后后台做好了功能再更新变动) -func GetTemplateSwitchInfo(templateId int64, templateMaterialImg string) GetTemplateSwitchInfoRsp { - return GetTemplateSwitchInfoRsp{ +func GetTemplateSwitchInfo(templateId int64, templateJsonStr *string, templateMaterialImg string) (resp GetTemplateSwitchInfoRsp, err error) { + if templateJsonStr == nil || *templateJsonStr == "" { + return GetTemplateSwitchInfoRsp{}, nil + } + var templateJsonInfo TemplateSimpleParseInfo + if err = json.Unmarshal([]byte(*templateJsonStr), &templateJsonInfo); err != nil { + logx.Error(err) + return GetTemplateSwitchInfoRsp{}, errors.New("解析模板json获取DIY开关设置失败") + } + mapSwitchInfo := GetTemplateSwitchInfoRsp{ Id: templateId, Material: templateMaterialImg, MaterialData: MaterialData{ - QRcode: QRcode{ - IfShow: true, - Text: "qrcode", - DefaultValue: "default qrcode", - }, - Website: Website{ - IfShow: true, - Text: "website", - DefaultValue: "default website", - }, - Address: Address{ - IfShow: true, - Text: "address", - DefaultValue: "default address", - }, - Phone: Phone{ - IfShow: true, - Text: "phone", - DefaultValue: "17557283679", - }, Logo: Logo{ Material: "/image/logo/aHnT1_rzubdwax_scale.png", }, }, } - + for _, v := range templateJsonInfo.MaterialList { + if v.Type == "combine" && !v.Visible { + return GetTemplateSwitchInfoRsp{ + MaterialData: MaterialData{ + Logo: Logo{ + Material: "/image/logo/aHnT1_rzubdwax_scale.png", + }, + }, + }, nil + } + switch v.Tag { + case "Phone": //电话 + mapSwitchInfo.MaterialData.Phone = Phone{ + IfShow: v.Visible, + Text: v.Text, + DefaultValue: "xxx xxx xxx xxxx", + } + case "Address": //地址 + mapSwitchInfo.MaterialData.Address = Address{ + IfShow: v.Visible, + Text: v.Text, + DefaultValue: "USA", + } + case "Website": + mapSwitchInfo.MaterialData.Website = Website{ + IfShow: v.Visible, + Text: v.Text, + DefaultValue: "https://www.xxxxxx.com", + } + case "QRcode": + mapSwitchInfo.MaterialData.QRcode = QRcode{ + IfShow: v.Visible, + Text: v.Text, + DefaultValue: "xxxxxxx", + } + } + } + return mapSwitchInfo, nil }