From 3a6ab7a666858d44b8ca5bcae2ea4d04c5e0385a Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Fri, 14 Jul 2023 15:57:27 +0800 Subject: [PATCH] fix --- .../gmodel/fs_product_render_design_logic.go | 14 ++-- model/gmodel/fs_tags_gen.go | 4 +- .../handler/getrenderdesignhandler.go | 78 +++++++++++++++++++ server/product/internal/handler/routes.go | 5 ++ .../internal/logic/getproductinfologic.go | 2 +- .../internal/logic/getrenderdesignlogic.go | 73 +++++++++++++++++ server/product/internal/types/types.go | 13 ++++ server_api/product.api | 17 ++++ 8 files changed, 198 insertions(+), 8 deletions(-) create mode 100644 server/product/internal/handler/getrenderdesignhandler.go create mode 100644 server/product/internal/logic/getrenderdesignlogic.go diff --git a/model/gmodel/fs_product_render_design_logic.go b/model/gmodel/fs_product_render_design_logic.go index 1b8b66b8..ffd6bf6c 100644 --- a/model/gmodel/fs_product_render_design_logic.go +++ b/model/gmodel/fs_product_render_design_logic.go @@ -6,14 +6,18 @@ import "context" type FindOneRenderDesignByParamsReq struct { ClientNo *string + Sn *string UserId *int64 - SortType int + OrderBy string Fields string Id *int } func (r *FsProductRenderDesignModel) FindOneRenderDesignByParams(ctx context.Context, req FindOneRenderDesignByParamsReq) (resp *FsProductRenderDesign, err error) { db := r.db.WithContext(ctx).Model(&FsProductRenderDesign{}) + if req.Sn != nil { + db = db.Where("`sn` = ?", *req.Sn) + } if req.ClientNo != nil { db = db.Where("`client_no` = ?", *req.ClientNo) } @@ -26,11 +30,11 @@ func (r *FsProductRenderDesignModel) FindOneRenderDesignByParams(ctx context.Con if req.Fields != "" { db = db.Select(req.Fields) } - switch req.SortType { - case 1: //id asc + switch req.OrderBy { + case "": //id asc db = db.Order("`id` ASC") - case 2: //id desc - db = db.Order("`id` DESC") + default: + db = db.Order(req.OrderBy) } err = db.Take(&resp).Error return resp, err diff --git a/model/gmodel/fs_tags_gen.go b/model/gmodel/fs_tags_gen.go index d38c9f64..041c2577 100644 --- a/model/gmodel/fs_tags_gen.go +++ b/model/gmodel/fs_tags_gen.go @@ -7,17 +7,17 @@ import ( // fs_tags 产品分类表 type FsTags struct { Id int64 `gorm:"primary_key;default:0;auto_increment;" json:"id"` // ID + LevelPrefix *string `gorm:"default:'';" json:"level_prefix"` // Title *string `gorm:"default:'';" json:"title"` // 标题 Level *int64 `gorm:"default:0;" json:"level"` // 层级、分类 1 => 二维码分类 ClickNum *int64 `gorm:"default:0;" json:"click_num"` // 点击次数 Sort *int64 `gorm:"default:0;" json:"sort"` // 排序(从大到小) CreateAt *int64 `gorm:"default:0;" json:"create_at"` // 创建时间 - Icon *string `gorm:"default:'';" json:"icon"` // + Icon *string `gorm:"default:'';" json:"icon"` // 标签图标 Status *int64 `gorm:"default:1;" json:"status"` // 状态 1:可用 Description *string `gorm:"default:'';" json:"description"` // 介绍 Seo RecommendProduct *string `gorm:"default:'';" json:"recommend_product"` // RecommendProductSort *string `gorm:"default:'';" json:"recommend_product_sort"` // - LevelPrefix *string `gorm:"default:'';" json:"level_prefix"` //归属等级前缀 } type FsTagsModel struct{ db *gorm.DB } diff --git a/server/product/internal/handler/getrenderdesignhandler.go b/server/product/internal/handler/getrenderdesignhandler.go new file mode 100644 index 00000000..2584153c --- /dev/null +++ b/server/product/internal/handler/getrenderdesignhandler.go @@ -0,0 +1,78 @@ +package handler + +import ( + "errors" + "net/http" + + "github.com/zeromicro/go-zero/core/logx" + "github.com/zeromicro/go-zero/rest/httpx" + + "fusenapi/utils/auth" + "fusenapi/utils/basic" + + "fusenapi/server/product/internal/logic" + "fusenapi/server/product/internal/svc" + "fusenapi/server/product/internal/types" +) + +func GetRenderDesignHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + + var ( + // 定义错误变量 + err error + // 定义用户信息变量 + userinfo *auth.UserInfo + ) + // 解析JWT token,并对空用户进行判断 + claims, err := svcCtx.ParseJwtToken(r) + // 如果解析JWT token出错,则返回未授权的JSON响应并记录错误消息 + if err != nil { + httpx.OkJsonCtx(r.Context(), w, &basic.Response{ + Code: 401, // 返回401状态码,表示未授权 + Message: "unauthorized", // 返回未授权信息 + }) + logx.Info("unauthorized:", err.Error()) // 记录错误日志 + return + } + + if claims != nil { + // 从token中获取对应的用户信息 + userinfo, err = auth.GetUserInfoFormMapClaims(claims) + // 如果获取用户信息出错,则返回未授权的JSON响应并记录错误消息 + if err != nil { + httpx.OkJsonCtx(r.Context(), w, &basic.Response{ + Code: 401, + Message: "unauthorized", + }) + logx.Info("unauthorized:", err.Error()) + return + } + } else { + // 如果claims为nil,则认为用户身份为白板用户 + userinfo = &auth.UserInfo{UserId: 0, GuestId: 0} + } + + var req types.GetRenderDesignReq + // 如果端点有请求结构体,则使用httpx.Parse方法从HTTP请求体中解析请求数据 + if err := httpx.Parse(r, &req); err != nil { + httpx.OkJsonCtx(r.Context(), w, &basic.Response{ + Code: 510, + Message: "parameter error", + }) + logx.Info(err) + return + } + // 创建一个业务逻辑层实例 + l := logic.NewGetRenderDesignLogic(r.Context(), svcCtx) + resp := l.GetRenderDesign(&req, userinfo) + // 如果响应不为nil,则使用httpx.OkJsonCtx方法返回JSON响应; + if resp != nil { + httpx.OkJsonCtx(r.Context(), w, resp) + } else { + err := errors.New("server logic is error, resp must not be nil") + httpx.ErrorCtx(r.Context(), w, err) + logx.Error(err) + } + } +} diff --git a/server/product/internal/handler/routes.go b/server/product/internal/handler/routes.go index 059eb87b..a65b0205 100644 --- a/server/product/internal/handler/routes.go +++ b/server/product/internal/handler/routes.go @@ -62,6 +62,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { Path: "/api/product/tag_product_list", Handler: GetTagProductListHandler(serverCtx), }, + { + Method: http.MethodGet, + Path: "/api/product/render_design", + Handler: GetRenderDesignHandler(serverCtx), + }, }, ) } diff --git a/server/product/internal/logic/getproductinfologic.go b/server/product/internal/logic/getproductinfologic.go index caaa7c21..8047752a 100644 --- a/server/product/internal/logic/getproductinfologic.go +++ b/server/product/internal/logic/getproductinfologic.go @@ -431,7 +431,7 @@ func (l *GetProductInfoLogic) getRenderDesign(clientNo string) interface{} { renderDesign, err := l.svcCtx.AllModels.FsProductRenderDesign.FindOneRenderDesignByParams(l.ctx, gmodel.FindOneRenderDesignByParamsReq{ ClientNo: &clientNo, Fields: "id,info,material_id,optional_id,size_id,template_id", - SortType: 2, + OrderBy: "`id` DESC", }) if err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { diff --git a/server/product/internal/logic/getrenderdesignlogic.go b/server/product/internal/logic/getrenderdesignlogic.go new file mode 100644 index 00000000..1645207d --- /dev/null +++ b/server/product/internal/logic/getrenderdesignlogic.go @@ -0,0 +1,73 @@ +package logic + +import ( + "encoding/json" + "errors" + "fusenapi/model/gmodel" + "fusenapi/utils/auth" + "fusenapi/utils/basic" + "gorm.io/gorm" + "strings" + + "context" + + "fusenapi/server/product/internal/svc" + "fusenapi/server/product/internal/types" + + "github.com/zeromicro/go-zero/core/logx" +) + +type GetRenderDesignLogic struct { + logx.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewGetRenderDesignLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetRenderDesignLogic { + return &GetRenderDesignLogic{ + Logger: logx.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +func (l *GetRenderDesignLogic) GetRenderDesign(req *types.GetRenderDesignReq, userinfo *auth.UserInfo) (resp *basic.Response) { + req.Sn = strings.Trim(req.Sn, " ") + if req.Sn == "" { + return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "err param:sn is empty") + } + renderDesign, err := l.svcCtx.AllModels.FsProductRenderDesign.FindOneRenderDesignByParams(l.ctx, gmodel.FindOneRenderDesignByParamsReq{ + Sn: &req.Sn, + Fields: "id,info,material_id,optional_id,size_id,template_id", + OrderBy: "`id` DESC", + }) + if err != nil { + if errors.Is(err, gorm.ErrRecordNotFound) { + return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "render design is not exists") + } + logx.Error(err) + return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get render design info") + } + var color []string + if renderDesign.LogoColor != nil && *renderDesign.LogoColor != "" { + if err = json.Unmarshal([]byte(*renderDesign.LogoColor), &color); err != nil { + logx.Error(err) + return resp.SetStatusWithMessage(basic.CodeJsonErr, "failed to parse render logo color") + } + } + var info interface{} + if renderDesign.Info != nil && *renderDesign.Info != "" { + if err = json.Unmarshal([]byte(*renderDesign.Info), &info); err != nil { + logx.Error(err) + return resp.SetStatusWithMessage(basic.CodeJsonErr, "failed to parse render design info") + } + } + return resp.SetStatusWithMessage(basic.CodeOK, "success", types.GetRenderDesignRsp{ + Id: renderDesign.Id, + Info: info, + OptionalId: *renderDesign.OptionalId, + SizeId: *renderDesign.SizeId, + TemplateId: *renderDesign.TemplateId, + LogoColor: color, + }) +} diff --git a/server/product/internal/types/types.go b/server/product/internal/types/types.go index df63cc23..c8abf939 100644 --- a/server/product/internal/types/types.go +++ b/server/product/internal/types/types.go @@ -280,6 +280,19 @@ type TagProduct struct { CoverDefault string `json:"cover_default"` } +type GetRenderDesignReq struct { + Sn string `form:"sn"` +} + +type GetRenderDesignRsp struct { + Id int64 `json:"id"` + Info interface{} `json:"info"` + OptionalId int64 `json:"optional_id"` + SizeId int64 `json:"size_id"` + TemplateId int64 `json:"template_id"` + LogoColor []string `json:"logo_color"` +} + type Request struct { } diff --git a/server_api/product.api b/server_api/product.api index b522dcb8..9fbf6e05 100644 --- a/server_api/product.api +++ b/server_api/product.api @@ -40,6 +40,11 @@ service product { //获取分类产品列表 @handler GetTagProductListHandler get /api/product/tag_product_list(GetTagProductListReq) returns (response); + //*********************产品详情分解接口开始*********************** + //获取云渲染设计方案信息 + @handler GetRenderDesignHandler + get /api/product/render_design(GetRenderDesignReq) returns (response); + //*********************产品详情分解接口结束*********************** } //获取产品列表 @@ -292,4 +297,16 @@ type TagProduct { SizeNum uint32 `json:"size_num"` MiniPrice int64 `json:"mini_price"` CoverDefault string `json:"cover_default"` +} +//获取云渲染设计方案信息 +type GetRenderDesignReq { + Sn string `form:"sn"` +} +type GetRenderDesignRsp { + Id int64 `json:"id"` + Info interface{} `json:"info"` + OptionalId int64 `json:"optional_id"` + SizeId int64 `json:"size_id"` + TemplateId int64 `json:"template_id"` + LogoColor []string `json:"logo_color"` } \ No newline at end of file