From 639e9f8d4acae0226d482e4f8055e83534cc8286 Mon Sep 17 00:00:00 2001 From: momo <1012651275@qq.com> Date: Thu, 26 Oct 2023 14:35:22 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E5=89=8D=E5=8F=B0=E4=BB=8Elogo=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E5=BA=93=E5=AD=98=E5=82=A8=E4=B8=BA=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E7=B4=A0=E6=9D=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../home-user-auth/internal/handler/routes.go | 5 + .../handler/userlogodatasethandler.go | 35 +++++++ .../internal/logic/userlogodatasetlogic.go | 97 +++++++++++++++++++ server/home-user-auth/internal/types/types.go | 4 + server_api/home-user-auth.api | 34 ++++--- 5 files changed, 163 insertions(+), 12 deletions(-) create mode 100644 server/home-user-auth/internal/handler/userlogodatasethandler.go create mode 100644 server/home-user-auth/internal/logic/userlogodatasetlogic.go diff --git a/server/home-user-auth/internal/handler/routes.go b/server/home-user-auth/internal/handler/routes.go index d8855f3a..4524f011 100644 --- a/server/home-user-auth/internal/handler/routes.go +++ b/server/home-user-auth/internal/handler/routes.go @@ -72,6 +72,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { Path: "/api/user/logo-templatetag-set", Handler: UserLogoTemplateTagSetHandler(serverCtx), }, + { + Method: http.MethodPost, + Path: "/api/user/logo-data-set", + Handler: UserLogoDataSetHandler(serverCtx), + }, }, ) } diff --git a/server/home-user-auth/internal/handler/userlogodatasethandler.go b/server/home-user-auth/internal/handler/userlogodatasethandler.go new file mode 100644 index 00000000..772b3df5 --- /dev/null +++ b/server/home-user-auth/internal/handler/userlogodatasethandler.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 UserLogoDataSetHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + + var req types.UserLogoDataSetReq + userinfo, err := basic.RequestParse(w, r, svcCtx, &req) + if err != nil { + return + } + + // 创建一个业务逻辑层实例 + l := logic.NewUserLogoDataSetLogic(r.Context(), svcCtx) + + rl := reflect.ValueOf(l) + basic.BeforeLogic(w, r, rl) + + resp := l.UserLogoDataSet(&req, userinfo) + + if !basic.AfterLogic(w, r, rl, resp) { + basic.NormalAfterLogic(w, r, resp) + } + } +} diff --git a/server/home-user-auth/internal/logic/userlogodatasetlogic.go b/server/home-user-auth/internal/logic/userlogodatasetlogic.go new file mode 100644 index 00000000..c0275d23 --- /dev/null +++ b/server/home-user-auth/internal/logic/userlogodatasetlogic.go @@ -0,0 +1,97 @@ +package logic + +import ( + "errors" + "fusenapi/model/gmodel" + "fusenapi/utils/auth" + "fusenapi/utils/basic" + "fusenapi/utils/s3url_to_s3id" + "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 UserLogoDataSetLogic struct { + logx.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewUserLogoDataSetLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserLogoDataSetLogic { + return &UserLogoDataSetLogic{ + Logger: logx.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +// 处理进入前逻辑w,r +// func (l *UserLogoDataSetLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) { +// } + +func (l *UserLogoDataSetLogic) UserLogoDataSet(req *types.UserLogoDataSetReq, 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 logoData = gmodel.FsPreprocessLogo{} + result := l.svcCtx.MysqlConn.Model(&gmodel.FsPreprocessLogo{}).Where("id = ?", req.LogoDataId).Take(&logoData) + err := result.Error + if err != nil { + if errors.Is(err, gorm.ErrRecordNotFound) { + logc.Errorf(l.ctx, "UserLogoDataSet logoData find err:%+v", err) + basic.CodeApiErr.Message = "logo data not find" + return resp.SetStatus(basic.CodeApiErr) + } + return resp.SetStatus(basic.CodeApiErr) + } + // 新增素材 + var module = "logo" + var nowTime = time.Now().UTC() + var resourceId = s3url_to_s3id.GetS3ResourceIdFormUrl(*logoData.ResourceUrl) + // 新增素材记录 + materialInfo := gmodel.FsUserMaterial{ + Module: &module, + UserId: &userId, + GuestId: &guestId, + ResourceId: &resourceId, + ResourceUrl: logoData.ResourceUrl, + Ctime: &nowTime, + } + resCreate := l.svcCtx.MysqlConn.Create(&materialInfo) + err = resCreate.Error + if err != nil { + logc.Errorf(l.ctx, "UserLogoDataSet logoData find err:%+v", err) + return resp.SetStatus(basic.CodeApiErr) + } + + // 返回成功的响应和上传URL + return resp.SetStatus(basic.CodeOK, map[string]interface{}{ + "user_material_id": materialInfo.Id, + }) +} + +// 处理逻辑后 w,r 如:重定向, resp 必须重新处理 +// func (l *UserLogoDataSetLogic) 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 00d9fa47..7f95ec3f 100644 --- a/server/home-user-auth/internal/types/types.go +++ b/server/home-user-auth/internal/types/types.go @@ -5,6 +5,10 @@ import ( "fusenapi/utils/basic" ) +type UserLogoDataSetReq struct { + LogoDataId int64 `form:"logo_data_id"` +} + type UserLogoTemplateTagSetReq struct { TemplateTag string `form:"template_tag"` TemplateTagColorIndex int64 `form:"template_tag_color_index"` diff --git a/server_api/home-user-auth.api b/server_api/home-user-auth.api index 5bc06711..ca794534 100644 --- a/server_api/home-user-auth.api +++ b/server_api/home-user-auth.api @@ -10,60 +10,70 @@ info ( import "basic.api" service home-user-auth { - + // @handler UserRegisterHandler // post /api/user/register(RequestUserRegister) returns (response); //用户字体 @handler UserFontsHandler get /api/user/fonts(request) returns (response); - + //状态配置 @handler UserStatusConfigHandler get /api/user/status-config(request) returns (response); - + //联系服务 @handler UserContactServiceHandler post /api/user/contact-service (RequestContactService) returns (response); - + // @handler UserOderListHandler // get /api/user/order-list(RequestOrderId) returns (response); //删除订单 @handler UserOderDeleteHandler post /api/user/order-delete(RequestOrderId) returns (response); - + //订单列表 @handler UserOrderListHandler get /api/user/order-list (UserOrderListReq) returns (response); - + //删除订单 @handler UserOrderDeleteHandler get /api/user/order-delete (UserOrderDeleteReq) returns (response); - + //取消订单 @handler UserOrderCancelHandler get /api/user/order-cancel (UserOrderCancelReq) returns (response); - + // 用户logo列表 @handler UserLogoListHandler get /api/user/logo-list (UserLogoListReq) returns (response); - + // 再来一单 @handler UserAgainOrderHandler get /api/user/one-more-order (UserAgainOrderReq) returns (response); - + // 保存商户信息 @handler UserInfoSetHandler post /api/user/set_user_info (UserInfoSetReq) returns (response); - + // 用户logo设置当前 @handler UserLogoSetHandler post /api/user/logo-set (UserLogoSetReq) returns (response); - + // 用户logo模版信息 @handler UserLogoTemplateTagSetHandler post /api/user/logo-templatetag-set (UserLogoTemplateTagSetReq) returns (response); + + // 用户logo数据设置用户logo素材 + @handler UserLogoDataSetHandler + post /api/user/logo-data-set (UserLogoDataSetReq) returns (response); } +type ( + UserLogoDataSetReq { + LogoDataId int64 `form:"logo_data_id"` + } +) + type ( UserLogoTemplateTagSetReq { TemplateTag string `form:"template_tag"`