diff --git a/model/gmodel/fs_user_material_logic.go b/model/gmodel/fs_user_material_logic.go index c353b9ad..ebe8cb9d 100644 --- a/model/gmodel/fs_user_material_logic.go +++ b/model/gmodel/fs_user_material_logic.go @@ -52,6 +52,15 @@ 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) 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 60b66955..1116922b 100644 --- a/server/home-user-auth/internal/handler/routes.go +++ b/server/home-user-auth/internal/handler/routes.go @@ -88,7 +88,7 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { Handler: UserInfoSetHandler(serverCtx), }, { - Method: http.MethodGet, + Method: http.MethodPost, Path: "/api/user/logo-set", Handler: UserLogoSetHandler(serverCtx), }, diff --git a/server/home-user-auth/internal/logic/userlogosetlogic.go b/server/home-user-auth/internal/logic/userlogosetlogic.go index 91b93fb0..99f1e04d 100644 --- a/server/home-user-auth/internal/logic/userlogosetlogic.go +++ b/server/home-user-auth/internal/logic/userlogosetlogic.go @@ -45,101 +45,110 @@ func (l *UserLogoSetLogic) UserLogoSet(req *types.UserLogoSetReq, userinfo *auth // 如果是,返回未授权的错误码 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) } - var userInfo = &gmodel.FsUserInfo{} - fsUserInfoModel := gmodel.NewFsUserInfoModel(l.svcCtx.MysqlConn) - BuilderDB := fsUserInfoModel.BuilderDB(l.ctx, nil).Model(&gmodel.FsUserInfo{}).Where("module = ?", "profile") - if userId > 0 { - BuilderDB.Where("user_id=?", userId) - } else { - BuilderDB.Where("guest_id=?", guestId) - } - userInfo, err := fsUserInfoModel.FindOne(BuilderDB, nil) + userMaterialInfo, err := NewFsUserMaterialModel.FindOne(l.ctx, NewFsUserMaterialModelRow.Model(&gmodel.FsUserMaterial{})) if err != nil { - if errors.Is(err, gorm.ErrRecordNotFound) { - logx.Error(err) - return resp.SetStatus(basic.CodeDbSqlErr, "UserInfoSet error system failed") - } + logc.Errorf(l.ctx, "FsUserMaterial FindOne err:%+v", err) + return resp.SetStatus(basic.CodeLogoSetCategory, "logo not find") } var nowTime = time.Now().UTC() - - var metadataMap map[string]interface{} err = l.svcCtx.MysqlConn.WithContext(l.ctx).Transaction(func(tx *gorm.DB) error { - if userInfo.Id != 0 { - var metadataMapOld map[string]interface{} - json.Unmarshal(*userInfo.Metadata, &metadataMapOld) - } // 更新merchant_category - if req.Module == "merchant_category" { - logoSelectedId, isEx := metadataMapOld["logo_selected_id"] - if isEx { - var materialId int64 = int64(logoSelectedId.(float64)) + if req.SetLogoCategory == 1 { - var userMaterialInfo gmodel.FsUserMaterial - userMaterialGorm := tx.Where("id = ?", materialId) - resUserMaterialInfo := userMaterialGorm.First(&userMaterialInfo) - err = resUserMaterialInfo.Error + var metadataMapOldUserMaterial map[string]interface{} + if userMaterialInfo.Metadata != nil { + err = json.Unmarshal(*userMaterialInfo.Metadata, &metadataMapOldUserMaterial) if err != nil { - logc.Errorf(l.ctx, "FsUserMaterial First err:%+v", err) + logc.Errorf(l.ctx, "userMaterialInfo Metadata Unmarshal err:%+v", err) return err } - var metadataMapOld map[string]interface{} - if userMaterialInfo.Id > 0 { - err = json.Unmarshal(*userMaterialInfo.Metadata, &metadataMapOld) - if err != nil { - logc.Errorf(l.ctx, "userMaterialInfo Metadata Unmarshal err:%+v", err) - return err - } - } - var metadataChild = make(map[string]interface{}, 1) - metadataChild["merchant_category"] = reqMetadata["category_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 - } - userMaterialInfo.Metadata = &metadataB - resUpdates := tx.Select("metadata").Where("id = ?", materialId).Updates(&userMaterialInfo) - err = resUpdates.Error - if err != nil { - if err != gorm.ErrRecordNotFound { - logc.Errorf(l.ctx, "userMaterialInfo Updates err:%+v", err) - return err - } - } } - } else { - var metadataChild = make(map[string]interface{}, 1) - metadataChild["logo_selected_id"] = reqMetadata["logo_selected_id"] - metadataMap, err = metadata.SetMetadata(metadataChild, metadataMapOld) + 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, "metadata SetMetadata err:%+v", err) + logc.Errorf(l.ctx, "userMaterialInfo Metadata SetMetadata err:%+v", err) return err } - metadataB, err := json.Marshal(metadataMap) + metadataBUserMaterial, err := json.Marshal(metadataMapUserMaterial) if err != nil { - logc.Errorf(l.ctx, "metadata marshal err:%+v", err) + logc.Errorf(l.ctx, "userMaterialInfo Metadata err:%+v", err) return err } - userInfo.Metadata = &metadataB + 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 + } + } + + } + + if req.SetLogoSelected == 1 { + + var userInfo = &gmodel.FsUserInfo{} + BuilderDB := tx.Model(&gmodel.FsUserInfo{}).Where("module = ?", "profile") + 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 userInfo.Utime = &nowTime resUpdates := tx.Model(&userInfo).Select("metadata").Where("id = ?", userInfo.Id).Updates(&userInfo) err = resUpdates.Error @@ -150,12 +159,11 @@ func (l *UserLogoSetLogic) UserLogoSet(req *types.UserLogoSetReq, userinfo *auth } } } - return nil }) if err != nil { logx.Error(err) - return resp.SetStatus(basic.CodeDbSqlErr, "service fail") + return resp.SetStatus(basic.CodeDbSqlErr, "set logo fail") } 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 763ebe23..0ec0cfaa 100644 --- a/server/home-user-auth/internal/types/types.go +++ b/server/home-user-auth/internal/types/types.go @@ -6,8 +6,10 @@ import ( ) type UserLogoSetReq struct { - LogoSelectedId int64 `form:"logo_selected_id"` - CategoryId int64 `form:"category_id,optional"` + 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 { diff --git a/server_api/home-user-auth.api b/server_api/home-user-auth.api index 65fb4fd0..2b030c28 100644 --- a/server_api/home-user-auth.api +++ b/server_api/home-user-auth.api @@ -70,13 +70,15 @@ service home-user-auth { // 用户logo设置当前 @handler UserLogoSetHandler - get /api/user/logo-set (UserLogoSetReq) returns (response); + post /api/user/logo-set (UserLogoSetReq) returns (response); } type ( UserLogoSetReq { - LogoSelectedId int64 `form:"logo_selected_id"` - CategoryId int64 `form:"category_id,optional"` + SetLogoSelected int64 `form:"set_logo_selected"` + LogoSelectedId int64 `form:"logo_selected_id"` + SetLogoCategory int64 `form:"set_logo_category"` + CategoryId int64 `form:"category_id"` } ) 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 {