From bdd9a70a2dedef60edda6edea1a6281488d31c16 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Mon, 14 Aug 2023 14:21:14 +0800 Subject: [PATCH 01/12] fix --- server/product/internal/logic/gettemplatebypidlogic.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/server/product/internal/logic/gettemplatebypidlogic.go b/server/product/internal/logic/gettemplatebypidlogic.go index e036fa15..d13dbd0a 100644 --- a/server/product/internal/logic/gettemplatebypidlogic.go +++ b/server/product/internal/logic/gettemplatebypidlogic.go @@ -88,7 +88,7 @@ func (l *GetTemplateByPidLogic) GetTemplateByPid(req *types.GetTemplateByPidReq, logx.Error(err) return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get template list") } - rsp := make(map[string][]interface{}) + rsp := make(map[string]interface{}) for _, templateInfo := range templateList { //没有设置模板据不要 if templateInfo.TemplateInfo == nil || *templateInfo.TemplateInfo == "" { @@ -115,11 +115,11 @@ func (l *GetTemplateByPidLogic) GetTemplateByPid(req *types.GetTemplateByPidReq, material = info["module_data"]["material"] } mapKey := fmt.Sprintf("_%d", *modelInfo.SizeId) - rsp[mapKey] = append(rsp[mapKey], map[string]interface{}{ + rsp[mapKey] = map[string]interface{}{ "id": templateInfo.Id, "material": material, "material_data": switchInfo, - }) + } } return resp.SetStatusWithMessage(basic.CodeOK, "success", rsp) } From 57e635c26cc08c8c2d179477addcfb2caac5a7f4 Mon Sep 17 00:00:00 2001 From: Hiven Date: Mon, 14 Aug 2023 14:40:50 +0800 Subject: [PATCH 02/12] =?UTF-8?q?feat:=E6=96=B0=E5=A2=9E=E5=95=86=E6=88=B7?= =?UTF-8?q?=E5=88=97=E8=A1=A8/=E4=BF=9D=E5=AD=98=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- model/gmodel/fs_merchant_category_gen.go | 25 +++++ model/gmodel/fs_merchant_category_logic.go | 45 +++++++++ model/gmodel/fs_product_template_v2_gen.go | 15 +-- model/gmodel/fs_user_info_gen.go | 25 +++++ model/gmodel/fs_user_info_logic.go | 42 ++++++++ model/gmodel/var_gen.go | 4 + server/base/base.go | 36 +++++++ server/base/base_test.go | 9 ++ server/base/etc/base.yaml | 10 ++ server/base/internal/config/config.go | 14 +++ .../handler/merchantcategorylisthandler.go | 35 +++++++ server/base/internal/handler/routes.go | 22 +++++ .../logic/merchantcategorylistlogic.go | 53 +++++++++++ server/base/internal/svc/servicecontext.go | 62 ++++++++++++ server/base/internal/types/types.go | 78 +++++++++++++++ .../home-user-auth/internal/handler/routes.go | 5 + .../internal/handler/userinfosethandler.go | 35 +++++++ .../internal/logic/userinfosetlogic.go | 95 +++++++++++++++++++ server/home-user-auth/internal/types/types.go | 5 + server_api/base.api | 20 ++++ server_api/home-user-auth.api | 11 +++ utils/validate/user_info.go | 26 +++++ 22 files changed, 665 insertions(+), 7 deletions(-) create mode 100644 model/gmodel/fs_merchant_category_gen.go create mode 100644 model/gmodel/fs_merchant_category_logic.go create mode 100644 model/gmodel/fs_user_info_gen.go create mode 100644 model/gmodel/fs_user_info_logic.go create mode 100644 server/base/base.go create mode 100644 server/base/base_test.go create mode 100644 server/base/etc/base.yaml create mode 100644 server/base/internal/config/config.go create mode 100644 server/base/internal/handler/merchantcategorylisthandler.go create mode 100644 server/base/internal/handler/routes.go create mode 100644 server/base/internal/logic/merchantcategorylistlogic.go create mode 100644 server/base/internal/svc/servicecontext.go create mode 100644 server/base/internal/types/types.go create mode 100644 server/home-user-auth/internal/handler/userinfosethandler.go create mode 100644 server/home-user-auth/internal/logic/userinfosetlogic.go create mode 100644 server_api/base.api create mode 100644 utils/validate/user_info.go diff --git a/model/gmodel/fs_merchant_category_gen.go b/model/gmodel/fs_merchant_category_gen.go new file mode 100644 index 00000000..37944dce --- /dev/null +++ b/model/gmodel/fs_merchant_category_gen.go @@ -0,0 +1,25 @@ +package gmodel + +import ( + "gorm.io/gorm" +) + +// fs_merchant_category 商户类型表 +type FsMerchantCategory struct { + Id int64 `gorm:"primary_key;default:0;auto_increment;" json:"id"` // Id + ZnName *string `gorm:"default:'';" json:"zn_name"` // 中文名 + EnName *string `gorm:"default:'';" json:"en_name"` // 英文名 + Icon *string `gorm:"default:'';" json:"icon"` // 图标 + RecommendProduct *string `gorm:"default:'';" json:"recommend_product"` // 推荐商品 + Sort *int64 `gorm:"default:128;" json:"sort"` // 排序 + Status *int64 `gorm:"default:0;" json:"status"` // 状态 + Ctime *int64 `gorm:"default:0;" json:"ctime"` // 添加时间 +} +type FsMerchantCategoryModel struct { + db *gorm.DB + name string +} + +func NewFsMerchantCategoryModel(db *gorm.DB) *FsMerchantCategoryModel { + return &FsMerchantCategoryModel{db: db, name: "fs_merchant_category"} +} diff --git a/model/gmodel/fs_merchant_category_logic.go b/model/gmodel/fs_merchant_category_logic.go new file mode 100644 index 00000000..bb6ef130 --- /dev/null +++ b/model/gmodel/fs_merchant_category_logic.go @@ -0,0 +1,45 @@ +package gmodel + +import ( + "context" + "fusenapi/utils/handlers" + "reflect" + + "gorm.io/gorm" +) + +// TODO: 使用model的属性做你想做的 + +func (m *FsMerchantCategoryModel) BuilderDB(ctx context.Context, selectData []string) *gorm.DB { + if selectData != nil { + return m.db.WithContext(ctx).Select(selectData) + } else { + return m.db.WithContext(ctx).Select("*") + } +} + +func (m *FsMerchantCategoryModel) FindAll(gormDB *gorm.DB, filterMap map[string]string, orderBy string) ([]*FsMerchantCategory, error) { + var resp []*FsMerchantCategory + + // 过滤 + if filterMap != nil { + gormDB = gormDB.Scopes(handlers.FilterData(filterMap)) + } + + // 排序 + if orderBy != "" { + var fieldsMap = make(map[string]struct{}) + s := reflect.TypeOf(&FsOrder{}).Elem() //通过反射获取type定义 + for i := 0; i < s.NumField(); i++ { + fieldsMap[s.Field(i).Tag.Get("json")] = struct{}{} + } + gormDB = gormDB.Scopes(handlers.OrderCheck(orderBy, fieldsMap)) + } + + result := gormDB.Find(&resp) + if result.Error != nil { + return nil, result.Error + } else { + return resp, nil + } +} diff --git a/model/gmodel/fs_product_template_v2_gen.go b/model/gmodel/fs_product_template_v2_gen.go index 5e14427f..07ad37fc 100644 --- a/model/gmodel/fs_product_template_v2_gen.go +++ b/model/gmodel/fs_product_template_v2_gen.go @@ -9,20 +9,21 @@ type FsProductTemplateV2 struct { Id int64 `gorm:"primary_key;default:0;auto_increment;" json:"id"` // ProductId *int64 `gorm:"index;default:0;" json:"product_id"` // 产品ID ModelId *int64 `gorm:"default:0;" json:"model_id"` // 模型ID - Title *string `gorm:"default:'';" json:"title"` // 模板(sku),预留字段 - Name *string `gorm:"default:'';" json:"name"` // 名称 - CoverImg *string `gorm:"default:'';" json:"cover_img"` // 模板背景图 - TemplateInfo *string `gorm:"default:'';" json:"template_info"` // 模板详情 - MaterialImg *string `gorm:"default:'';" json:"material_img"` // 合成好的贴图 + Title *string `gorm:"default:'';" json:"title"` // + Name *string `gorm:"default:'';" json:"name"` // + CoverImg *string `gorm:"default:'';" json:"cover_img"` // + TemplateInfo *string `gorm:"default:'';" json:"template_info"` // + MaterialImg *string `gorm:"default:'';" json:"material_img"` // Sort *int64 `gorm:"default:0;" json:"sort"` // 排序 LogoWidth *int64 `gorm:"default:0;" json:"logo_width"` // logo图最大宽度 LogoHeight *int64 `gorm:"default:0;" json:"logo_height"` // logo图最大高度 IsPublic *int64 `gorm:"default:0;" json:"is_public"` // 是否可公用(1:可以,0:不可以) Status *int64 `gorm:"default:0;" json:"status"` // 状态1正常 2异常 Ctime *int64 `gorm:"default:0;" json:"ctime"` // 添加时间 - Tag *string `gorm:"default:'';" json:"tag"` // 标签(用户自填) + Tag *string `gorm:"default:'';" json:"tag"` // IsDel *int64 `gorm:"default:0;" json:"is_del"` // 是否删除 1删除 - GroupOptions *string `gorm:"default:'';" json:"group_options"` // 颜色分组 + SwitchInfo *string `gorm:"default:'';" json:"switch_info"` // + GroupOptions *string `gorm:"default:'';" json:"group_options"` // Version *int64 `gorm:"default:0;" json:"version"` // } type FsProductTemplateV2Model struct { diff --git a/model/gmodel/fs_user_info_gen.go b/model/gmodel/fs_user_info_gen.go new file mode 100644 index 00000000..71b1bd7a --- /dev/null +++ b/model/gmodel/fs_user_info_gen.go @@ -0,0 +1,25 @@ +package gmodel + +import ( + "gorm.io/gorm" +) + +// fs_user_info 用户信息表 +type FsUserInfo struct { + Id int64 `gorm:"primary_key;default:0;auto_increment;" json:"id"` // ID + Module *string `gorm:"default:'';" json:"module"` // 所属模块 + UserId *int64 `gorm:"index;default:0;" json:"user_id"` // 用户 ID + GuestId *int64 `gorm:"index;default:0;" json:"guest_id"` // 访客 ID + Metadata *string `gorm:"default:'';" json:"metadata"` // 元数据,json格式 + MetaKey1 *string `gorm:"default:'';" json:"meta_key1"` // 需要关键信息查询的自定义属性1,可以动态增加 + Ctime *int64 `gorm:"default:0;" json:"ctime"` // + Utime *int64 `gorm:"default:0;" json:"utime"` // +} +type FsUserInfoModel struct { + db *gorm.DB + name string +} + +func NewFsUserInfoModel(db *gorm.DB) *FsUserInfoModel { + return &FsUserInfoModel{db: db, name: "fs_user_info"} +} diff --git a/model/gmodel/fs_user_info_logic.go b/model/gmodel/fs_user_info_logic.go new file mode 100644 index 00000000..3ca2b8d0 --- /dev/null +++ b/model/gmodel/fs_user_info_logic.go @@ -0,0 +1,42 @@ +package gmodel + +import ( + "context" + "fusenapi/utils/handlers" + + "gorm.io/gorm" +) + +// TODO: 使用model的属性做你想做的 + +func (m *FsUserInfoModel) BuilderDB(ctx context.Context, selectData []string) *gorm.DB { + if selectData != nil { + return m.db.WithContext(ctx).Select(selectData) + } else { + return m.db.WithContext(ctx).Select("*") + } +} + +func (m *FsUserInfoModel) FindOne(gormDB *gorm.DB, filterMap map[string]string) (*FsUserInfo, error) { + var resp FsUserInfo + + if filterMap != nil { + gormDB = gormDB.Scopes(handlers.FilterData(filterMap)) + } + + result := gormDB.Limit(1).Find(&resp) + if result.Error != nil { + return nil, result.Error + } else { + return &resp, nil + } +} + +func (p *FsUserInfoModel) CreateOrUpdate(gormDB *gorm.DB, req *FsUserInfo) (resp *FsUserInfo, err error) { + if req.Id > 0 { + err = gormDB.Save(req).Error + } else { + err = gormDB.Create(req).Error + } + return req, err +} diff --git a/model/gmodel/var_gen.go b/model/gmodel/var_gen.go index 46818f31..d8c7fa82 100644 --- a/model/gmodel/var_gen.go +++ b/model/gmodel/var_gen.go @@ -45,6 +45,7 @@ type AllModelsGen struct { FsLog *FsLogModel // fs_log 日志表 FsMapLibrary *FsMapLibraryModel // fs_map_library 贴图库 FsMenu *FsMenuModel // fs_menu 后台菜单 + FsMerchantCategory *FsMerchantCategoryModel // fs_merchant_category 商户类型表 FsMigration *FsMigrationModel // fs_migration 版本库 FsOrder *FsOrderModel // fs_order FsOrderAffiliate *FsOrderAffiliateModel // fs_order_affiliate 订单附属表-流程控制时间等 @@ -91,6 +92,7 @@ type AllModelsGen struct { FsTrade *FsTradeModel // fs_trade FsUser *FsUserModel // fs_user 用户表 FsUserDesign *FsUserDesignModel // fs_user_design 废弃表 + FsUserInfo *FsUserInfoModel // fs_user_info 用户信息表 FsUserMaterial *FsUserMaterialModel // fs_user_material 用户素材表 FsUserStock *FsUserStockModel // fs_user_stock 用户云仓库存 FsWebSet *FsWebSetModel // fs_web_set 网站配置表 @@ -140,6 +142,7 @@ func NewAllModels(gdb *gorm.DB) *AllModelsGen { FsLog: NewFsLogModel(gdb), FsMapLibrary: NewFsMapLibraryModel(gdb), FsMenu: NewFsMenuModel(gdb), + FsMerchantCategory: NewFsMerchantCategoryModel(gdb), FsMigration: NewFsMigrationModel(gdb), FsOrder: NewFsOrderModel(gdb), FsOrderAffiliate: NewFsOrderAffiliateModel(gdb), @@ -186,6 +189,7 @@ func NewAllModels(gdb *gorm.DB) *AllModelsGen { FsTrade: NewFsTradeModel(gdb), FsUser: NewFsUserModel(gdb), FsUserDesign: NewFsUserDesignModel(gdb), + FsUserInfo: NewFsUserInfoModel(gdb), FsUserMaterial: NewFsUserMaterialModel(gdb), FsUserStock: NewFsUserStockModel(gdb), FsWebSet: NewFsWebSetModel(gdb), diff --git a/server/base/base.go b/server/base/base.go new file mode 100644 index 00000000..df2962f3 --- /dev/null +++ b/server/base/base.go @@ -0,0 +1,36 @@ +package main + +import ( + "flag" + "fmt" + "net/http" + "time" + + "fusenapi/utils/auth" + + "fusenapi/server/base/internal/config" + "fusenapi/server/base/internal/handler" + "fusenapi/server/base/internal/svc" + + "github.com/zeromicro/go-zero/core/conf" + "github.com/zeromicro/go-zero/rest" +) + +var configFile = flag.String("f", "etc/base.yaml", "the config file") + +func main() { + flag.Parse() + + var c config.Config + conf.MustLoad(*configFile, &c) + c.Timeout = int64(time.Second * 15) + server := rest.MustNewServer(c.RestConf, rest.WithCustomCors(auth.FsCors, func(w http.ResponseWriter) { + })) + defer server.Stop() + + ctx := svc.NewServiceContext(c) + handler.RegisterHandlers(server, ctx) + + fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port) + server.Start() +} diff --git a/server/base/base_test.go b/server/base/base_test.go new file mode 100644 index 00000000..80820ef2 --- /dev/null +++ b/server/base/base_test.go @@ -0,0 +1,9 @@ +package main + +import ( + "testing" +) + +func TestMain(t *testing.T) { + main() +} diff --git a/server/base/etc/base.yaml b/server/base/etc/base.yaml new file mode 100644 index 00000000..e201a85b --- /dev/null +++ b/server/base/etc/base.yaml @@ -0,0 +1,10 @@ +Name: base +Host: 0.0.0.0 +Port: 9920 +Timeout: 15000 #服务超时时间(毫秒) +SourceMysql: fusentest:XErSYmLELKMnf3Dh@tcp(110.41.19.98:3306)/fusentest +Auth: + AccessSecret: fusen2023 + AccessExpire: 2592000 + RefreshAfter: 1592000 +SourceRabbitMq: amqp://rabbit001:rabbit001129@110.41.19.98:5672 \ No newline at end of file diff --git a/server/base/internal/config/config.go b/server/base/internal/config/config.go new file mode 100644 index 00000000..be42afd5 --- /dev/null +++ b/server/base/internal/config/config.go @@ -0,0 +1,14 @@ +package config + +import ( + "fusenapi/server/base/internal/types" + + "github.com/zeromicro/go-zero/rest" +) + +type Config struct { + rest.RestConf + SourceMysql string + Auth types.Auth + SourceRabbitMq string +} diff --git a/server/base/internal/handler/merchantcategorylisthandler.go b/server/base/internal/handler/merchantcategorylisthandler.go new file mode 100644 index 00000000..c7b4484a --- /dev/null +++ b/server/base/internal/handler/merchantcategorylisthandler.go @@ -0,0 +1,35 @@ +package handler + +import ( + "net/http" + "reflect" + + "fusenapi/utils/basic" + + "fusenapi/server/base/internal/logic" + "fusenapi/server/base/internal/svc" + "fusenapi/server/base/internal/types" +) + +func MerchantCategoryListHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + + var req types.MerchantCategoryListReq + userinfo, err := basic.RequestParse(w, r, svcCtx, &req) + if err != nil { + return + } + + // 创建一个业务逻辑层实例 + l := logic.NewMerchantCategoryListLogic(r.Context(), svcCtx) + + rl := reflect.ValueOf(l) + basic.BeforeLogic(w, r, rl) + + resp := l.MerchantCategoryList(&req, userinfo) + + if !basic.AfterLogic(w, r, rl, resp) { + basic.NormalAfterLogic(w, r, resp) + } + } +} diff --git a/server/base/internal/handler/routes.go b/server/base/internal/handler/routes.go new file mode 100644 index 00000000..4f4764c0 --- /dev/null +++ b/server/base/internal/handler/routes.go @@ -0,0 +1,22 @@ +// Code generated by goctl. DO NOT EDIT. +package handler + +import ( + "net/http" + + "fusenapi/server/base/internal/svc" + + "github.com/zeromicro/go-zero/rest" +) + +func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { + server.AddRoutes( + []rest.Route{ + { + Method: http.MethodGet, + Path: "/api/base/merchant_category_list", + Handler: MerchantCategoryListHandler(serverCtx), + }, + }, + ) +} diff --git a/server/base/internal/logic/merchantcategorylistlogic.go b/server/base/internal/logic/merchantcategorylistlogic.go new file mode 100644 index 00000000..eb9dd3ae --- /dev/null +++ b/server/base/internal/logic/merchantcategorylistlogic.go @@ -0,0 +1,53 @@ +package logic + +import ( + "fusenapi/model/gmodel" + "fusenapi/utils/auth" + "fusenapi/utils/basic" + + "context" + + "fusenapi/server/base/internal/svc" + "fusenapi/server/base/internal/types" + + "github.com/zeromicro/go-zero/core/logx" +) + +type MerchantCategoryListLogic struct { + logx.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewMerchantCategoryListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *MerchantCategoryListLogic { + return &MerchantCategoryListLogic{ + Logger: logx.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +// 处理进入前逻辑w,r +// func (l *MerchantCategoryListLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) { +// } + +// 处理逻辑后 w,r 如:重定向, resp 必须重新处理 +// func (l *MerchantCategoryListLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) { +// // httpx.OkJsonCtx(r.Context(), w, resp) +// } + +func (l *MerchantCategoryListLogic) MerchantCategoryList(req *types.MerchantCategoryListReq, userinfo *auth.UserInfo) (resp *basic.Response) { + // 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data) + // userinfo 传入值时, 一定不为null + fsMerchantCategoryModel := gmodel.NewFsMerchantCategoryModel(l.svcCtx.MysqlConn) + BuilderDB := fsMerchantCategoryModel.BuilderDB(l.ctx, nil).Model(&gmodel.FsMerchantCategory{}) + resourceInfo, err := fsMerchantCategoryModel.FindAll(BuilderDB, nil, "sort desc") + if err != nil { + logx.Error(err) + return resp.SetStatus(basic.CodeDbSqlErr, "MerchantCategoryList error system failed") + } + // 返回成功 + return resp.SetStatus(basic.CodeOK, map[string]interface{}{ + "list": resourceInfo, + }) +} diff --git a/server/base/internal/svc/servicecontext.go b/server/base/internal/svc/servicecontext.go new file mode 100644 index 00000000..335cab29 --- /dev/null +++ b/server/base/internal/svc/servicecontext.go @@ -0,0 +1,62 @@ +package svc + +import ( + "errors" + "fmt" + "fusenapi/server/base/internal/config" + "net/http" + + "fusenapi/initalize" + "fusenapi/model/gmodel" + + "github.com/golang-jwt/jwt" + "gorm.io/gorm" +) + +type ServiceContext struct { + Config config.Config + + MysqlConn *gorm.DB + AllModels *gmodel.AllModelsGen + RabbitMq *initalize.RabbitMqHandle +} + +func NewServiceContext(c config.Config) *ServiceContext { + return &ServiceContext{ + Config: c, + MysqlConn: initalize.InitMysql(c.SourceMysql), + AllModels: gmodel.NewAllModels(initalize.InitMysql(c.SourceMysql)), + RabbitMq: initalize.InitRabbitMq(c.SourceRabbitMq, nil), + } +} + +func (svcCtx *ServiceContext) ParseJwtToken(r *http.Request) (jwt.MapClaims, error) { + AuthKey := r.Header.Get("Authorization") + if AuthKey == "" { + return nil, nil + } + AuthKey = AuthKey[7:] + + if len(AuthKey) <= 50 { + return nil, errors.New(fmt.Sprint("Error parsing token, len:", len(AuthKey))) + } + + token, err := jwt.Parse(AuthKey, func(token *jwt.Token) (interface{}, error) { + // 检查签名方法是否为 HS256 + if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok { + return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"]) + } + // 返回用于验证签名的密钥 + return []byte(svcCtx.Config.Auth.AccessSecret), nil + }) + if err != nil { + return nil, errors.New(fmt.Sprint("Error parsing token:", err)) + } + + // 验证成功返回 + if claims, ok := token.Claims.(jwt.MapClaims); ok && token.Valid { + return claims, nil + } + + return nil, errors.New(fmt.Sprint("Invalid token", err)) +} diff --git a/server/base/internal/types/types.go b/server/base/internal/types/types.go new file mode 100644 index 00000000..afa558e9 --- /dev/null +++ b/server/base/internal/types/types.go @@ -0,0 +1,78 @@ +// Code generated by goctl. DO NOT EDIT. +package types + +import ( + "fusenapi/utils/basic" +) + +type MerchantCategoryListReq struct { +} + +type Request struct { +} + +type Response struct { + Code int `json:"code"` + Message string `json:"msg"` + Data interface{} `json:"data"` +} + +type Auth struct { + AccessSecret string `json:"accessSecret"` + AccessExpire int64 `json:"accessExpire"` + RefreshAfter int64 `json:"refreshAfter"` +} + +type File struct { + Filename string `fsfile:"filename"` + Header map[string][]string `fsfile:"header"` + Size int64 `fsfile:"size"` + Data []byte `fsfile:"data"` +} + +type Meta struct { + TotalCount int64 `json:"totalCount"` + PageCount int64 `json:"pageCount"` + CurrentPage int `json:"currentPage"` + PerPage int `json:"perPage"` +} + +// Set 设置Response的Code和Message值 +func (resp *Response) Set(Code int, Message string) *Response { + return &Response{ + Code: Code, + Message: Message, + } +} + +// Set 设置整个Response +func (resp *Response) SetWithData(Code int, Message string, Data interface{}) *Response { + return &Response{ + Code: Code, + Message: Message, + Data: Data, + } +} + +// SetStatus 设置默认StatusResponse(内部自定义) 默认msg, 可以带data, data只使用一个参数 +func (resp *Response) SetStatus(sr *basic.StatusResponse, data ...interface{}) *Response { + newResp := &Response{ + Code: sr.Code, + } + if len(data) == 1 { + newResp.Data = data[0] + } + return newResp +} + +// SetStatusWithMessage 设置默认StatusResponse(内部自定义) 非默认msg, 可以带data, data只使用一个参数 +func (resp *Response) SetStatusWithMessage(sr *basic.StatusResponse, msg string, data ...interface{}) *Response { + newResp := &Response{ + Code: sr.Code, + Message: msg, + } + if len(data) == 1 { + newResp.Data = data[0] + } + return newResp +} diff --git a/server/home-user-auth/internal/handler/routes.go b/server/home-user-auth/internal/handler/routes.go index 607e6278..c6957627 100644 --- a/server/home-user-auth/internal/handler/routes.go +++ b/server/home-user-auth/internal/handler/routes.go @@ -102,6 +102,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { Path: "/api/user/one-more-order", Handler: UserAgainOrderHandler(serverCtx), }, + { + Method: http.MethodPost, + Path: "/api/user/set_user_info", + Handler: UserInfoSetHandler(serverCtx), + }, }, ) } diff --git a/server/home-user-auth/internal/handler/userinfosethandler.go b/server/home-user-auth/internal/handler/userinfosethandler.go new file mode 100644 index 00000000..8593b501 --- /dev/null +++ b/server/home-user-auth/internal/handler/userinfosethandler.go @@ -0,0 +1,35 @@ +package handler + +import ( + "net/http" + "reflect" + + "fusenapi/utils/basic" + + "fusenapi/server/home-user-auth/internal/logic" + "fusenapi/server/home-user-auth/internal/svc" + "fusenapi/server/home-user-auth/internal/types" +) + +func UserInfoSetHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + + var req types.UserInfoSetReq + userinfo, err := basic.RequestParse(w, r, svcCtx, &req) + if err != nil { + return + } + + // 创建一个业务逻辑层实例 + l := logic.NewUserInfoSetLogic(r.Context(), svcCtx) + + rl := reflect.ValueOf(l) + basic.BeforeLogic(w, r, rl) + + resp := l.UserInfoSet(&req, userinfo) + + if !basic.AfterLogic(w, r, rl, resp) { + basic.NormalAfterLogic(w, r, resp) + } + } +} diff --git a/server/home-user-auth/internal/logic/userinfosetlogic.go b/server/home-user-auth/internal/logic/userinfosetlogic.go new file mode 100644 index 00000000..c27836b8 --- /dev/null +++ b/server/home-user-auth/internal/logic/userinfosetlogic.go @@ -0,0 +1,95 @@ +package logic + +import ( + "fusenapi/model/gmodel" + "fusenapi/utils/auth" + "fusenapi/utils/basic" + "fusenapi/utils/validate" + "time" + + "context" + + "fusenapi/server/home-user-auth/internal/svc" + "fusenapi/server/home-user-auth/internal/types" + + "github.com/zeromicro/go-zero/core/logx" +) + +type UserInfoSetLogic struct { + logx.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewUserInfoSetLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserInfoSetLogic { + return &UserInfoSetLogic{ + Logger: logx.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +// 处理进入前逻辑w,r +// func (l *UserInfoSetLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) { +// } + +// 处理逻辑后 w,r 如:重定向, resp 必须重新处理 +// func (l *UserInfoSetLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) { +// // httpx.OkJsonCtx(r.Context(), w, resp) +// } + +func (l *UserInfoSetLogic) UserInfoSet(req *types.UserInfoSetReq, userinfo *auth.UserInfo) (resp *basic.Response) { + // 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data) + // userinfo 传入值时, 一定不为null + if userinfo.IsOnlooker() { + // 如果是,返回未授权的错误码 + return resp.SetStatus(basic.CodeUnAuth) + } + + var userId int64 + var guestId int64 + + // 检查用户是否是游客 + if userinfo.IsGuest() { + // 如果是,使用游客ID和游客键名格式 + guestId = userinfo.GuestId + } else { + // 否则,使用用户ID和用户键名格式 + userId = userinfo.UserId + } + + _, err := validate.Validate(&req.Module, &req.Metadata) + if err != nil { + logx.Error(err) + return resp.SetStatus(basic.CodeRequestParamsErr, "UserInfoSet error Validate failed") + } + + // 根据模块类型检查数据 + var userInfo = &gmodel.FsUserInfo{} + fsUserInfoModel := gmodel.NewFsUserInfoModel(l.svcCtx.MysqlConn) + BuilderDB := fsUserInfoModel.BuilderDB(l.ctx, nil).Model(&gmodel.FsUserInfo{}) + BuilderDB1 := BuilderDB.Where("module = ?", req.Module).Where("user_id=?", userId).Where("guest_id=?", guestId) + userInfo, err = fsUserInfoModel.FindOne(BuilderDB1, nil) + if err != nil { + logx.Error(err) + return resp.SetStatus(basic.CodeDbSqlErr, "UserInfoSet error system failed") + } + var nowTime = time.Now().Unix() + if userInfo.Id != 0 { + userInfo.Metadata = &req.Metadata + userInfo.Utime = &nowTime + } else { + userInfo.GuestId = &guestId + userInfo.UserId = &userId + userInfo.Module = &req.Module + userInfo.Metadata = &req.Metadata + userInfo.Ctime = &nowTime + } + _, err = fsUserInfoModel.CreateOrUpdate(BuilderDB, userInfo) + if err != nil { + logx.Error(err) + return resp.SetStatus(basic.CodeDbSqlErr, "UserInfoSet error system failed") + } + + return resp.SetStatus(basic.CodeOK) +} diff --git a/server/home-user-auth/internal/types/types.go b/server/home-user-auth/internal/types/types.go index b183ef86..6bbf9f49 100644 --- a/server/home-user-auth/internal/types/types.go +++ b/server/home-user-auth/internal/types/types.go @@ -5,6 +5,11 @@ import ( "fusenapi/utils/basic" ) +type UserInfoSetReq struct { + Module string `form:"module,options=[merchant_category]"` // json格式字符串 + Metadata string `form:"metadata"` // json格式字符串 +} + type UserAgainOrderReq struct { Sn string `form:"sn"` // 订单编号 } diff --git a/server_api/base.api b/server_api/base.api new file mode 100644 index 00000000..3a174324 --- /dev/null +++ b/server_api/base.api @@ -0,0 +1,20 @@ +syntax = "v1" + +info ( + title: // TODO: add title + desc: // TODO: add description + author: "" + email: "" +) + +import "basic.api" + +service base { + @handler MerchantCategoryListHandler + get /api/base/merchant_category_list(MerchantCategoryListReq) returns (response); +} + +type ( + MerchantCategoryListReq { + } +) \ No newline at end of file diff --git a/server_api/home-user-auth.api b/server_api/home-user-auth.api index 3b686163..62ae1f35 100644 --- a/server_api/home-user-auth.api +++ b/server_api/home-user-auth.api @@ -75,8 +75,19 @@ service home-user-auth { // 再来一单 @handler UserAgainOrderHandler get /api/user/one-more-order (UserAgainOrderReq) returns (response); + + // 保存商户信息 + @handler UserInfoSetHandler + post /api/user/set_user_info (UserInfoSetReq) returns (response); } +type ( + UserInfoSetReq { + Module string `form:"module,options=[merchant_category]"` // json格式字符串 + Metadata string `form:"metadata"` // json格式字符串 + } +) + type ( UserAgainOrderReq { Sn string `form:"sn"` // 订单编号 diff --git a/utils/validate/user_info.go b/utils/validate/user_info.go new file mode 100644 index 00000000..9e0342ec --- /dev/null +++ b/utils/validate/user_info.go @@ -0,0 +1,26 @@ +package validate + +import ( + "encoding/json" + "errors" +) + +type MerchantCategory struct { + CategoryId int64 `json:"category_id"` +} + +func Validate(module *string, metadata *string) (interface{}, error) { + if *module == "merchant_category" { + var merchantCategory MerchantCategory + err := json.Unmarshal([]byte(*metadata), &merchantCategory) + if err != nil { + return nil, err + } else { + if merchantCategory.CategoryId == 0 { + return nil, errors.New("merchant_category.category_id is required") + } + return merchantCategory, nil + } + } + return nil, nil +} From b7849935a0f9f70234ca378c58bf2f5c834254a7 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Mon, 14 Aug 2023 15:06:32 +0800 Subject: [PATCH 03/12] fix --- model/gmodel/fs_product_template_tags_gen.go | 3 ++- model/gmodel/var_gen.go | 2 ++ .../internal/logic/getproducttemplatetagslogic.go | 2 +- server/product/internal/logic/gettemplatebypidlogic.go | 6 +----- 4 files changed, 6 insertions(+), 7 deletions(-) diff --git a/model/gmodel/fs_product_template_tags_gen.go b/model/gmodel/fs_product_template_tags_gen.go index 227af16d..07d86482 100644 --- a/model/gmodel/fs_product_template_tags_gen.go +++ b/model/gmodel/fs_product_template_tags_gen.go @@ -8,9 +8,10 @@ import ( type FsProductTemplateTags struct { Id int64 `gorm:"primary_key;default:0;auto_increment;" json:"id"` // ID Title *string `gorm:"default:'';" json:"title"` // 标题 - CoverImg *string `gorm:"default:'';" json:"cover_img"` // 封面图 + Cover *string `gorm:"default:'';" json:"cover"` // 封面图 Status *int64 `gorm:"default:0;" json:"status"` // 状态 1:可用 CreateAt *int64 `gorm:"default:0;" json:"create_at"` // 创建时间 + Groups *string `gorm:"default:'';" json:"groups"` // 分组信息 } type FsProductTemplateTagsModel struct { db *gorm.DB diff --git a/model/gmodel/var_gen.go b/model/gmodel/var_gen.go index b62e2377..831f79d8 100644 --- a/model/gmodel/var_gen.go +++ b/model/gmodel/var_gen.go @@ -91,6 +91,7 @@ type AllModelsGen struct { FsTrade *FsTradeModel // fs_trade FsUser *FsUserModel // fs_user 用户表 FsUserDesign *FsUserDesignModel // fs_user_design 废弃表 + FsUserInfo *FsUserInfoModel // fs_user_info 用户信息表 FsUserMaterial *FsUserMaterialModel // fs_user_material 用户素材表 FsUserStock *FsUserStockModel // fs_user_stock 用户云仓库存 FsWebSet *FsWebSetModel // fs_web_set 网站配置表 @@ -186,6 +187,7 @@ func NewAllModels(gdb *gorm.DB) *AllModelsGen { FsTrade: NewFsTradeModel(gdb), FsUser: NewFsUserModel(gdb), FsUserDesign: NewFsUserDesignModel(gdb), + FsUserInfo: NewFsUserInfoModel(gdb), FsUserMaterial: NewFsUserMaterialModel(gdb), FsUserStock: NewFsUserStockModel(gdb), FsWebSet: NewFsWebSetModel(gdb), diff --git a/server/product-template-tag/internal/logic/getproducttemplatetagslogic.go b/server/product-template-tag/internal/logic/getproducttemplatetagslogic.go index 815ef07c..cb335505 100644 --- a/server/product-template-tag/internal/logic/getproducttemplatetagslogic.go +++ b/server/product-template-tag/internal/logic/getproducttemplatetagslogic.go @@ -49,7 +49,7 @@ func (l *GetProductTemplateTagsLogic) GetProductTemplateTags(req *types.GetProdu list = append(list, types.GetProductTemplateTagsRsp{ Id: v.Id, Tag: *v.Title, - Cover: *v.CoverImg, + Cover: *v.Cover, }) } return resp.SetStatusWithMessage(basic.CodeOK, "success", list) diff --git a/server/product/internal/logic/gettemplatebypidlogic.go b/server/product/internal/logic/gettemplatebypidlogic.go index d13dbd0a..e14a0c88 100644 --- a/server/product/internal/logic/gettemplatebypidlogic.go +++ b/server/product/internal/logic/gettemplatebypidlogic.go @@ -110,14 +110,10 @@ func (l *GetTemplateByPidLogic) GetTemplateByPid(req *types.GetTemplateByPidReq, _ = json.Unmarshal([]byte(*templateInfo.SwitchInfo), &switchInfo) } modelInfo := modelList[modelIndex] - var material interface{} - if info["module_data"] != nil && info["module_data"]["material"] != nil { - material = info["module_data"]["material"] - } mapKey := fmt.Sprintf("_%d", *modelInfo.SizeId) rsp[mapKey] = map[string]interface{}{ "id": templateInfo.Id, - "material": material, + "material": *templateInfo.MaterialImg, "material_data": switchInfo, } } From 81bfb0dda8dac0980219d862d5c7eda568aed6a3 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Mon, 14 Aug 2023 15:07:12 +0800 Subject: [PATCH 04/12] fix --- model/gmodel/fs_user_info_gen.go | 25 +++++++++++++++++++++++++ model/gmodel/fs_user_info_logic.go | 2 ++ 2 files changed, 27 insertions(+) create mode 100644 model/gmodel/fs_user_info_gen.go create mode 100644 model/gmodel/fs_user_info_logic.go diff --git a/model/gmodel/fs_user_info_gen.go b/model/gmodel/fs_user_info_gen.go new file mode 100644 index 00000000..71b1bd7a --- /dev/null +++ b/model/gmodel/fs_user_info_gen.go @@ -0,0 +1,25 @@ +package gmodel + +import ( + "gorm.io/gorm" +) + +// fs_user_info 用户信息表 +type FsUserInfo struct { + Id int64 `gorm:"primary_key;default:0;auto_increment;" json:"id"` // ID + Module *string `gorm:"default:'';" json:"module"` // 所属模块 + UserId *int64 `gorm:"index;default:0;" json:"user_id"` // 用户 ID + GuestId *int64 `gorm:"index;default:0;" json:"guest_id"` // 访客 ID + Metadata *string `gorm:"default:'';" json:"metadata"` // 元数据,json格式 + MetaKey1 *string `gorm:"default:'';" json:"meta_key1"` // 需要关键信息查询的自定义属性1,可以动态增加 + Ctime *int64 `gorm:"default:0;" json:"ctime"` // + Utime *int64 `gorm:"default:0;" json:"utime"` // +} +type FsUserInfoModel struct { + db *gorm.DB + name string +} + +func NewFsUserInfoModel(db *gorm.DB) *FsUserInfoModel { + return &FsUserInfoModel{db: db, name: "fs_user_info"} +} diff --git a/model/gmodel/fs_user_info_logic.go b/model/gmodel/fs_user_info_logic.go new file mode 100644 index 00000000..e68225aa --- /dev/null +++ b/model/gmodel/fs_user_info_logic.go @@ -0,0 +1,2 @@ +package gmodel +// TODO: 使用model的属性做你想做的 \ No newline at end of file From 357f402031d8364314240907e557f98606e592ba Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Mon, 14 Aug 2023 15:18:07 +0800 Subject: [PATCH 05/12] fix --- .../render/consumer/assemble_render_data.go | 27 ++++++++++--------- 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/server/render/consumer/assemble_render_data.go b/server/render/consumer/assemble_render_data.go index bf368975..2eaf2831 100644 --- a/server/render/consumer/assemble_render_data.go +++ b/server/render/consumer/assemble_render_data.go @@ -175,13 +175,13 @@ func getCombineImage(ctx context.Context, svcCtx *svc.ServiceContext, parseInfo return "", errors.New("product template info`template_info is empty") } //反序列化替换其中一些参数 - var combineInfo map[string]interface{} - if err = json.Unmarshal([]byte(*productTemplate.TemplateInfo), &combineInfo); err != nil { + var moduleInfo interface{} + if err = json.Unmarshal([]byte(*productTemplate.TemplateInfo), &moduleInfo); err != nil { logx.Error("failed to parse json:template_info:", err) return "", err } //需要替换的参数 - replaceData := map[string]interface{}{ + paramData := map[string]interface{}{ "logo_url": parseInfo.RenderData.Logo, "website": parseInfo.RenderData.Website, "slogan": parseInfo.RenderData.Slogan, @@ -214,17 +214,20 @@ func getCombineImage(ctx context.Context, svcCtx *svc.ServiceContext, parseInfo return "", err } //赋值 - replaceData["colors"] = materialMetaData["colors"] - replaceData["logo_url"] = materialMetaData["logo_url"] - replaceData["shape"] = materialMetaData["shape"] - replaceData["is_crop"] = materialMetaData["is_crop"] - replaceData["ratio"] = materialMetaData["ratio"] - replaceData["line"] = materialMetaData["line"] - replaceData["other"] = materialMetaData["other"] - replaceData["other1"] = materialMetaData["other1"] + 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["param_data"] = replaceData + combineInfo := map[string]interface{}{ + "module_data": moduleInfo, + "param_data": paramData, + } postData, _ := json.Marshal(combineInfo) //请求合成图片 url := svcCtx.Config.PythonApi.CombineImageUrl From 50da55d7bb9d60b78c67856f1466ef665b8cb009 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Mon, 14 Aug 2023 15:46:28 +0800 Subject: [PATCH 06/12] fix --- server/render/consumer/assemble_render_data.go | 9 +++++---- server/render/etc/render.yaml | 2 +- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/server/render/consumer/assemble_render_data.go b/server/render/consumer/assemble_render_data.go index 2eaf2831..a37a131c 100644 --- a/server/render/consumer/assemble_render_data.go +++ b/server/render/consumer/assemble_render_data.go @@ -17,6 +17,7 @@ import ( "github.com/zeromicro/go-zero/core/logx" "gorm.io/gorm" "io/ioutil" + "os" "strconv" "time" ) @@ -233,9 +234,9 @@ func getCombineImage(ctx context.Context, svcCtx *svc.ServiceContext, parseInfo 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)*/ + /* 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) @@ -252,7 +253,6 @@ func getCombineImage(ctx context.Context, svcCtx *svc.ServiceContext, parseInfo logx.Error("failed to parse python api rsp:", err) return "", err } - //fmt.Println("××××××××××××××××××××:", pythonApiInfo) //上传刀版图 var upload = file.Upload{ Ctx: ctx, @@ -271,5 +271,6 @@ func getCombineImage(ctx context.Context, svcCtx *svc.ServiceContext, parseInfo logx.Error("上传刀版图到s3失败:", err) return "", err } + logx.Info("发送渲染组装数据到unity成功") return uploadRes.ResourceUrl, nil } diff --git a/server/render/etc/render.yaml b/server/render/etc/render.yaml index 7081d46b..2be7fc7d 100644 --- a/server/render/etc/render.yaml +++ b/server/render/etc/render.yaml @@ -14,4 +14,4 @@ AWS: Secret: sjCEv0JxATnPCxno2KNLm0X8oDc7srUR+4vkYhvm Token: PythonApi: #python接口 - CombineImageUrl: http://192.168.1.7:45678/LogoCombine #合成刀版图接口 \ No newline at end of file + CombineImageUrl: http://18.119.109.254:8999/LogoCombine #合成刀版图接口 \ No newline at end of file From 1c03e5bb01594cce2ca47d3463c5e04876f15a6b Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Mon, 14 Aug 2023 15:49:24 +0800 Subject: [PATCH 07/12] fix --- server/render/consumer/assemble_render_data.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/server/render/consumer/assemble_render_data.go b/server/render/consumer/assemble_render_data.go index a37a131c..0953d70b 100644 --- a/server/render/consumer/assemble_render_data.go +++ b/server/render/consumer/assemble_render_data.go @@ -17,7 +17,6 @@ import ( "github.com/zeromicro/go-zero/core/logx" "gorm.io/gorm" "io/ioutil" - "os" "strconv" "time" ) @@ -250,7 +249,7 @@ func getCombineImage(ctx context.Context, svcCtx *svc.ServiceContext, parseInfo } var pythonApiInfo pythonApiRsp if err = json.Unmarshal(bytes, &pythonApiInfo); err != nil { - logx.Error("failed to parse python api rsp:", err) + logx.Error("请求python合图失败,err:", err, " 请求数据:", string(postData)) return "", err } //上传刀版图 From 63a7aa0a3de0f419251974825be2b5f1252a4a15 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Mon, 14 Aug 2023 15:52:10 +0800 Subject: [PATCH 08/12] fix --- server/product/internal/logic/gettemplatebypidlogic.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/product/internal/logic/gettemplatebypidlogic.go b/server/product/internal/logic/gettemplatebypidlogic.go index e14a0c88..9e1b66d1 100644 --- a/server/product/internal/logic/gettemplatebypidlogic.go +++ b/server/product/internal/logic/gettemplatebypidlogic.go @@ -99,7 +99,7 @@ func (l *GetTemplateByPidLogic) GetTemplateByPid(req *types.GetTemplateByPidReq, continue } //基础模板信息 - var info map[string]map[string]interface{} + var info interface{} if err = json.Unmarshal([]byte(*templateInfo.TemplateInfo), &info); err != nil { logx.Error(err) return resp.SetStatusWithMessage(basic.CodeJsonErr, fmt.Sprintf("failed to parse json product template info(may be old data):%d", templateInfo.Id)) From 94a36bd98ed549c4f213186336ed717794ffae2e Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Mon, 14 Aug 2023 15:58:31 +0800 Subject: [PATCH 09/12] fix --- server/render/consumer/assemble_render_data.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/render/consumer/assemble_render_data.go b/server/render/consumer/assemble_render_data.go index 0953d70b..434dbee8 100644 --- a/server/render/consumer/assemble_render_data.go +++ b/server/render/consumer/assemble_render_data.go @@ -55,7 +55,7 @@ func (m *MqConsumerRenderAssemble) Run(ctx context.Context, data []byte) error { return nil //不返回错误就删除消息 } rabbitmq := initalize.RabbitMqHandle{} - //获取模板(产品第一个sku的模板) + //获取模板(模板标签下的对一个物料的的模板) productTemplate, err := svcCtx.AllModels.FsProductTemplateV2.FindOneByProductIdTagIdWithSizeTable(ctx, parseInfo.RenderData.ProductId, fmt.Sprintf("%d", parseInfo.RenderData.TemplateTagId)) if err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { From 24a5bf11a39f5db5db1ac7640a25e7f745bafadb Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Mon, 14 Aug 2023 17:31:57 +0800 Subject: [PATCH 10/12] fix --- .../gmodel/fs_product_template_tags_logic.go | 11 ++ model/gmodel/fs_user_material_logic.go | 14 +- .../logic/getproducttemplatetagslogic.go | 38 ++++- .../render/consumer/assemble_render_data.go | 132 +----------------- utils/websocket_data/render_data.go | 8 +- 5 files changed, 61 insertions(+), 142 deletions(-) diff --git a/model/gmodel/fs_product_template_tags_logic.go b/model/gmodel/fs_product_template_tags_logic.go index 4c26d2fb..5ef7e6ae 100755 --- a/model/gmodel/fs_product_template_tags_logic.go +++ b/model/gmodel/fs_product_template_tags_logic.go @@ -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 +} diff --git a/model/gmodel/fs_user_material_logic.go b/model/gmodel/fs_user_material_logic.go index 98c0bb08..c500f71e 100644 --- a/model/gmodel/fs_user_material_logic.go +++ b/model/gmodel/fs_user_material_logic.go @@ -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 +} diff --git a/server/product-template-tag/internal/logic/getproducttemplatetagslogic.go b/server/product-template-tag/internal/logic/getproducttemplatetagslogic.go index cb335505..21953ee1 100644 --- a/server/product-template-tag/internal/logic/getproducttemplatetagslogic.go +++ b/server/product-template-tag/internal/logic/getproducttemplatetagslogic.go @@ -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") diff --git a/server/render/consumer/assemble_render_data.go b/server/render/consumer/assemble_render_data.go index 434dbee8..06b164ad 100644 --- a/server/render/consumer/assemble_render_data.go +++ b/server/render/consumer/assemble_render_data.go @@ -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 -} diff --git a/utils/websocket_data/render_data.go b/utils/websocket_data/render_data.go index fcec9690..3b004441 100644 --- a/utils/websocket_data/render_data.go +++ b/utils/websocket_data/render_data.go @@ -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发送渲染完的数据 From 05e385313406d4c5cfef26553b17fadb8f026833 Mon Sep 17 00:00:00 2001 From: Hiven Date: Mon, 14 Aug 2023 17:56:06 +0800 Subject: [PATCH 11/12] =?UTF-8?q?fix:=E9=87=8D=E6=9E=84=E5=90=88=E5=9B=BE?= =?UTF-8?q?=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- constants/blm_service.go | 5 + initalize/service.go | 24 +++ server/resource/etc/resource.yaml | 1 + server/resource/internal/config/config.go | 1 + .../internal/logic/logocombinelogic.go | 132 +------------ .../resource/internal/svc/servicecontext.go | 14 +- service/repositories/image_handle.go | 179 ++++++++++++++++++ 7 files changed, 230 insertions(+), 126 deletions(-) create mode 100644 constants/blm_service.go create mode 100644 initalize/service.go create mode 100644 service/repositories/image_handle.go diff --git a/constants/blm_service.go b/constants/blm_service.go new file mode 100644 index 00000000..d4a36c9e --- /dev/null +++ b/constants/blm_service.go @@ -0,0 +1,5 @@ +package constants + +const ( + BLMServiceUrlLogoCombine string = "/LogoCombine" +) diff --git a/initalize/service.go b/initalize/service.go new file mode 100644 index 00000000..7e486360 --- /dev/null +++ b/initalize/service.go @@ -0,0 +1,24 @@ +package initalize + +import ( + "fusenapi/service/repositories" + + "github.com/aws/aws-sdk-go/aws/session" + "gorm.io/gorm" +) + +type Repositories struct { + ImageHandle repositories.ImageHandle +} + +type NewAllRepositorieData struct { + GormDB *gorm.DB + BLMServiceUrl *string + AwsSession *session.Session +} + +func NewAllRepositories(newData *NewAllRepositorieData) *Repositories { + return &Repositories{ + ImageHandle: repositories.NewImageHandle(newData.GormDB, newData.BLMServiceUrl, newData.AwsSession), + } +} diff --git a/server/resource/etc/resource.yaml b/server/resource/etc/resource.yaml index befe8caf..473ac3f3 100644 --- a/server/resource/etc/resource.yaml +++ b/server/resource/etc/resource.yaml @@ -15,6 +15,7 @@ AWS: Secret: sjCEv0JxATnPCxno2KNLm0X8oDc7srUR+4vkYhvm Token: BLMService: + Url: "http://18.119.109.254:8999" LogoCombine: #Url: "http://192.168.1.7:8999/LogoCombine" Url: "http://18.119.109.254:8999/LogoCombine" \ No newline at end of file diff --git a/server/resource/internal/config/config.go b/server/resource/internal/config/config.go index 51aad1c7..e8569f26 100644 --- a/server/resource/internal/config/config.go +++ b/server/resource/internal/config/config.go @@ -21,6 +21,7 @@ type Config struct { } } BLMService struct { + Url string LogoCombine struct { Url string } diff --git a/server/resource/internal/logic/logocombinelogic.go b/server/resource/internal/logic/logocombinelogic.go index 86da8d7c..bad59cb9 100644 --- a/server/resource/internal/logic/logocombinelogic.go +++ b/server/resource/internal/logic/logocombinelogic.go @@ -1,17 +1,9 @@ package logic import ( - "encoding/json" - "errors" - "fusenapi/model/gmodel" + "fusenapi/service/repositories" "fusenapi/utils/auth" "fusenapi/utils/basic" - "fusenapi/utils/curl" - "fusenapi/utils/file" - "fusenapi/utils/hash" - "io" - "strings" - "time" "context" @@ -19,7 +11,6 @@ import ( "fusenapi/server/resource/internal/types" "github.com/zeromicro/go-zero/core/logx" - "gorm.io/gorm" ) type LogoCombineLogic struct { @@ -65,125 +56,22 @@ func (l *LogoCombineLogic) LogoCombine(req *types.LogoCombineReq, userinfo *auth // 否则,使用用户ID和用户键名格式 userId = userinfo.UserId } - - // 根据hash 查询数据资源 - var resourceId string = hash.JsonHashKey(req.ResourceKey) - resourceModel := gmodel.NewFsResourceModel(l.svcCtx.MysqlConn) - resourceInfo, err := resourceModel.FindOneById(l.ctx, resourceId) - if err == nil && resourceInfo.ResourceId != "" { - return resp.SetStatus(basic.CodeOK, map[string]interface{}{ - "resource_id": resourceId, - "resource_url": resourceInfo.ResourceUrl, - "resource_metadata": resourceInfo.Metadata, - }) - } else { - if err != nil { - if !errors.Is(err, gorm.ErrRecordNotFound) { - logx.Error(err) - return resp.SetStatus(basic.CodeDbSqlErr, "LogoCombine error") - } - } - } - - // 没有查到,先根据模版id 查询模版数据 请求算法数据 - productTemplateV2Model := gmodel.NewFsProductTemplateV2Model(l.svcCtx.MysqlConn) - productTemplateV2Info, err := productTemplateV2Model.FindOne(l.ctx, req.TemplateId) - - if err != nil { - logx.Error(err) - return resp.SetStatus(basic.CodeDbSqlErr, "LogoCombine error") - } - var groupOptions map[string]interface{} - if productTemplateV2Info.GroupOptions != nil { - err = json.Unmarshal([]byte(*productTemplateV2Info.GroupOptions), &groupOptions) - - if err != nil { - logx.Error(err) - return resp.SetStatus(basic.CodeDbSqlErr, "LogoCombine error Unmarshal groupOptions") - } - } - - var materialList []interface{} - if productTemplateV2Info.TemplateInfo != nil { - var templateInfo map[string]interface{} - err = json.Unmarshal([]byte(*productTemplateV2Info.TemplateInfo), &templateInfo) - - if err != nil { - logx.Error(err) - return resp.SetStatus(basic.CodeDbSqlErr, "LogoCombine error Unmarshal templateInfo") - } - materialList = templateInfo["materialList"].([]interface{}) - } - - var moduleDataMap = make(map[string]interface{}, 4) - moduleDataMap["id"] = productTemplateV2Info.Id - moduleDataMap["material"] = productTemplateV2Info.MaterialImg - moduleDataMap["groupOptions"] = groupOptions - moduleDataMap["materialList"] = materialList - - var combineParam map[string]interface{} - json.Unmarshal([]byte(req.CombineParam), &combineParam) - var postMap = make(map[string]interface{}, 2) - postMap["module_data"] = moduleDataMap - postMap["param_data"] = combineParam - postMapB, _ := json.Marshal(postMap) - - //result, err := http.Post(l.svcCtx.Config.BLMService.LogoCombine.Url, "application/json", strings.NewReader(string(postMapB))) - var headerData = make(map[string]string, 1) - headerData["Content-Type"] = "application/json" - result, err := curl.ApiCall(l.svcCtx.Config.BLMService.LogoCombine.Url, "POST", headerData, strings.NewReader(string(postMapB)), time.Second*20) - - if err != nil { - logx.Error(err) - return resp.SetStatus(basic.CodeFileLogoCombineErr, "service post fail") - } - defer result.Body.Close() - b, err := io.ReadAll(result.Body) - if err != nil { - logx.Error(err) - return resp.SetStatus(basic.CodeFileLogoCombineErr, "service read fail") - } - ress := string(b) - - if ress == "Internal Server Error" { - return resp.SetStatus(basic.CodeFileLogoCombineErr, "service read fail") - } - - var resultData map[string]interface{} - err = json.Unmarshal(b, &resultData) - if err != nil { - logx.Error(err) - return resp.SetStatus(basic.CodeFileLogoCombineErr, "service read fail") - } - // { - // id: "", - // logo_url:"https://s3.amazon.com/xxxx", - // result: "$saa541afaldjaldjasldjsadjsapsaasda" - // } - var fileBase = resultData["result"] - - // 上传文件 - var upload = file.Upload{ - Ctx: l.ctx, - MysqlConn: l.svcCtx.MysqlConn, - AwsSession: l.svcCtx.AwsSession, - } - uploadRes, err := upload.UploadFileByBase64(&file.UploadBaseReq{ - FileHash: resourceId, - FileData: fileBase.(string), - UploadBucket: 1, - ApiType: 2, + res, err := l.svcCtx.Repositories.ImageHandle.LogoCombine(l.ctx, &repositories.LogoCombineReq{ + ResourceKey: req.ResourceKey, + TemplateId: req.TemplateId, + CombineParam: req.CombineParam, UserId: userId, GuestId: guestId, }) + if err != nil { - logx.Error(err) - return resp.SetStatus(basic.CodeFileLogoCombineErr, "LogoCombine error upload file failed") + return resp.SetStatus(basic.CodeServiceErr) } + // 返回成功的响应和上传URL return resp.SetStatus(basic.CodeOK, map[string]interface{}{ - "resource_id": resourceId, - "resource_url": uploadRes.ResourceUrl, + "resource_id": res.ResourceId, + "resource_url": res.ResourceUrl, "resource_metadata": "", }) } diff --git a/server/resource/internal/svc/servicecontext.go b/server/resource/internal/svc/servicecontext.go index a81a0767..e006fbce 100644 --- a/server/resource/internal/svc/servicecontext.go +++ b/server/resource/internal/svc/servicecontext.go @@ -19,10 +19,11 @@ import ( type ServiceContext struct { Config config.Config - MysqlConn *gorm.DB - AllModels *gmodel.AllModelsGen - RabbitMq *initalize.RabbitMqHandle - AwsSession *session.Session + MysqlConn *gorm.DB + AllModels *gmodel.AllModelsGen + RabbitMq *initalize.RabbitMqHandle + AwsSession *session.Session + Repositories *initalize.Repositories } func NewServiceContext(c config.Config) *ServiceContext { @@ -35,6 +36,11 @@ func NewServiceContext(c config.Config) *ServiceContext { AllModels: gmodel.NewAllModels(initalize.InitMysql(c.SourceMysql)), RabbitMq: initalize.InitRabbitMq(c.SourceRabbitMq, nil), AwsSession: session.Must(session.NewSession(&config)), + Repositories: initalize.NewAllRepositories(&initalize.NewAllRepositorieData{ + GormDB: initalize.InitMysql(c.SourceMysql), + BLMServiceUrl: &c.BLMService.Url, + AwsSession: session.Must(session.NewSession(&config)), + }), } } diff --git a/service/repositories/image_handle.go b/service/repositories/image_handle.go new file mode 100644 index 00000000..5e873b3f --- /dev/null +++ b/service/repositories/image_handle.go @@ -0,0 +1,179 @@ +package repositories + +import ( + "context" + "encoding/json" + "errors" + "fusenapi/constants" + "fusenapi/model/gmodel" + "fusenapi/utils/curl" + "fusenapi/utils/file" + "fusenapi/utils/hash" + "io" + "strings" + "time" + + "github.com/aws/aws-sdk-go/aws/session" + "github.com/zeromicro/go-zero/core/logx" + "gorm.io/gorm" +) + +func NewImageHandle(gormDB *gorm.DB, bLMServiceUrl *string, awsSession *session.Session) ImageHandle { + return &defaultImageHandle{ + MysqlConn: gormDB, + BLMServiceUrl: bLMServiceUrl, + AwsSession: awsSession, + } +} + +type ( + defaultImageHandle struct { + MysqlConn *gorm.DB + BLMServiceUrl *string + AwsSession *session.Session + } + + ImageHandle = interface { + + // logo合图 + LogoCombine(ctx context.Context, in *LogoCombineReq) (*LogoCombineRes, error) + } +) + +/* logo合图 */ +type ( + LogoCombineReq struct { + ResourceKey string `json:"resource_key"` + TemplateId int64 `json:"template_id"` + CombineParam string `json:"combine_param"` + UserId int64 `json:"user_id"` + GuestId int64 `json:"guest_id"` + } + LogoCombineRes struct { + ResourceId string + ResourceUrl *string + Metadata *string + } +) + +func (l *defaultImageHandle) LogoCombine(ctx context.Context, in *LogoCombineReq) (*LogoCombineRes, error) { + // 根据hash 查询数据资源 + var resourceId string = hash.JsonHashKey(in.ResourceKey) + resourceModel := gmodel.NewFsResourceModel(l.MysqlConn) + resourceInfo, err := resourceModel.FindOneById(ctx, resourceId) + if err == nil && resourceInfo.ResourceId != "" { + return &LogoCombineRes{ + ResourceId: resourceId, + ResourceUrl: resourceInfo.ResourceUrl, + }, nil + } else { + if err != nil { + if !errors.Is(err, gorm.ErrRecordNotFound) { + logx.Error(err) + return nil, err + } + } + } + + // 没有查到,先根据模版id 查询模版数据 请求算法数据 + productTemplateV2Model := gmodel.NewFsProductTemplateV2Model(l.MysqlConn) + productTemplateV2Info, err := productTemplateV2Model.FindOne(ctx, in.TemplateId) + + if err != nil { + logx.Error(err) + return nil, err + } + var groupOptions map[string]interface{} + if productTemplateV2Info.GroupOptions != nil { + err = json.Unmarshal([]byte(*productTemplateV2Info.GroupOptions), &groupOptions) + + if err != nil { + logx.Error(err) + return nil, err + } + } + + var materialList []interface{} + if productTemplateV2Info.TemplateInfo != nil { + var templateInfo map[string]interface{} + err = json.Unmarshal([]byte(*productTemplateV2Info.TemplateInfo), &templateInfo) + + if err != nil { + logx.Error(err) + return nil, err + } + materialList = templateInfo["materialList"].([]interface{}) + } + + var moduleDataMap = make(map[string]interface{}, 4) + moduleDataMap["id"] = productTemplateV2Info.Id + moduleDataMap["material"] = productTemplateV2Info.MaterialImg + moduleDataMap["groupOptions"] = groupOptions + moduleDataMap["materialList"] = materialList + + var combineParam map[string]interface{} + json.Unmarshal([]byte(in.CombineParam), &combineParam) + var postMap = make(map[string]interface{}, 2) + postMap["module_data"] = moduleDataMap + postMap["param_data"] = combineParam + postMapB, _ := json.Marshal(postMap) + + var headerData = make(map[string]string, 1) + headerData["Content-Type"] = "application/json" + result, err := curl.ApiCall(*l.BLMServiceUrl+constants.BLMServiceUrlLogoCombine, "POST", headerData, strings.NewReader(string(postMapB)), time.Second*20) + + if err != nil { + logx.Error(err) + return nil, err + } + defer result.Body.Close() + b, err := io.ReadAll(result.Body) + if err != nil { + logx.Error(err) + return nil, err + } + ress := string(b) + + if ress == "Internal Server Error" { + logx.Error(errors.New("BLMService fail Internal Server Error")) + return nil, err + } + + var resultData map[string]interface{} + err = json.Unmarshal(b, &resultData) + if err != nil { + logx.Error(err) + return nil, err + } + // { + // id: "", + // logo_url:"https://s3.amazon.com/xxxx", + // result: "$saa541afaldjaldjasldjsadjsapsaasda" + // } + var fileBase = resultData["result"] + + // 上传文件 + var upload = file.Upload{ + Ctx: ctx, + MysqlConn: l.MysqlConn, + AwsSession: l.AwsSession, + } + uploadRes, err := upload.UploadFileByBase64(&file.UploadBaseReq{ + FileHash: resourceId, + FileData: fileBase.(string), + UploadBucket: 1, + ApiType: 2, + UserId: in.UserId, + GuestId: in.GuestId, + }) + if err != nil { + logx.Error(err) + return nil, err + } + return &LogoCombineRes{ + ResourceId: uploadRes.ResourceId, + ResourceUrl: &uploadRes.ResourceUrl, + }, nil +} + +/* logo合图 */ From f652a98c0e477eff30564a43073517e9c06cb0a8 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Mon, 14 Aug 2023 18:40:34 +0800 Subject: [PATCH 12/12] fix --- .../render/consumer/assemble_render_data.go | 40 +++++++++++++++++++ server/render/etc/render.yaml | 7 +++- server/render/internal/config/config.go | 7 +++- server/render/internal/svc/servicecontext.go | 16 +++++--- 4 files changed, 61 insertions(+), 9 deletions(-) diff --git a/server/render/consumer/assemble_render_data.go b/server/render/consumer/assemble_render_data.go index 06b164ad..85a32996 100644 --- a/server/render/consumer/assemble_render_data.go +++ b/server/render/consumer/assemble_render_data.go @@ -8,10 +8,13 @@ import ( "fusenapi/constants" "fusenapi/initalize" "fusenapi/server/render/internal/svc" + "fusenapi/service/repositories" + "fusenapi/utils/hash" "fusenapi/utils/websocket_data" "github.com/zeromicro/go-zero/core/logx" "gorm.io/gorm" "strconv" + "time" ) // 这里请求的py接口返回数据 @@ -58,8 +61,45 @@ func (m *MqConsumerRenderAssemble) Run(ctx context.Context, data []byte) error { logx.Error("failed to get template info:", err) return nil //不返回错误就删除消息 } + time.Now().UTC() + resourceKey := hash.JsonHashKey(parseInfo) + combineParam := map[string]interface{}{ + "logo_url": parseInfo.RenderData.Logo, + "website": "", + "slogan": "", + "address": "", + "phone": "", + "colors": []string{}, + "template_tagid": "b1a", + "is_crop": false, + "shape": "rectangle", + "ratio": 0, + "line": "", + "other": "", + "other1": "", + } + combineParamBytes, _ := json.Marshal(combineParam) //获取刀版图 + res, err := svcCtx.Repositories.ImageHandle.LogoCombine(ctx, &repositories.LogoCombineReq{ + ResourceKey: resourceKey, + TemplateId: productTemplate.Id, + CombineParam: string(combineParamBytes), + UserId: parseInfo.RenderData.UserId, + GuestId: parseInfo.RenderData.GuestId, + }) + + 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("合成刀版图成功") //获取渲染设置信息 element, err := svcCtx.AllModels.FsProductTemplateElement.FindOneByModelId(ctx, *productTemplate.ModelId) if err != nil { diff --git a/server/render/etc/render.yaml b/server/render/etc/render.yaml index 2be7fc7d..a75711b8 100644 --- a/server/render/etc/render.yaml +++ b/server/render/etc/render.yaml @@ -13,5 +13,8 @@ AWS: AccessKeyID: AKIAZB2JKUXDPNRP4YT2 Secret: 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 Token: -PythonApi: #python接口 - CombineImageUrl: http://18.119.109.254:8999/LogoCombine #合成刀版图接口 \ No newline at end of file +BLMService: + Url: "http://18.119.109.254:8999" + LogoCombine: + #Url: "http://192.168.1.7:8999/LogoCombine" + Url: "http://18.119.109.254:8999/LogoCombine" \ No newline at end of file diff --git a/server/render/internal/config/config.go b/server/render/internal/config/config.go index 3cc5089e..a180c7da 100644 --- a/server/render/internal/config/config.go +++ b/server/render/internal/config/config.go @@ -19,7 +19,10 @@ type Config struct { } } } - PythonApi struct { - CombineImageUrl string //合图url + BLMService struct { + Url string + LogoCombine struct { + Url string + } } } diff --git a/server/render/internal/svc/servicecontext.go b/server/render/internal/svc/servicecontext.go index 08a78653..a0a334b7 100644 --- a/server/render/internal/svc/servicecontext.go +++ b/server/render/internal/svc/servicecontext.go @@ -17,11 +17,12 @@ import ( ) type ServiceContext struct { - Config config.Config - MysqlConn *gorm.DB - AllModels *gmodel.AllModelsGen - RabbitMq *initalize.RabbitMqHandle - AwsSession *session.Session + Config config.Config + MysqlConn *gorm.DB + AllModels *gmodel.AllModelsGen + RabbitMq *initalize.RabbitMqHandle + AwsSession *session.Session + Repositories *initalize.Repositories } func NewServiceContext(c config.Config) *ServiceContext { @@ -34,6 +35,11 @@ func NewServiceContext(c config.Config) *ServiceContext { AllModels: gmodel.NewAllModels(initalize.InitMysql(c.SourceMysql)), RabbitMq: initalize.InitRabbitMq(c.SourceRabbitMq, nil), AwsSession: session.Must(session.NewSession(&config)), + Repositories: initalize.NewAllRepositories(&initalize.NewAllRepositorieData{ + GormDB: initalize.InitMysql(c.SourceMysql), + BLMServiceUrl: &c.BLMService.Url, + AwsSession: session.Must(session.NewSession(&config)), + }), } }