diff --git a/go.mod b/go.mod index 069b20a9..01c94f65 100644 --- a/go.mod +++ b/go.mod @@ -123,5 +123,4 @@ require ( google.golang.org/grpc v1.56.2 // indirect google.golang.org/protobuf v1.31.0 // indirect gopkg.in/yaml.v2 v2.4.0 - gorm.io/datatypes v1.2.0 ) diff --git a/go.sum b/go.sum index cabd1d03..67d9f1b0 100644 --- a/go.sum +++ b/go.sum @@ -986,8 +986,6 @@ gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -gorm.io/datatypes v1.2.0 h1:5YT+eokWdIxhJgWHdrb2zYUimyk0+TaFth+7a0ybzco= -gorm.io/datatypes v1.2.0/go.mod h1:o1dh0ZvjIjhH/bngTpypG6lVRJ5chTBxE09FH/71k04= gorm.io/driver/mysql v1.5.1 h1:WUEH5VF9obL/lTtzjmML/5e6VfFR/788coz2uaVCAZw= gorm.io/driver/mysql v1.5.1/go.mod h1:Jo3Xu7mMhCyj8dlrb3WoCaRd1FhsVh+yMXb1jUInf5o= gorm.io/gorm v1.25.1 h1:nsSALe5Pr+cM3V1qwwQ7rOkw+6UeLrX5O4v3llhHa64= diff --git a/model/gmodel/fs_product_logic.go b/model/gmodel/fs_product_logic.go index 95c301ce..8e7cca3c 100755 --- a/model/gmodel/fs_product_logic.go +++ b/model/gmodel/fs_product_logic.go @@ -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) { diff --git a/model/gmodel/fs_user_material_logic.go b/model/gmodel/fs_user_material_logic.go index c353b9ad..77ae2736 100644 --- a/model/gmodel/fs_user_material_logic.go +++ b/model/gmodel/fs_user_material_logic.go @@ -52,6 +52,25 @@ func (m *FsUserMaterialModel) FindAll(ctx context.Context, rowBuilder *gorm.DB, return resp, nil } } +func (m *FsUserMaterialModel) FindOne(ctx context.Context, rowBuilder *gorm.DB) (*FsUserMaterial, error) { + var resp *FsUserMaterial + result := rowBuilder.WithContext(ctx).First(&resp) + if result.Error != nil { + return nil, result.Error + } else { + return resp, nil + } +} + +func (m *FsUserMaterialModel) FindOneData(ctx context.Context, rowBuilder *gorm.DB) (*RelaFsUserMaterial, error) { + var resp *RelaFsUserMaterial + result := rowBuilder.WithContext(ctx).First(&resp) + if result.Error != nil { + return nil, result.Error + } else { + return resp, nil + } +} func (m *FsUserMaterialModel) FindList(ctx context.Context, rowBuilder *gorm.DB, filterMap map[string]string, orderBy string) ([]*RelaFsUserMaterial, error) { var resp []*RelaFsUserMaterial diff --git a/server/home-user-auth/internal/handler/routes.go b/server/home-user-auth/internal/handler/routes.go index 9fbecfd9..1116922b 100644 --- a/server/home-user-auth/internal/handler/routes.go +++ b/server/home-user-auth/internal/handler/routes.go @@ -87,6 +87,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { Path: "/api/user/set_user_info", Handler: UserInfoSetHandler(serverCtx), }, + { + Method: http.MethodPost, + Path: "/api/user/logo-set", + Handler: UserLogoSetHandler(serverCtx), + }, }, ) } diff --git a/server/home-user-auth/internal/handler/userlogosethandler.go b/server/home-user-auth/internal/handler/userlogosethandler.go new file mode 100644 index 00000000..26f953c8 --- /dev/null +++ b/server/home-user-auth/internal/handler/userlogosethandler.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 UserLogoSetHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + + var req types.UserLogoSetReq + userinfo, err := basic.RequestParse(w, r, svcCtx, &req) + if err != nil { + return + } + + // 创建一个业务逻辑层实例 + l := logic.NewUserLogoSetLogic(r.Context(), svcCtx) + + rl := reflect.ValueOf(l) + basic.BeforeLogic(w, r, rl) + + resp := l.UserLogoSet(&req, userinfo) + + if !basic.AfterLogic(w, r, rl, resp) { + basic.NormalAfterLogic(w, r, resp) + } + } +} diff --git a/server/home-user-auth/internal/logic/userlogolistlogic.go b/server/home-user-auth/internal/logic/userlogolistlogic.go index 033fd0ed..c77cdb85 100644 --- a/server/home-user-auth/internal/logic/userlogolistlogic.go +++ b/server/home-user-auth/internal/logic/userlogolistlogic.go @@ -12,6 +12,7 @@ import ( "fusenapi/server/home-user-auth/internal/svc" "fusenapi/server/home-user-auth/internal/types" + "github.com/zeromicro/go-zero/core/logc" "github.com/zeromicro/go-zero/core/logx" "gorm.io/gorm" ) @@ -46,19 +47,76 @@ func (l *UserLogoListLogic) UserLogoList(req *types.UserLogoListReq, userinfo *a var userId int64 var guestId int64 + if userinfo.IsOnlooker() { + // 如果是,返回未授权的错误码 + return resp.SetStatus(basic.CodeUnAuth) + } + + // 用户信息 + NewFsUserInfoModel := gmodel.NewFsUserInfoModel(l.svcCtx.MysqlConn) + userInfoGorm := NewFsUserInfoModel.BuilderDB(l.ctx, nil).Where("module = ?", "profile") + userInfo := gmodel.FsUserInfo{} + // 检查用户是否是游客 if userinfo.IsGuest() { // 如果是,使用游客ID和游客键名格式 guestId = userinfo.GuestId + userInfoGorm.Where("guest_id = ?", guestId) } else { // 否则,使用用户ID和用户键名格式 userId = userinfo.UserId + userInfoGorm.Where("user_id = ?", userId) + } + var merchantCategoryIds []int64 + var logoSelectedIdd int64 + var userMaterialInfo *gmodel.RelaFsUserMaterial + userMaterialModel := gmodel.NewFsUserMaterialModel(l.svcCtx.MysqlConn) + resFirst := userInfoGorm.First(&userInfo) + err := resFirst.Error + if err != nil { + if err != gorm.ErrRecordNotFound { + logc.Errorf(l.ctx, "FsUserInfo First err:%+v", err) + return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "data not found") + } + } + if userInfo.Id != 0 && userInfo.Metadata != nil { + var metadata map[string]interface{} + err = json.Unmarshal([]byte(*userInfo.Metadata), &metadata) + if err != nil { + logc.Errorf(l.ctx, "userInfo.Metadata Unmarshal err:%+v", err) + return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get data list") + } + logoSelectedId, isEx := metadata["logo_selected_id"] + if isEx { + logoSelectedIdd = int64(logoSelectedId.(float64)) + userMaterialRSB1 := userMaterialModel.RowSelectBuilder(nil).Preload("ResourceInfo", func(dbPreload *gorm.DB) *gorm.DB { + return dbPreload.Table(gmodel.NewFsResourceModel(l.svcCtx.MysqlConn).TableName()) + }).Where("module = ?", "logo").Where("id = ?", logoSelectedIdd) + userMaterialInfo, err = userMaterialModel.FindOneData(l.ctx, userMaterialRSB1) + if err != nil { + if err != gorm.ErrRecordNotFound { + logc.Errorf(l.ctx, "FsUserInfo First err:%+v", err) + return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get data list") + } + } + var metadataMapuserMaterialInfo map[string]interface{} + if userMaterialInfo.Metadata != nil { + json.Unmarshal(*userMaterialInfo.Metadata, &metadataMapuserMaterialInfo) + merchantCategoryuserMaterialInfo, merchantCategoryEixuserMaterialInfo := metadataMapuserMaterialInfo["merchant_category"] + if merchantCategoryEixuserMaterialInfo { + merchantCategoryIduserMaterialInfo := int64(merchantCategoryuserMaterialInfo.(float64)) + merchantCategoryIds = append(merchantCategoryIds, merchantCategoryIduserMaterialInfo) + } + } + userMaterialInfo.MetaDataMap = metadataMapuserMaterialInfo + } } - userMaterialModel := gmodel.NewFsUserMaterialModel(l.svcCtx.MysqlConn) + // 历史列表 + userMaterialRSB := userMaterialModel.RowSelectBuilder(nil).Preload("ResourceInfo", func(dbPreload *gorm.DB) *gorm.DB { return dbPreload.Table(gmodel.NewFsResourceModel(l.svcCtx.MysqlConn).TableName()) - }).Where("module = ?", "logo").Order("id desc").Limit(10) + }).Where("module = ?", "logo").Order("id desc").Limit(5) if userId != 0 { userMaterialRSB.Where("user_id = ?", userId) } else { @@ -66,14 +124,13 @@ func (l *UserLogoListLogic) UserLogoList(req *types.UserLogoListReq, userinfo *a } list, err := userMaterialModel.FindList(l.ctx, userMaterialRSB, nil, "") if err != nil { - if errors.Is(err, gorm.ErrRecordNotFound) { - return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "data not found") + if !errors.Is(err, gorm.ErrRecordNotFound) { + logx.Error(err) + return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get data list") } - logx.Error(err) - return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get data list") } + var isDefaul bool = false if len(list) > 0 { - var merchantCategoryIds []int64 for _, v := range list { var metadataMap map[string]interface{} if v.Metadata != nil { @@ -95,6 +152,10 @@ func (l *UserLogoListLogic) UserLogoList(req *types.UserLogoListReq, userinfo *a v.ResourceInfo.MetaDataMap = resourceMetadata } } + + if logoSelectedIdd == v.Id { + isDefaul = true + } } if len(merchantCategoryIds) > 0 { newFsMerchantCategoryModel := gmodel.NewFsMerchantCategoryModel(l.svcCtx.MysqlConn) @@ -111,7 +172,10 @@ func (l *UserLogoListLogic) UserLogoList(req *types.UserLogoListReq, userinfo *a merchantCategoryData[v.Id] = v } } - for _, v := range list { + for k, v := range list { + if !isDefaul && k == 4 && userMaterialInfo != nil { + v = userMaterialInfo + } if v.MetaDataMap != nil { merchantCategory1, merchantCategoryEix1 := v.MetaDataMap["merchant_category"] if merchantCategoryEix1 { @@ -121,6 +185,7 @@ func (l *UserLogoListLogic) UserLogoList(req *types.UserLogoListReq, userinfo *a v.MetaDataMap["merchant_category_info"] = nil } } + list[k] = v } } } diff --git a/server/home-user-auth/internal/logic/userlogosetlogic.go b/server/home-user-auth/internal/logic/userlogosetlogic.go new file mode 100644 index 00000000..230b2af1 --- /dev/null +++ b/server/home-user-auth/internal/logic/userlogosetlogic.go @@ -0,0 +1,188 @@ +package logic + +import ( + "encoding/json" + "errors" + "fusenapi/model/gmodel" + "fusenapi/utils/auth" + "fusenapi/utils/basic" + "fusenapi/utils/metadata" + "time" + + "context" + + "fusenapi/server/home-user-auth/internal/svc" + "fusenapi/server/home-user-auth/internal/types" + + "github.com/zeromicro/go-zero/core/logc" + "github.com/zeromicro/go-zero/core/logx" + "gorm.io/gorm" +) + +type UserLogoSetLogic struct { + logx.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewUserLogoSetLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserLogoSetLogic { + return &UserLogoSetLogic{ + Logger: logx.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +// 处理进入前逻辑w,r +// func (l *UserLogoSetLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) { +// } + +func (l *UserLogoSetLogic) UserLogoSet(req *types.UserLogoSetReq, userinfo *auth.UserInfo) (resp *basic.Response) { + // 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data) + // userinfo 传入值时, 一定不为null + // 更新用户信息 + if userinfo.IsOnlooker() { + // 如果是,返回未授权的错误码 + return resp.SetStatus(basic.CodeUnAuth) + } + if req.LogoSelectedId == 0 { + return resp.SetStatus(basic.CodeLogoSetCategory, "logo logo_selected_id not null") + } + if req.SetLogoCategory == 1 && req.CategoryId == 0 { + return resp.SetStatus(basic.CodeLogoSetCategory, "logo category_id not null") + } + + var userId int64 + var guestId int64 + NewFsUserMaterialModel := gmodel.NewFsUserMaterialModel(l.svcCtx.MysqlConn) + NewFsUserMaterialModelRow := NewFsUserMaterialModel.RowSelectBuilder(nil).Where("id = ?", req.LogoSelectedId) + + if userinfo.IsGuest() { + // 如果是,使用游客ID和游客键名格式 + guestId = userinfo.GuestId + NewFsUserMaterialModelRow.Where("guest_id = ?", guestId) + } else { + // 否则,使用用户ID和用户键名格式 + userId = userinfo.UserId + NewFsUserMaterialModelRow.Where("user_id = ?", userId) + } + + userMaterialInfo, err := NewFsUserMaterialModel.FindOne(l.ctx, NewFsUserMaterialModelRow.Model(&gmodel.FsUserMaterial{})) + if err != nil { + logc.Errorf(l.ctx, "FsUserMaterial FindOne err:%+v", err) + return resp.SetStatus(basic.CodeLogoSetCategory, "logo not find") + } + + var nowTime = time.Now().UTC() + err = l.svcCtx.MysqlConn.WithContext(l.ctx).Transaction(func(tx *gorm.DB) error { + // 更新merchant_category + if req.SetLogoCategory == 1 { + + var metadataMapOldUserMaterial map[string]interface{} + if userMaterialInfo.Metadata != nil { + err = json.Unmarshal(*userMaterialInfo.Metadata, &metadataMapOldUserMaterial) + if err != nil { + logc.Errorf(l.ctx, "userMaterialInfo Metadata Unmarshal err:%+v", err) + return err + } + } + var metadataChildUserMaterial = make(map[string]interface{}, 1) + metadataChildUserMaterial["merchant_category"] = req.CategoryId + metadataMapUserMaterial, err := metadata.SetMetadata(metadataChildUserMaterial, metadataMapOldUserMaterial) + if err != nil { + logc.Errorf(l.ctx, "userMaterialInfo Metadata SetMetadata err:%+v", err) + return err + } + metadataBUserMaterial, err := json.Marshal(metadataMapUserMaterial) + if err != nil { + logc.Errorf(l.ctx, "userMaterialInfo Metadata err:%+v", err) + return err + } + userMaterialInfo.Metadata = &metadataBUserMaterial + resUpdates := tx.Select("metadata").Where("id = ?", req.LogoSelectedId).Updates(&userMaterialInfo) + err = resUpdates.Error + if err != nil { + if err != gorm.ErrRecordNotFound { + logc.Errorf(l.ctx, "userMaterialInfo Updates err:%+v", err) + return err + } + } + + } + var module = "profile" + if req.SetLogoSelected == 1 { + + var userInfo = &gmodel.FsUserInfo{} + BuilderDB := tx.Model(&gmodel.FsUserInfo{}).Where("module = ?", module) + if userId > 0 { + BuilderDB.Where("user_id=?", userId) + } else { + BuilderDB.Where("guest_id=?", guestId) + } + userInfoFirstRes := BuilderDB.First(userInfo) + err = userInfoFirstRes.Error + if err != nil { + if !errors.Is(err, gorm.ErrRecordNotFound) { + logc.Errorf(l.ctx, "userInfo First err:%+v", err) + return err + } + } + var metadataMapOldUserInfo map[string]interface{} + if userInfo.Metadata != nil { + err = json.Unmarshal(*userInfo.Metadata, &metadataMapOldUserInfo) + if err != nil { + logc.Errorf(l.ctx, "userInfo Metadata Unmarshal err:%+v", err) + return err + } + } + + var metadataChildUserInfo = make(map[string]interface{}, 1) + metadataChildUserInfo["logo_selected_id"] = req.LogoSelectedId + metadataMapUserInfo, err := metadata.SetMetadata(metadataChildUserInfo, metadataMapOldUserInfo) + if err != nil { + logc.Errorf(l.ctx, "userInfo Metadata SetMetadata err:%+v", err) + return err + } + metadataBUserInfo, err := json.Marshal(metadataMapUserInfo) + if err != nil { + logc.Errorf(l.ctx, "userInfo Metadata marshal err:%+v", err) + return err + } + userInfo.Metadata = &metadataBUserInfo + + if userInfo.Id == 0 { + // 新增 + userInfo.Module = &module + userInfo.Ctime = &nowTime + userInfo.Utime = &nowTime + userInfo.UserId = &userId + userInfo.GuestId = &guestId + resCreate := tx.Model(&userInfo).Create(&userInfo) + err = resCreate.Error + } else { + // 更新 + userInfo.Utime = &nowTime + resUpdates := tx.Model(&userInfo).Select("metadata").Where("id = ?", userInfo.Id).Updates(&userInfo) + err = resUpdates.Error + } + + if err != nil { + if err != gorm.ErrRecordNotFound { + logc.Errorf(l.ctx, "FsUserInfo Updates err:%+v", err) + return err + } + } + } + return nil + }) + if err != nil { + logx.Error(err) + return resp.SetStatus(basic.CodeDbSqlErr, "set logo fail") + } + return resp.SetStatus(basic.CodeOK) +} + +// 处理逻辑后 w,r 如:重定向, resp 必须重新处理 +// func (l *UserLogoSetLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) { +// // httpx.OkJsonCtx(r.Context(), w, resp) +// } diff --git a/server/home-user-auth/internal/types/types.go b/server/home-user-auth/internal/types/types.go index 0d1c0fc6..0ec0cfaa 100644 --- a/server/home-user-auth/internal/types/types.go +++ b/server/home-user-auth/internal/types/types.go @@ -5,6 +5,13 @@ import ( "fusenapi/utils/basic" ) +type UserLogoSetReq struct { + SetLogoSelected int64 `form:"set_logo_selected"` + LogoSelectedId int64 `form:"logo_selected_id"` + SetLogoCategory int64 `form:"set_logo_category"` + CategoryId int64 `form:"category_id"` +} + type UserInfoSetReq struct { Module string `form:"module,options=[merchant_category,logo_merchant_category,profile]"` // json格式字符串 Metadata string `form:"metadata"` // json格式字符串 diff --git a/server/product/internal/logic/getrecommandproductlistlogic.go b/server/product/internal/logic/getrecommandproductlistlogic.go index 1f3d2326..c708f8f9 100644 --- a/server/product/internal/logic/getrecommandproductlistlogic.go +++ b/server/product/internal/logic/getrecommandproductlistlogic.go @@ -193,7 +193,7 @@ func (l *GetRecommandProductListLogic) GetRecommandProductList(req *types.GetRec Intro: *v.Intro, IsRecommend: isRecommend, MinPrice: minPrice, - IsCustomization: *productInfo.IsCustomization, + IsCustomization: *v.IsCustomization, } if _, ok := mapTagProp[productInfo.Id]; ok { item.CoverDefault = mapTagProp[productInfo.Id] diff --git a/server/resource/internal/logic/logoresizelogic.go b/server/resource/internal/logic/logoresizelogic.go index c405915d..47526031 100644 --- a/server/resource/internal/logic/logoresizelogic.go +++ b/server/resource/internal/logic/logoresizelogic.go @@ -98,7 +98,7 @@ func (l *LogoResizeLogic) LogoResize(req *types.LogoResizeReq, userinfo *auth.Us hashKeyDataB, _ := json.Marshal(req) json.Unmarshal(hashKeyDataB, &hashKeyDataMap) var resourceId string = hash.JsonHashKey(hashKeyDataMap) - fmt.Println(resourceId) + //fmt.Println(resourceId) // 上传文件 var upload = file.Upload{ @@ -107,6 +107,7 @@ func (l *LogoResizeLogic) LogoResize(req *types.LogoResizeReq, userinfo *auth.Us AwsSession: l.svcCtx.AwsSession, } uploadRes, err := upload.UploadFileByByte(&file.UploadBaseReq{ + Refresh: 1, FileHash: resourceId, FileByte: emptyBuff.Bytes(), UploadBucket: 1, diff --git a/server/upload/internal/logic/uploadfilesbackendlogic.go b/server/upload/internal/logic/uploadfilesbackendlogic.go index cedbb8de..7552f894 100644 --- a/server/upload/internal/logic/uploadfilesbackendlogic.go +++ b/server/upload/internal/logic/uploadfilesbackendlogic.go @@ -207,6 +207,7 @@ type UploadData struct { } type UploadUrl struct { + LogoId int64 `json:"logo_id"` Key string `json:"key"` Status int64 `json:"status"` ApiType int64 `json:"api_type"` diff --git a/server/upload/internal/logic/uploadlogologic.go b/server/upload/internal/logic/uploadlogologic.go index 658077cb..7db0fed9 100644 --- a/server/upload/internal/logic/uploadlogologic.go +++ b/server/upload/internal/logic/uploadlogologic.go @@ -1,7 +1,6 @@ package logic import ( - "encoding/json" "fmt" "fusenapi/model/gmodel" "fusenapi/service/repositories" @@ -9,7 +8,6 @@ import ( "fusenapi/utils/basic" "fusenapi/utils/file" "fusenapi/utils/hash" - "fusenapi/utils/metadata" "io" "net/http" "time" @@ -190,88 +188,23 @@ func (l *UploadLogoLogic) UploadLogo(req *types.UploadLogoReq, userinfo *auth.Us var module = "logo" var nowTime = time.Now().UTC() + // 新增素材记录 + materialInfo := gmodel.FsUserMaterial{ + Module: &module, + UserId: &userId, + GuestId: &guestId, + ResourceId: &uploadRes.ResourceId, + ResourceUrl: &uploadRes.ResourceUrl, + Metadata: &resultStrB, + Ctime: &nowTime, + } err = l.svcCtx.MysqlConn.WithContext(l.ctx).Transaction(func(tx *gorm.DB) error { - // 新增素材记录 - materialInfo := gmodel.FsUserMaterial{ - Module: &module, - UserId: &userId, - GuestId: &guestId, - ResourceId: &uploadRes.ResourceId, - ResourceUrl: &uploadRes.ResourceUrl, - Metadata: &resultStrB, - Ctime: &nowTime, - } resCreate := tx.Create(&materialInfo) err = resCreate.Error if err != nil { logc.Errorf(l.ctx, "FsUserInfo First err:%+v", err) return err } - // 更新用户信息 - var module = "profile" - userInfoGorm := tx.Where("module = ?", module) - userInfo := gmodel.FsUserInfo{} - if userId > 0 { - userInfoGorm.Where("user_id = ?", userId) - userInfo.UserId = &userId - } else { - userInfoGorm.Where("guest_id = ?", guestId) - userInfo.GuestId = &guestId - } - resFirst := userInfoGorm.First(&userInfo) - err = resFirst.Error - if err != nil { - if err != gorm.ErrRecordNotFound { - logc.Errorf(l.ctx, "FsUserInfo First err:%+v", err) - return err - } - } - var metadataMap map[string]interface{} - var metadataMapOld map[string]interface{} - if userInfo.Id > 0 { - err = json.Unmarshal(*userInfo.Metadata, &metadataMapOld) - if err != nil { - logc.Errorf(l.ctx, "userInfo.Metadata Unmarshal err:%+v", err) - return err - } - } - var metadataChild = make(map[string]interface{}, 1) - metadataChild["logo_selected_id"] = materialInfo.Id - metadataMap, err = metadata.SetMetadata(metadataChild, metadataMapOld) - if err != nil { - logc.Errorf(l.ctx, "metadata SetMetadata err:%+v", err) - return err - } - metadataB, err := json.Marshal(metadataMap) - if err != nil { - logc.Errorf(l.ctx, "metadata marshal err:%+v", err) - return err - } - userInfo.Metadata = &metadataB - if userInfo.Id > 0 { - userInfo.Utime = &nowTime - resUpdates := tx.Select("metadata").Where("id = ?", userInfo.Id).Updates(&userInfo) - err = resUpdates.Error - if err != nil { - if err != gorm.ErrRecordNotFound { - logc.Errorf(l.ctx, "FsUserInfo Updates err:%+v", err) - return err - } - } - } else { - var nowTime = time.Now().UTC() - userInfo.Module = &module - userInfo.Ctime = &nowTime - userInfo.Utime = &nowTime - resCreate := tx.Create(&userInfo) - err = resCreate.Error - if err != nil { - if err != gorm.ErrRecordNotFound { - logc.Errorf(l.ctx, "FsUserInfo Create err:%+v", err) - return err - } - } - } return nil }) @@ -284,6 +217,7 @@ func (l *UploadLogoLogic) UploadLogo(req *types.UploadLogoReq, userinfo *auth.Us return resp.SetStatus(basic.CodeOK, map[string]interface{}{ "upload_data": UploadUrl{ Status: 1, + LogoId: materialInfo.Id, ResourceId: uploadRes.ResourceId, ResourceUrl: uploadRes.ResourceUrl, }, diff --git a/server/websocket/internal/logic/datatransferlogic.go b/server/websocket/internal/logic/datatransferlogic.go index a01a9970..19f3704d 100644 --- a/server/websocket/internal/logic/datatransferlogic.go +++ b/server/websocket/internal/logic/datatransferlogic.go @@ -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纳秒,后面需要根据不同用户不同触发速度 }, } //保存连接 diff --git a/server/websocket/internal/logic/ws_render_image.go b/server/websocket/internal/logic/ws_render_image.go index 26976272..d1cc0405 100644 --- a/server/websocket/internal/logic/ws_render_image.go +++ b/server/websocket/internal/logic/ws_render_image.go @@ -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 diff --git a/server_api/home-user-auth.api b/server_api/home-user-auth.api index d97db2ca..2b030c28 100644 --- a/server_api/home-user-auth.api +++ b/server_api/home-user-auth.api @@ -67,8 +67,21 @@ service home-user-auth { // 保存商户信息 @handler UserInfoSetHandler post /api/user/set_user_info (UserInfoSetReq) returns (response); + + // 用户logo设置当前 + @handler UserLogoSetHandler + post /api/user/logo-set (UserLogoSetReq) returns (response); } +type ( + UserLogoSetReq { + SetLogoSelected int64 `form:"set_logo_selected"` + LogoSelectedId int64 `form:"logo_selected_id"` + SetLogoCategory int64 `form:"set_logo_category"` + CategoryId int64 `form:"category_id"` + } +) + type ( UserInfoSetReq { Module string `form:"module,options=[merchant_category,logo_merchant_category,profile]"` // json格式字符串 diff --git a/utils/basic/basic.go b/utils/basic/basic.go index afce9235..35f17a2b 100644 --- a/utils/basic/basic.go +++ b/utils/basic/basic.go @@ -95,6 +95,7 @@ var ( CodeFileUploadLogoErrType = &StatusResponse{5111, "logo upload err file is not image"} // 用户上位LOGO失败"} CodeFileLogoCombineErr = &StatusResponse{5112, "logo upload err"} // 用户合图失败 CodeFileNoFoundErr = &StatusResponse{5113, "file not found err"} // 文件不存在 + CodeLogoSetCategory = &StatusResponse{5114, "logo set category fail"} // 文件不存在 ) type Response struct {