diff --git a/server/home-user-auth/internal/handler/routes.go b/server/home-user-auth/internal/handler/routes.go index 1116922b..6e056ad4 100644 --- a/server/home-user-auth/internal/handler/routes.go +++ b/server/home-user-auth/internal/handler/routes.go @@ -92,6 +92,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { Path: "/api/user/logo-set", Handler: UserLogoSetHandler(serverCtx), }, + { + Method: http.MethodPost, + Path: "/api/user/logo-templatetag-set", + Handler: UserLogoTemplateTagSetHandler(serverCtx), + }, }, ) } diff --git a/server/home-user-auth/internal/handler/userlogotemplatetagsethandler.go b/server/home-user-auth/internal/handler/userlogotemplatetagsethandler.go new file mode 100644 index 00000000..3c7994a8 --- /dev/null +++ b/server/home-user-auth/internal/handler/userlogotemplatetagsethandler.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 UserLogoTemplateTagSetHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + + var req types.UserLogoTemplateTagSetReq + userinfo, err := basic.RequestParse(w, r, svcCtx, &req) + if err != nil { + return + } + + // 创建一个业务逻辑层实例 + l := logic.NewUserLogoTemplateTagSetLogic(r.Context(), svcCtx) + + rl := reflect.ValueOf(l) + basic.BeforeLogic(w, r, rl) + + resp := l.UserLogoTemplateTagSet(&req, userinfo) + + if !basic.AfterLogic(w, r, rl, resp) { + basic.NormalAfterLogic(w, r, resp) + } + } +} diff --git a/server/home-user-auth/internal/logic/userlogosetlogic.go b/server/home-user-auth/internal/logic/userlogosetlogic.go index 230b2af1..f6d4df4b 100644 --- a/server/home-user-auth/internal/logic/userlogosetlogic.go +++ b/server/home-user-auth/internal/logic/userlogosetlogic.go @@ -75,17 +75,16 @@ func (l *UserLogoSetLogic) UserLogoSet(req *types.UserLogoSetReq, userinfo *auth var nowTime = time.Now().UTC() err = l.svcCtx.MysqlConn.WithContext(l.ctx).Transaction(func(tx *gorm.DB) error { + 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 + } + } // 更新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) @@ -137,7 +136,41 @@ func (l *UserLogoSetLogic) UserLogoSet(req *types.UserLogoSetReq, userinfo *auth } var metadataChildUserInfo = make(map[string]interface{}, 1) - metadataChildUserInfo["logo_selected_id"] = req.LogoSelectedId + + _, userInfoLogoSelectedEx := metadataMapOldUserInfo["logo_selected"] + if !userInfoLogoSelectedEx { + userMaterialTemplateTagData, userMaterialTemplateTagEx := metadataMapOldUserMaterial["template_tag"] + if !userMaterialTemplateTagEx { + logc.Errorf(l.ctx, "userMaterialInfo Metadata template_tag err:%+v", err) + return err + } + userMaterialTemplateIdTagIdData, userMaterialTemplateTagIdEx := metadataMapOldUserMaterial["template_tag_id"] + if !userMaterialTemplateTagIdEx { + logc.Errorf(l.ctx, "userMaterialInfo Metadata template_tag_id err:%+v", err) + return err + } + userMaterialTemplateIdTagIds := userMaterialTemplateIdTagIdData.([]interface{}) + + userMaterialTemplateIdTagId := userMaterialTemplateIdTagIds[0].(string) + + userMaterialTemplateTag := userMaterialTemplateTagData.(map[string]interface{}) + userMaterialTemplateTagIndex := userMaterialTemplateTag[userMaterialTemplateIdTagId] + + templateTagSelected := make(map[string]interface{}, 3) + templateTagSelected["color"] = userMaterialTemplateTagIndex + templateTagSelected["template_tag"] = userMaterialTemplateIdTagId + templateTagSelected["selected_index"] = 0 + + metadataChildUserInfo["logo_selected"] = map[string]interface{}{ + "logo_selected_id": req.LogoSelectedId, + "template_tag_selected": templateTagSelected, + } + } else { + metadataChildUserInfo["logo_selected"] = map[string]interface{}{ + "logo_selected_id": req.LogoSelectedId, + } + } + metadataMapUserInfo, err := metadata.SetMetadata(metadataChildUserInfo, metadataMapOldUserInfo) if err != nil { logc.Errorf(l.ctx, "userInfo Metadata SetMetadata err:%+v", err) diff --git a/server/home-user-auth/internal/logic/userlogotemplatetagsetlogic.go b/server/home-user-auth/internal/logic/userlogotemplatetagsetlogic.go new file mode 100644 index 00000000..59c7e3ca --- /dev/null +++ b/server/home-user-auth/internal/logic/userlogotemplatetagsetlogic.go @@ -0,0 +1,187 @@ +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 UserLogoTemplateTagSetLogic struct { + logx.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewUserLogoTemplateTagSetLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserLogoTemplateTagSetLogic { + return &UserLogoTemplateTagSetLogic{ + Logger: logx.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +// 处理进入前逻辑w,r +// func (l *UserLogoTemplateTagSetLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) { +// } + +func (l *UserLogoTemplateTagSetLogic) UserLogoTemplateTagSet(req *types.UserLogoTemplateTagSetReq, 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.CodeLogoSetTemplateErr, "logo logo_selected_id not null") + } + if req.TemplateTag == "" { + return resp.SetStatus(basic.CodeLogoSetTemplateErr, "logo template tag 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 { + 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 module = "profile" + + 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) + + _, userInfoLogoSelectedEx := metadataMapOldUserInfo["logo_selected"] + if userInfoLogoSelectedEx { + userMaterialTemplateTagData, userMaterialTemplateTagEx := metadataMapOldUserMaterial["template_tag"] + if !userMaterialTemplateTagEx { + logc.Errorf(l.ctx, "userMaterialInfo Metadata template_tag err:%+v", err) + return err + } + + userMaterialTemplateTag := userMaterialTemplateTagData.(map[string]interface{}) + userMaterialTemplateTagIndex := userMaterialTemplateTag[req.TemplateTag] + + if userMaterialTemplateTagIndex == nil { + err = errors.New("TemplateTag not find") + logc.Errorf(l.ctx, "TemplateTag not find err:%+v", err) + return err + } + + templateTagSelected := make(map[string]interface{}, 3) + templateTagSelected["color"] = userMaterialTemplateTagIndex + templateTagSelected["template_tag"] = req.TemplateTag + templateTagSelected["selected_index"] = req.TemplateTagColorIndex + + metadataChildUserInfo["logo_selected"] = map[string]interface{}{ + "template_tag_selected": templateTagSelected, + } + } + + 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 *UserLogoTemplateTagSetLogic) 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 0ec0cfaa..1c049a7e 100644 --- a/server/home-user-auth/internal/types/types.go +++ b/server/home-user-auth/internal/types/types.go @@ -5,6 +5,12 @@ import ( "fusenapi/utils/basic" ) +type UserLogoTemplateTagSetReq struct { + LogoSelectedId int64 `form:"logo_selected_id"` + TemplateTag string `form:"template_tag"` + TemplateTagColorIndex int64 `form:"template_tag_color_index"` +} + type UserLogoSetReq struct { SetLogoSelected int64 `form:"set_logo_selected"` LogoSelectedId int64 `form:"logo_selected_id"` diff --git a/server/upload/etc/upload.yaml b/server/upload/etc/upload.yaml index b8596b53..36922560 100644 --- a/server/upload/etc/upload.yaml +++ b/server/upload/etc/upload.yaml @@ -20,6 +20,7 @@ AWS: Token: BLMService: Url: "http://18.119.109.254:8999" + # Url: "http://192.168.1.9:8999" ImageProcess: # Url: "http://192.168.1.7:8999/FeatureExtraction" Url: "http://18.119.109.254:8999/FeatureExtraction" diff --git a/server_api/home-user-auth.api b/server_api/home-user-auth.api index 2b030c28..27d9f03e 100644 --- a/server_api/home-user-auth.api +++ b/server_api/home-user-auth.api @@ -71,8 +71,20 @@ service home-user-auth { // 用户logo设置当前 @handler UserLogoSetHandler post /api/user/logo-set (UserLogoSetReq) returns (response); + + // 用户logo模版信息 + @handler UserLogoTemplateTagSetHandler + post /api/user/logo-templatetag-set (UserLogoTemplateTagSetReq) returns (response); } +type ( + UserLogoTemplateTagSetReq { + LogoSelectedId int64 `form:"logo_selected_id"` + TemplateTag string `form:"template_tag"` + TemplateTagColorIndex int64 `form:"template_tag_color_index"` + } +) + type ( UserLogoSetReq { SetLogoSelected int64 `form:"set_logo_selected"` diff --git a/service/repositories/image_handle.go b/service/repositories/image_handle.go index a18c70de..fe8e486c 100644 --- a/service/repositories/image_handle.go +++ b/service/repositories/image_handle.go @@ -54,8 +54,16 @@ type ( GuestId int64 `json:"guest_id"` } LogoInfoRes struct { - Metadata *string `json:"metadata"` - LogoUrl *string `json:"logo_url"` + Metadata *string `json:"metadata"` + LogoUrl *string `json:"logo_url"` + LogoSelected struct { + TemplateTagSelected *struct { + Color [][]string `json:"color"` + TemplateTag string `json:"template_tag"` + SelectedIndex int64 `json:"selected_index"` + } `json:"template_tag_selected"` + LogoSelectedId int64 `json:"logo_selected_id"` + } `json:"logo_selected"` } ) @@ -84,28 +92,36 @@ func (l *defaultImageHandle) LogoInfo(ctx context.Context, in *LogoInfoReq) (*Lo } var userMaterialInfo gmodel.FsUserMaterial userMaterialModel := gmodel.NewFsUserMaterialModel(l.MysqlConn) - + var metadataUserInfo struct { + LogoSelected struct { + TemplateTagSelected *struct { + Color [][]string `json:"color"` + TemplateTag string `json:"template_tag"` + SelectedIndex int64 `json:"selected_index"` + } `json:"template_tag_selected"` + LogoSelectedId int64 `json:"logo_selected_id"` + } `json:"logo_selected"` + } if userInfo.Id == 0 { userMaterialInfo, err = userMaterialModel.FindLatestOne(ctx, in.UserId, in.GuestId) if err != gorm.ErrRecordNotFound { logc.Errorf(ctx, "FsUserMaterial FindLatestOne err:%+v", err) return nil, err } + // var userMaterialInfoMetadata map[string]interface{} } else { if userInfo.Metadata != nil { - var metadata map[string]interface{} - err = json.Unmarshal([]byte(*userInfo.Metadata), &metadata) + err = json.Unmarshal([]byte(*userInfo.Metadata), &metadataUserInfo) if err != nil { logc.Errorf(ctx, "userInfo.Metadata Unmarshal err:%+v", err) return nil, err } - logoSelectedId, isEx := metadata["logo_selected_id"] - if isEx { - var materialId int64 = int64(logoSelectedId.(float64)) - userMaterialInfos, err := userMaterialModel.FindOneById(ctx, materialId) + + if metadataUserInfo.LogoSelected.LogoSelectedId != 0 { + userMaterialInfos, err := userMaterialModel.FindOneById(ctx, metadataUserInfo.LogoSelected.LogoSelectedId) if err != nil { if err != gorm.ErrRecordNotFound { - logc.Errorf(ctx, "FsUserInfo First err:%+v", err) + logc.Errorf(ctx, "userMaterial findOneById err:%+v", err) return nil, err } } @@ -134,8 +150,9 @@ func (l *defaultImageHandle) LogoInfo(ctx context.Context, in *LogoInfoReq) (*Lo logoUrl = userMaterialInfo.ResourceUrl } return &LogoInfoRes{ - Metadata: metadata, - LogoUrl: logoUrl, + Metadata: metadata, + LogoUrl: logoUrl, + LogoSelected: metadataUserInfo.LogoSelected, }, nil } @@ -302,6 +319,7 @@ func (l *defaultImageHandle) LogoCombine(ctx context.Context, in *LogoCombineReq combineParam["phone"] = in.Phone combineParam["address"] = in.Address combineParam["qrcode"] = in.Qrcode + combineParam["template_tag_selected"] = resLogoInfo.LogoSelected var postMap = make(map[string]interface{}, 2) postMap["module_data"] = moduleDataMap diff --git a/utils/basic/basic.go b/utils/basic/basic.go index 795f04ba..54efa272 100644 --- a/utils/basic/basic.go +++ b/utils/basic/basic.go @@ -99,6 +99,8 @@ var ( CodeLogoCombineErr = &StatusResponse{5115, "logo combine fail"} // 合图失败 CodeLogoCombineNoFoundErr = &StatusResponse{5116, "template record not found"} // 模版不存在 + + CodeLogoSetTemplateErr = &StatusResponse{5117, "logo set template fail"} // 设置模版标签失败 ) type Response struct { diff --git a/utils/fssql/metadata_test.go b/utils/fssql/metadata_test.go index 4c12011d..7cdf3e54 100644 --- a/utils/fssql/metadata_test.go +++ b/utils/fssql/metadata_test.go @@ -14,15 +14,9 @@ func TestCase1(t *testing.T) { } conn := initalize.InitMysql("fsreaderwriter:XErSYmLELKMnf3Dh@tcp(fusen.cdmigcvz3rle.us-east-2.rds.amazonaws.com:3306)/fusen") - err := fssql.MetadataModulePATCH(conn, "logo", gmodel.FsChangeCode{}, u, "id = ?", 6) + err := fssql.MetadataModulePATCH(conn, "profile", gmodel.FsUserInfo{}, u, "id = ?", 90) if err != nil { panic(err) } - err = fssql.MetadataResourcePATCH(conn, - "f8932c0379fa5aa3397dc0a963696ca90536cc273ea10119b0137fd15ecfe673", u) - - if err != nil { - panic(err) - } } diff --git a/utils/metadata/metadata.go b/utils/metadata/metadata.go index 19855260..b7cd4abf 100644 --- a/utils/metadata/metadata.go +++ b/utils/metadata/metadata.go @@ -5,9 +5,15 @@ func SetMetadata(metadataChild map[string]interface{}, oldMetadata map[string]in for k, v := range metadataChild { switch val := v.(type) { case map[string]interface{}: - oldResourceMetadataData := oldMetadata[k].(map[string]interface{}) - for key, val1 := range val { - oldResourceMetadataData[key] = val1 + kValue, kEx := oldMetadata[k] + var oldResourceMetadataData map[string]interface{} + if kEx { + oldResourceMetadataData = kValue.(map[string]interface{}) + } else { + oldResourceMetadataData = make(map[string]interface{}, len(val)) + } + for key1, val1 := range val { + oldResourceMetadataData[key1] = val1 } oldMetadata[k] = oldResourceMetadataData default: @@ -20,11 +26,17 @@ func SetMetadata(metadataChild map[string]interface{}, oldMetadata map[string]in for k, v := range metadataChild { switch val := v.(type) { case map[string]interface{}: - var resourceMetadataData = make(map[string]interface{}, len(val)) - for key, val1 := range val { - resourceMetadataData[key] = val1 + kValue, kEx := oldMetadata[k] + var oldResourceMetadataData map[string]interface{} + if kEx { + oldResourceMetadataData = kValue.(map[string]interface{}) + } else { + oldResourceMetadataData = make(map[string]interface{}, len(val)) } - oldMetadata[k] = resourceMetadataData + for key, val1 := range val { + oldResourceMetadataData[key] = val1 + } + oldMetadata[k] = oldResourceMetadataData default: oldMetadata[k] = val }