diff --git a/server/home-user-auth/internal/handler/routes.go b/server/home-user-auth/internal/handler/routes.go index 9fbecfd9..60b66955 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.MethodGet, + 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/userlogosetlogic.go b/server/home-user-auth/internal/logic/userlogosetlogic.go new file mode 100644 index 00000000..91b93fb0 --- /dev/null +++ b/server/home-user-auth/internal/logic/userlogosetlogic.go @@ -0,0 +1,166 @@ +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) + } + + var userId int64 + var guestId int64 + + // 检查用户是否是游客 + if userinfo.IsGuest() { + // 如果是,使用游客ID和游客键名格式 + guestId = userinfo.GuestId + } else { + // 否则,使用用户ID和用户键名格式 + userId = userinfo.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) + if err != nil { + if errors.Is(err, gorm.ErrRecordNotFound) { + logx.Error(err) + return resp.SetStatus(basic.CodeDbSqlErr, "UserInfoSet error system failed") + } + } + + 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)) + + var userMaterialInfo gmodel.FsUserMaterial + userMaterialGorm := tx.Where("id = ?", materialId) + resUserMaterialInfo := userMaterialGorm.First(&userMaterialInfo) + err = resUserMaterialInfo.Error + if err != nil { + logc.Errorf(l.ctx, "FsUserMaterial First 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) + 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 + 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, "service 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..763ebe23 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 UserLogoSetReq struct { + LogoSelectedId int64 `form:"logo_selected_id"` + CategoryId int64 `form:"category_id,optional"` +} + type UserInfoSetReq struct { Module string `form:"module,options=[merchant_category,logo_merchant_category,profile]"` // json格式字符串 Metadata string `form:"metadata"` // json格式字符串 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_api/home-user-auth.api b/server_api/home-user-auth.api index d97db2ca..65fb4fd0 100644 --- a/server_api/home-user-auth.api +++ b/server_api/home-user-auth.api @@ -67,8 +67,19 @@ service home-user-auth { // 保存商户信息 @handler UserInfoSetHandler post /api/user/set_user_info (UserInfoSetReq) returns (response); + + // 用户logo设置当前 + @handler UserLogoSetHandler + get /api/user/logo-set (UserLogoSetReq) returns (response); } +type ( + UserLogoSetReq { + LogoSelectedId int64 `form:"logo_selected_id"` + CategoryId int64 `form:"category_id,optional"` + } +) + type ( UserInfoSetReq { Module string `form:"module,options=[merchant_category,logo_merchant_category,profile]"` // json格式字符串