From 0f96eaac30a2d5dce217ada28c6a3b06d4d8dd3c Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Wed, 12 Jul 2023 15:27:09 +0800 Subject: [PATCH 01/11] fix --- server/product/internal/logic/getproductinfologic.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/server/product/internal/logic/getproductinfologic.go b/server/product/internal/logic/getproductinfologic.go index c3108ac3..caaa7c21 100644 --- a/server/product/internal/logic/getproductinfologic.go +++ b/server/product/internal/logic/getproductinfologic.go @@ -123,14 +123,11 @@ func (l *GetProductInfoLogic) GetProductInfo(req *types.GetProductInfoReq, useri for _, v := range sizeList { model3dInfo, ok := mapModel3dWithSizeIdIndex[v.Id] if !ok { - fmt.Println("没有模型:", v.Id) continue } if _, ok = mapTemplateModelId[model3dInfo.Id]; !ok { - fmt.Println("没有模板:", v.Id) continue } - fmt.Println("剩下的:", v.Id) var title types.SizeTitle if err = json.Unmarshal([]byte(*v.Title), &title); err != nil { logx.Error(err) From e4925c21947826840c2476eb9a03cb67d9e43cb9 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Thu, 13 Jul 2023 11:36:02 +0800 Subject: [PATCH 02/11] fix --- model/gmodel/fs_product_logic.go | 10 ++++++---- .../internal/logic/getproductlistlogic.go | 18 ++++++++++++++---- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/model/gmodel/fs_product_logic.go b/model/gmodel/fs_product_logic.go index d0f1850b..0afa4100 100755 --- a/model/gmodel/fs_product_logic.go +++ b/model/gmodel/fs_product_logic.go @@ -44,7 +44,7 @@ func (p *FsProductModel) GetProductListByIdsWithoutStatus(ctx context.Context, p } return } -func (p *FsProductModel) GetProductListByTypeIds(ctx context.Context, productTypes []int64, sort string) (resp []FsProduct, err error) { +func (p *FsProductModel) GetProductListByTypeIds(ctx context.Context, productTypes []int64, page int, limit int, sort string) (resp []FsProduct, total int64, err error) { if len(productTypes) == 0 { return } @@ -55,11 +55,13 @@ func (p *FsProductModel) GetProductListByTypeIds(ctx context.Context, productTyp case "sort-desc": db = db.Order("`sort` DESC") } - err = db.Find(&resp).Error + err = db.Count(&total).Error if err != nil { - return nil, err + return nil, 0, err } - return + offset := (page - 1) * limit + err = db.Offset(offset).Limit(limit).Find(&resp).Error + return resp, total, err } func (p *FsProductModel) GetRandomProductList(ctx context.Context, limit int) (resp []FsProduct, err error) { err = p.db.WithContext(ctx).Model(&FsProduct{}). diff --git a/server/product/internal/logic/getproductlistlogic.go b/server/product/internal/logic/getproductlistlogic.go index 3f370f86..ed7bfa8e 100644 --- a/server/product/internal/logic/getproductlistlogic.go +++ b/server/product/internal/logic/getproductlistlogic.go @@ -15,6 +15,7 @@ import ( "github.com/zeromicro/go-zero/core/logx" "github.com/zeromicro/go-zero/core/stores/sqlc" "gorm.io/gorm" + "math" "sort" "strings" ) @@ -44,13 +45,14 @@ func (l *GetProductListLogic) GetProductList(req *types.GetProductListReq, useri } return resp.SetStatusWithMessage(basic.CodeOK, "success", demo) } - if req.Page <= 0 { - req.Page = 1 + if req.Page <= 0 && req.Page > 10000 { + req.Page = constants.DEFAULT_PAGE } //获取合适尺寸 if req.Size > 0 { req.Size = image.GetCurrentSize(req.Size) } + pageSize := constants.DEFAULT_PAGE_SIZE //查询用户信息(不用判断存在) userModel := gmodel.NewFsUserModel(l.svcCtx.MysqlConn) user, err := userModel.FindUserById(l.ctx, userinfo.UserId) @@ -60,7 +62,7 @@ func (l *GetProductListLogic) GetProductList(req *types.GetProductListReq, useri } //查询符合的产品列表 productModel := gmodel.NewFsProductModel(l.svcCtx.MysqlConn) - productList, err := productModel.GetProductListByTypeIds(l.ctx, []int64{req.Cid}, "sort-desc") + productList, total, err := productModel.GetProductListByTypeIds(l.ctx, []int64{req.Cid}, int(req.Page), pageSize, "sort-desc") if err != nil { logx.Error(err) return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get product list") @@ -172,6 +174,14 @@ func (l *GetProductListLogic) GetProductList(req *types.GetProductListReq, useri return resp.SetStatusWithMessage(basic.CodeOK, "success", types.GetProductListRsp{ Ob: types.Ob{ Items: itemList, - }, TypeName: *tagInfo.Title, Description: *tagInfo.Description, + Meta: types.Meta{ + TotalCount: total, + PageCount: int64(math.Ceil(float64(total) / float64(pageSize))), + CurrentPage: int(req.Page), + PerPage: pageSize, + }, + }, + TypeName: *tagInfo.Title, + Description: *tagInfo.Description, }) } From 6cd5a6d0e86f23ce01b61ad7c6588f064141cbeb Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Thu, 13 Jul 2023 19:05:13 +0800 Subject: [PATCH 03/11] fix --- model/gmodel/fs_product_logic.go | 32 +++ model/gmodel/fs_tags_gen.go | 1 + model/gmodel/fs_tags_logic.go | 24 ++ .../handler/gettagproductlisthandler.go | 78 ++++++ server/product/internal/handler/routes.go | 5 + .../internal/logic/gettagproductlistlogic.go | 248 ++++++++++++++++++ server/product/internal/types/types.go | 37 ++- server_api/product.api | 36 ++- 8 files changed, 454 insertions(+), 7 deletions(-) create mode 100644 server/product/internal/handler/gettagproductlisthandler.go create mode 100644 server/product/internal/logic/gettagproductlistlogic.go diff --git a/model/gmodel/fs_product_logic.go b/model/gmodel/fs_product_logic.go index 0afa4100..6328daa4 100755 --- a/model/gmodel/fs_product_logic.go +++ b/model/gmodel/fs_product_logic.go @@ -85,3 +85,35 @@ func (p *FsProductModel) GetRandomProductListInIds(ctx context.Context, ids []in err = db.Find(&resp).Error return resp, err } + +type GetProductListByParamsReq struct { + Type []int64 + IsDel *int64 + IsShelf *int64 + Status *int64 + OrderBy string +} + +func (p *FsProductModel) GetProductListByParams(ctx context.Context, req GetProductListByParamsReq) (resp []FsProduct, err error) { + db := p.db.WithContext(ctx).Model(&FsProduct{}) + if len(req.Type) > 0 { + db = db.Where("`type` in (?)", req.Type) + } + if req.IsDel != nil { + db = db.Where("`is_del` = ?", *req.IsDel) + } + if req.IsShelf != nil { + db = db.Where("`is_shelf` = ?", *req.IsShelf) + } + if req.Status != nil { + db = db.Where("`status` = ?", *req.Status) + } + switch req.OrderBy { + case "": + db = db.Order("`id` DESC") + default: + db = db.Order(req.OrderBy) + } + err = db.Find(&resp).Error + return resp, err +} diff --git a/model/gmodel/fs_tags_gen.go b/model/gmodel/fs_tags_gen.go index 9d72c58f..d38c9f64 100644 --- a/model/gmodel/fs_tags_gen.go +++ b/model/gmodel/fs_tags_gen.go @@ -17,6 +17,7 @@ type FsTags struct { 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/model/gmodel/fs_tags_logic.go b/model/gmodel/fs_tags_logic.go index 1a77ccd5..c45bcc05 100755 --- a/model/gmodel/fs_tags_logic.go +++ b/model/gmodel/fs_tags_logic.go @@ -29,3 +29,27 @@ func (t *FsTagsModel) GetAllByLevel(ctx context.Context, level int) (resp []FsTa } return } + +type GetAllTagByParamsReq struct { + Ids []int64 + Status *int64 + OrderBy string +} + +func (t *FsTagsModel) GetAllTagByParams(ctx context.Context, req GetAllTagByParamsReq) (resp []FsTags, err error) { + db := t.db.WithContext(ctx).Model(&FsTags{}) + if len(req.Ids) > 0 { + db = db.Where("`id` in (?)", req.Ids) + } + if req.Status != nil { + db = db.Where("`status` = ?", *req.Status) + } + switch req.OrderBy { + case "": + db = db.Order("`id` DESC") + default: + db = db.Order(req.OrderBy) + } + err = db.Find(&resp).Error + return resp, err +} diff --git a/server/product/internal/handler/gettagproductlisthandler.go b/server/product/internal/handler/gettagproductlisthandler.go new file mode 100644 index 00000000..18a2a96c --- /dev/null +++ b/server/product/internal/handler/gettagproductlisthandler.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 GetTagProductListHandler(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.GetTagProductListReq + // 如果端点有请求结构体,则使用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.NewGetTagProductListLogic(r.Context(), svcCtx) + resp := l.GetTagProductList(&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 31b80260..059eb87b 100644 --- a/server/product/internal/handler/routes.go +++ b/server/product/internal/handler/routes.go @@ -57,6 +57,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { Path: "/api/product/recommand", Handler: GetRecommandProductListHandler(serverCtx), }, + { + Method: http.MethodGet, + Path: "/api/product/tag_product_list", + Handler: GetTagProductListHandler(serverCtx), + }, }, ) } diff --git a/server/product/internal/logic/gettagproductlistlogic.go b/server/product/internal/logic/gettagproductlistlogic.go new file mode 100644 index 00000000..3868337d --- /dev/null +++ b/server/product/internal/logic/gettagproductlistlogic.go @@ -0,0 +1,248 @@ +package logic + +import ( + "errors" + "fusenapi/model/gmodel" + "fusenapi/utils/auth" + "fusenapi/utils/basic" + "fusenapi/utils/format" + "fusenapi/utils/image" + "gorm.io/gorm" + "sort" + "strings" + + "context" + + "fusenapi/server/product/internal/svc" + "fusenapi/server/product/internal/types" + + "github.com/zeromicro/go-zero/core/logx" +) + +type GetTagProductListLogic struct { + logx.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewGetTagProductListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetTagProductListLogic { + return &GetTagProductListLogic{ + Logger: logx.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +func (l *GetTagProductListLogic) GetTagProductList(req *types.GetTagProductListReq, userinfo *auth.UserInfo) (resp *basic.Response) { + //获取合适尺寸 + if req.Size > 0 { + req.Size = image.GetCurrentSize(req.Size) + } + //查询用户信息(不用判断存在) + user, err := l.svcCtx.AllModels.FsUser.FindUserById(l.ctx, userinfo.UserId) + if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) { + logx.Error(err) + return resp.SetStatusWithMessage(basic.CodeServiceErr, "get user info err") + } + //查询分类列表 + tStatus := int64(1) + tReq := gmodel.GetAllTagByParamsReq{ + Status: &tStatus, + OrderBy: "`sort` DESC", + } + //传入分类id + if req.Cid > 0 { + tReq.Ids = []int64{req.Cid} + } + tagList, err := l.svcCtx.AllModels.FsTags.GetAllTagByParams(l.ctx, tReq) + if err != nil { + logx.Error(err) + return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get tag list") + } + if len(tagList) == 0 { + return resp.SetStatusWithMessage(basic.CodeOK, "success", []interface{}{}) + } + typeIds := make([]int64, 0, len(tagList)) + for _, v := range tagList { + typeIds = append(typeIds, v.Id) + } + //查询符合的产品列表 + pIsDel := int64(0) + pStatus := int64(1) + pIsShelf := int64(1) + pReq := gmodel.GetProductListByParamsReq{ + Type: typeIds, + IsDel: &pIsDel, + IsShelf: &pIsShelf, + Status: &pStatus, + OrderBy: "`sort` DESC", + } + //获取产品列表 + productList, err := l.svcCtx.AllModels.FsProduct.GetProductListByParams(l.ctx, pReq) + if err != nil { + logx.Error(err) + return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get product list") + } + productLen := len(productList) + if productLen == 0 { + return resp.SetStatusWithMessage(basic.CodeOK, "success") + } + //提取产品ids + productIds := make([]int64, 0, productLen) + for _, v := range productList { + productIds = append(productIds, v.Id) + } + productPriceList, err := l.svcCtx.AllModels.FsProductPrice.GetSimplePriceListByProductIds(l.ctx, productIds) + if err != nil { + logx.Error(err) + return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get product min price list") + } + //存储产品最小价格 + mapProductMinPrice := make(map[int64]int64) + for _, v := range productPriceList { + priceStrSlic := strings.Split(v.Price, ",") + priceSlice, err := format.StrSlicToIntSlice(priceStrSlic) + if err != nil { + logx.Error(err) + return resp.SetStatusWithMessage(basic.CodeServiceErr, err.Error()) + } + if len(priceSlice) == 0 { + continue + } + sort.Ints(priceSlice) + mapProductMinPrice[v.ProductId] = int64(priceSlice[0]) + } + //获取模板 + productTemplatesV2, err := l.svcCtx.AllModels.FsProductTemplateV2.FindAllByProductIds(l.ctx, productIds) + if err != nil { + logx.Error(err) + return resp.SetStatusWithMessage(basic.CodeServiceErr, "get product template_v2 err") + } + mapProductTemplate := make(map[int64]struct{}) + for _, v := range productTemplatesV2 { + mapProductTemplate[*v.ProductId] = struct{}{} + } + //获取产品尺寸数量 + productSizeCountList, err := l.svcCtx.AllModels.FsProductSize.GetGroupProductSizeByStatus(l.ctx, productIds, 1) + if err != nil { + logx.Error(err) + return resp.SetStatusWithMessage(basic.CodeServiceErr, "get product size count err") + } + mapProductSizeCount := make(map[int64]int64) + for _, v := range productSizeCountList { + mapProductSizeCount[v.ProductId] = v.Num + } + mapTagLevel := make(map[string]types.TagItem) + minLevel := int64(0) //记录最小等级数字 + for _, tagInfo := range tagList { + if (minLevel == 0 && *tagInfo.Level > 0) || (minLevel > *tagInfo.Level) { + minLevel = *tagInfo.Level + } + productListRsp := l.getTagProductList(getTagProductListReq{ + TagInfo: tagInfo, + ProductList: productList, + MapProductMinPrice: mapProductMinPrice, + MapProductTemplate: mapProductTemplate, + MapProductSizeCount: mapProductSizeCount, + Size: req.Size, + User: user, + }) + //加入分类 + tagTem := types.TagItem{ + TagProductList: productListRsp, + TypeName: *tagInfo.Title, + TypeId: tagInfo.Id, + Level: *tagInfo.Level, + BelongPrefix: *tagInfo.LevelPrefix, + Description: *tagInfo.Description, + ChildTagList: []types.TagItem{}, + } + //当前tag保存入map + mapTagLevel[*tagInfo.LevelPrefix] = tagTem + } + //组装等级从属关系 + for prefix, tagItem := range mapTagLevel { + //查看有没有直接父级,有的话则把当前的append到父级的ChildTagList中 + prefixSlice := strings.Split(prefix, "/") + //等于1表示自己是最上级 + if len(prefixSlice) == 1 { + continue + } + parentPrefix := strings.Join(prefixSlice[:len(prefixSlice)-1], "/") + if parent, ok := mapTagLevel[parentPrefix]; ok { + parent.ChildTagList = append(parent.ChildTagList, tagItem) + mapTagLevel[parentPrefix] = parent + } + } + //最终值提取最高级别那一层出来 + finalSlice := make([]interface{}, 0, len(mapTagLevel)) + for _, v := range mapTagLevel { + if v.Level != minLevel { + continue + } + finalSlice = append(finalSlice, v) + } + return resp.SetStatusWithMessage(basic.CodeOK, "success", finalSlice) +} + +type getTagProductListReq struct { + TagInfo gmodel.FsTags + ProductList []gmodel.FsProduct + MapProductMinPrice map[int64]int64 + MapProductTemplate map[int64]struct{} + MapProductSizeCount map[int64]int64 + Size uint32 + User gmodel.FsUser +} + +// 获取对应tag的产品列表 +func (l *GetTagProductListLogic) getTagProductList(req getTagProductListReq) (productListRsp []types.TagProduct) { + //默认给50个容量 + productListRsp = make([]types.TagProduct, 0, 50) + for _, productInfo := range req.ProductList { + //不属于一个类型则跳过 + if *productInfo.Type != req.TagInfo.Id { + continue + } + minPrice, ok := req.MapProductMinPrice[productInfo.Id] + _, tmpOk := req.MapProductTemplate[productInfo.Id] + //无最小价格则不显示 || 没有模板也不显示 + if !ok || !tmpOk { + continue + } + sizeNum := int64(0) + if mapSizeNum, ok := req.MapProductSizeCount[productInfo.Id]; ok { + sizeNum = mapSizeNum + } + item := types.TagProduct{ + ProductId: productInfo.Id, + Sn: *productInfo.Sn, + Title: *productInfo.Title, + Intro: *productInfo.Intro, + IsEnv: *productInfo.IsProtection, + IsMicro: *productInfo.IsMicrowave, + SizeNum: uint32(sizeNum), + MiniPrice: minPrice, + } + //千人千面处理 + r := image.ThousandFaceImageFormatReq{ + Size: int(req.Size), + IsThousandFace: 0, + Cover: *productInfo.Cover, + CoverImg: *productInfo.CoverImg, + CoverDefault: *productInfo.CoverImg, + ProductId: productInfo.Id, + UserId: req.User.Id, + } + if req.User.Id != 0 { + r.IsThousandFace = int(*req.User.IsThousandFace) + } + image.ThousandFaceImageFormat(&r) + item.Cover = r.Cover + item.CoverImg = r.CoverImg + item.CoverDefault = r.CoverDefault + //加入分类产品切片 + productListRsp = append(productListRsp, item) + } + return productListRsp +} diff --git a/server/product/internal/types/types.go b/server/product/internal/types/types.go index f58d7a3f..65880de8 100644 --- a/server/product/internal/types/types.go +++ b/server/product/internal/types/types.go @@ -23,10 +23,6 @@ type Ob struct { Meta Meta `json:"_meta"` } -type HrefUrl struct { - Href string `json:"href"` -} - type Items struct { Id int64 `json:"id"` Sn string `json:"sn"` @@ -248,6 +244,39 @@ type GetRecommandProductListRsp struct { Intro string `json:"intro"` } +type GetTagProductListReq struct { + Cid int64 `form:"cid,optional"` //分类id + Size uint32 `form:"size,optional"` //尺寸 +} + +type GetTagProductListRsp struct { + TagList []TagItem `json:"tag_list"` +} + +type TagItem struct { + TypeName string `json:"typeName"` + TypeId int64 `json:"type_id"` + Description string `json:"description"` + Level int64 `json:"level"` + BelongPrefix string `json:"belong_prefix"` + TagProductList []TagProduct `json:"tag_product_list"` + ChildTagList []TagItem `json:"child_tag_list"` +} + +type TagProduct struct { + ProductId int64 `json:"product_id"` + Sn string `json:"sn"` + Title string `json:"title"` + Cover string `json:"cover"` + Intro string `json:"intro"` + CoverImg string `json:"cover_img"` + IsEnv int64 `json:"isEnv"` + IsMicro int64 `json:"isMicro"` + SizeNum uint32 `json:"sizeNum"` + MiniPrice int64 `json:"miniPrice"` + CoverDefault string `json:"coverDefault"` +} + type Request struct { } diff --git a/server_api/product.api b/server_api/product.api index e59a9b36..ae8ab5a2 100644 --- a/server_api/product.api +++ b/server_api/product.api @@ -37,6 +37,9 @@ service product { //获取详情页推荐产品列表 @handler GetRecommandProductListHandler get /api/product/recommand (GetRecommandProductListReq) returns (response); + //获取分类产品列表 + @handler GetTagProductListHandler + get /api/product/tag_product_list(GetTagProductListReq) returns (response); } //获取产品列表 @@ -55,9 +58,6 @@ type Ob { Items []Items `json:"items"` Meta Meta `json:"_meta"` } -type HrefUrl { - Href string `json:"href"` -} type Items { Id int64 `json:"id"` Sn string `json:"sn"` @@ -259,4 +259,34 @@ type GetRecommandProductListRsp { CoverImg string `json:"cover_img"` CoverDefault string `json:"cover_default"` Intro string `json:"intro"` +} +//获取分类产品列表 +type GetTagProductListReq { + Cid int64 `form:"cid,optional"` //分类id + Size uint32 `form:"size,optional"` //尺寸 +} +type GetTagProductListRsp { + TagList []TagItem `json:"tag_list"` +} +type TagItem { + TypeName string `json:"typeName"` + TypeId int64 `json:"type_id"` + Description string `json:"description"` + Level int64 `json:"level"` + BelongPrefix string `json:"belong_prefix"` + TagProductList []TagProduct `json:"tag_product_list"` + ChildTagList []TagItem `json:"child_tag_list"` +} +type TagProduct { + ProductId int64 `json:"product_id"` + Sn string `json:"sn"` + Title string `json:"title"` + Cover string `json:"cover"` + Intro string `json:"intro"` + CoverImg string `json:"cover_img"` + IsEnv int64 `json:"isEnv"` + IsMicro int64 `json:"isMicro"` + SizeNum uint32 `json:"sizeNum"` + MiniPrice int64 `json:"miniPrice"` + CoverDefault string `json:"coverDefault"` } \ No newline at end of file From 52f65afe1a7c3c3beeb057ab34942a489bd812cf Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Thu, 13 Jul 2023 19:06:02 +0800 Subject: [PATCH 04/11] fix --- server/product/internal/logic/gettagproductlistlogic.go | 1 + 1 file changed, 1 insertion(+) diff --git a/server/product/internal/logic/gettagproductlistlogic.go b/server/product/internal/logic/gettagproductlistlogic.go index 3868337d..667084c9 100644 --- a/server/product/internal/logic/gettagproductlistlogic.go +++ b/server/product/internal/logic/gettagproductlistlogic.go @@ -138,6 +138,7 @@ func (l *GetTagProductListLogic) GetTagProductList(req *types.GetTagProductListR if (minLevel == 0 && *tagInfo.Level > 0) || (minLevel > *tagInfo.Level) { minLevel = *tagInfo.Level } + //获取分类产品列表 productListRsp := l.getTagProductList(getTagProductListReq{ TagInfo: tagInfo, ProductList: productList, From 6d3a3b2dda6b6af71c9c486e45c76bf76604ffa4 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Fri, 14 Jul 2023 12:25:02 +0800 Subject: [PATCH 05/11] fix --- .../handler/gettagproductlisthandler.go | 62 ++++++++-------- .../internal/logic/gettagproductlistlogic.go | 70 +++++++++++-------- server/product/internal/types/types.go | 8 ++- server_api/product.api | 8 ++- 4 files changed, 82 insertions(+), 66 deletions(-) diff --git a/server/product/internal/handler/gettagproductlisthandler.go b/server/product/internal/handler/gettagproductlisthandler.go index 18a2a96c..1952ac42 100644 --- a/server/product/internal/handler/gettagproductlisthandler.go +++ b/server/product/internal/handler/gettagproductlisthandler.go @@ -17,41 +17,41 @@ import ( func GetTagProductListHandler(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响应并记录错误消息 + /* + 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, - Message: "unauthorized", + Code: 401, // 返回401状态码,表示未授权 + Message: "unauthorized", // 返回未授权信息 }) - logx.Info("unauthorized:", err.Error()) + logx.Info("unauthorized:", err.Error()) // 记录错误日志 return } - } else { - // 如果claims为nil,则认为用户身份为白板用户 - userinfo = &auth.UserInfo{UserId: 0, GuestId: 0} - } + + 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.GetTagProductListReq // 如果端点有请求结构体,则使用httpx.Parse方法从HTTP请求体中解析请求数据 @@ -65,7 +65,7 @@ func GetTagProductListHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { } // 创建一个业务逻辑层实例 l := logic.NewGetTagProductListLogic(r.Context(), svcCtx) - resp := l.GetTagProductList(&req, userinfo) + resp := l.GetTagProductList(&req, &auth.UserInfo{39, 0}) // 如果响应不为nil,则使用httpx.OkJsonCtx方法返回JSON响应; if resp != nil { httpx.OkJsonCtx(r.Context(), w, resp) diff --git a/server/product/internal/logic/gettagproductlistlogic.go b/server/product/internal/logic/gettagproductlistlogic.go index 667084c9..9844130f 100644 --- a/server/product/internal/logic/gettagproductlistlogic.go +++ b/server/product/internal/logic/gettagproductlistlogic.go @@ -1,13 +1,11 @@ package logic import ( - "errors" "fusenapi/model/gmodel" "fusenapi/utils/auth" "fusenapi/utils/basic" "fusenapi/utils/format" "fusenapi/utils/image" - "gorm.io/gorm" "sort" "strings" @@ -39,11 +37,11 @@ func (l *GetTagProductListLogic) GetTagProductList(req *types.GetTagProductListR req.Size = image.GetCurrentSize(req.Size) } //查询用户信息(不用判断存在) - user, err := l.svcCtx.AllModels.FsUser.FindUserById(l.ctx, userinfo.UserId) + /*user, err := l.svcCtx.AllModels.FsUser.FindUserById(l.ctx, userinfo.UserId) if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) { logx.Error(err) return resp.SetStatusWithMessage(basic.CodeServiceErr, "get user info err") - } + }*/ //查询分类列表 tStatus := int64(1) tReq := gmodel.GetAllTagByParamsReq{ @@ -132,62 +130,77 @@ func (l *GetTagProductListLogic) GetTagProductList(req *types.GetTagProductListR for _, v := range productSizeCountList { mapProductSizeCount[v.ProductId] = v.Num } - mapTagLevel := make(map[string]types.TagItem) + mapTagLevel := make(map[string]*types.TagItem) minLevel := int64(0) //记录最小等级数字 for _, tagInfo := range tagList { - if (minLevel == 0 && *tagInfo.Level > 0) || (minLevel > *tagInfo.Level) { + if minLevel == 0 && *tagInfo.Level > 0 { + minLevel = *tagInfo.Level + } + if minLevel > *tagInfo.Level { minLevel = *tagInfo.Level } //获取分类产品列表 - productListRsp := l.getTagProductList(getTagProductListReq{ - TagInfo: tagInfo, + /*productListRsp := l.getTagProducts(getTagProductsReq{ + TagId: tagInfo.Id, ProductList: productList, MapProductMinPrice: mapProductMinPrice, MapProductTemplate: mapProductTemplate, MapProductSizeCount: mapProductSizeCount, Size: req.Size, User: user, - }) + })*/ //加入分类 tagTem := types.TagItem{ - TagProductList: productListRsp, - TypeName: *tagInfo.Title, - TypeId: tagInfo.Id, - Level: *tagInfo.Level, - BelongPrefix: *tagInfo.LevelPrefix, - Description: *tagInfo.Description, - ChildTagList: []types.TagItem{}, + //TagProductList: productListRsp, + TypeName: *tagInfo.Title, + TypeId: tagInfo.Id, + Level: *tagInfo.Level, + LevelPrefix: *tagInfo.LevelPrefix, + Icon: *tagInfo.Icon, + Sort: *tagInfo.Sort, + Description: *tagInfo.Description, + ChildTagList: make([]*types.TagItem, 0, 100), } //当前tag保存入map - mapTagLevel[*tagInfo.LevelPrefix] = tagTem + mapTagLevel[*tagInfo.LevelPrefix] = &tagTem } //组装等级从属关系 for prefix, tagItem := range mapTagLevel { - //查看有没有直接父级,有的话则把当前的append到父级的ChildTagList中 - prefixSlice := strings.Split(prefix, "/") - //等于1表示自己是最上级 - if len(prefixSlice) == 1 { + prefix = strings.Trim(prefix, " ") + //最上级没有父级 + if !strings.Contains(prefix, "/") { continue } + prefixSlice := strings.Split(prefix, "/") + //有父级 parentPrefix := strings.Join(prefixSlice[:len(prefixSlice)-1], "/") if parent, ok := mapTagLevel[parentPrefix]; ok { parent.ChildTagList = append(parent.ChildTagList, tagItem) + //排序 + sort.Slice(parent.ChildTagList, func(i, j int) bool { + return parent.ChildTagList[i].Sort < parent.ChildTagList[j].Sort + }) mapTagLevel[parentPrefix] = parent } } //最终值提取最高级别那一层出来 - finalSlice := make([]interface{}, 0, len(mapTagLevel)) + menus := make([]types.TagItem, 0, len(mapTagLevel)) for _, v := range mapTagLevel { if v.Level != minLevel { continue } - finalSlice = append(finalSlice, v) + menus = append(menus, *v) } - return resp.SetStatusWithMessage(basic.CodeOK, "success", finalSlice) + //排序 + sort.Slice(menus, func(i, j int) bool { + return menus[i].Sort < menus[j].Sort + }) + return resp.SetStatusWithMessage(basic.CodeOK, "success", menus) } -type getTagProductListReq struct { - TagInfo gmodel.FsTags +// 获取对应tag的产品列表 +type getTagProductsReq struct { + TagId int64 ProductList []gmodel.FsProduct MapProductMinPrice map[int64]int64 MapProductTemplate map[int64]struct{} @@ -196,13 +209,12 @@ type getTagProductListReq struct { User gmodel.FsUser } -// 获取对应tag的产品列表 -func (l *GetTagProductListLogic) getTagProductList(req getTagProductListReq) (productListRsp []types.TagProduct) { +func (l *GetTagProductListLogic) getTagProducts(req getTagProductsReq) (productListRsp []types.TagProduct) { //默认给50个容量 productListRsp = make([]types.TagProduct, 0, 50) for _, productInfo := range req.ProductList { //不属于一个类型则跳过 - if *productInfo.Type != req.TagInfo.Id { + if *productInfo.Type != req.TagId { continue } minPrice, ok := req.MapProductMinPrice[productInfo.Id] diff --git a/server/product/internal/types/types.go b/server/product/internal/types/types.go index 65880de8..573f6a5d 100644 --- a/server/product/internal/types/types.go +++ b/server/product/internal/types/types.go @@ -254,13 +254,15 @@ type GetTagProductListRsp struct { } type TagItem struct { - TypeName string `json:"typeName"` + TypeName string `json:"type_name"` TypeId int64 `json:"type_id"` Description string `json:"description"` Level int64 `json:"level"` - BelongPrefix string `json:"belong_prefix"` + LevelPrefix string `json:"level_prefix"` + Icon string `json:"icon"` + Sort int64 `json:"sort"` TagProductList []TagProduct `json:"tag_product_list"` - ChildTagList []TagItem `json:"child_tag_list"` + ChildTagList []*TagItem `json:"child_tag_list"` } type TagProduct struct { diff --git a/server_api/product.api b/server_api/product.api index ae8ab5a2..fa469e46 100644 --- a/server_api/product.api +++ b/server_api/product.api @@ -269,13 +269,15 @@ type GetTagProductListRsp { TagList []TagItem `json:"tag_list"` } type TagItem { - TypeName string `json:"typeName"` + TypeName string `json:"type_name"` TypeId int64 `json:"type_id"` Description string `json:"description"` Level int64 `json:"level"` - BelongPrefix string `json:"belong_prefix"` + LevelPrefix string `json:"level_prefix"` + Icon string `json:"icon"` + Sort int64 `json:"sort"` TagProductList []TagProduct `json:"tag_product_list"` - ChildTagList []TagItem `json:"child_tag_list"` + ChildTagList []*TagItem `json:"child_tag_list"` } type TagProduct { ProductId int64 `json:"product_id"` From 56a8b32d90ac4ab5718c0fa86407ea80162098fe Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Fri, 14 Jul 2023 14:07:23 +0800 Subject: [PATCH 06/11] fix --- .../internal/logic/gettagproductlistlogic.go | 13 ++++++++----- server/product/internal/types/types.go | 3 ++- server_api/product.api | 3 ++- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/server/product/internal/logic/gettagproductlistlogic.go b/server/product/internal/logic/gettagproductlistlogic.go index 9844130f..e39bfb7f 100644 --- a/server/product/internal/logic/gettagproductlistlogic.go +++ b/server/product/internal/logic/gettagproductlistlogic.go @@ -184,18 +184,21 @@ func (l *GetTagProductListLogic) GetTagProductList(req *types.GetTagProductListR } } //最终值提取最高级别那一层出来 - menus := make([]types.TagItem, 0, len(mapTagLevel)) + tagListRsp := make([]types.TagItem, 0, len(mapTagLevel)) for _, v := range mapTagLevel { if v.Level != minLevel { continue } - menus = append(menus, *v) + tagListRsp = append(tagListRsp, *v) } //排序 - sort.Slice(menus, func(i, j int) bool { - return menus[i].Sort < menus[j].Sort + sort.Slice(tagListRsp, func(i, j int) bool { + return tagListRsp[i].Sort < tagListRsp[j].Sort + }) + return resp.SetStatusWithMessage(basic.CodeOK, "success", types.GetTagProductListRsp{ + TotalCategory: len(mapTagLevel), + TagList: tagListRsp, }) - return resp.SetStatusWithMessage(basic.CodeOK, "success", menus) } // 获取对应tag的产品列表 diff --git a/server/product/internal/types/types.go b/server/product/internal/types/types.go index 573f6a5d..f97f220d 100644 --- a/server/product/internal/types/types.go +++ b/server/product/internal/types/types.go @@ -250,7 +250,8 @@ type GetTagProductListReq struct { } type GetTagProductListRsp struct { - TagList []TagItem `json:"tag_list"` + TotalCategory int `json:"totalCategory"` + TagList []TagItem `json:"tag_list"` } type TagItem struct { diff --git a/server_api/product.api b/server_api/product.api index fa469e46..2c1ddf71 100644 --- a/server_api/product.api +++ b/server_api/product.api @@ -266,7 +266,8 @@ type GetTagProductListReq { Size uint32 `form:"size,optional"` //尺寸 } type GetTagProductListRsp { - TagList []TagItem `json:"tag_list"` + TotalCategory int `json:"totalCategory"` + TagList []TagItem `json:"tag_list"` } type TagItem { TypeName string `json:"type_name"` From 86a2deacfbcc83199bf7259adafbfcdde1edbc35 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Fri, 14 Jul 2023 14:38:34 +0800 Subject: [PATCH 07/11] fix --- model/gmodel/fs_tags_logic.go | 16 ++++- .../handler/gettagproductlisthandler.go | 62 +++++++++---------- .../internal/logic/gettagproductlistlogic.go | 48 +++++++------- 3 files changed, 71 insertions(+), 55 deletions(-) diff --git a/model/gmodel/fs_tags_logic.go b/model/gmodel/fs_tags_logic.go index c45bcc05..75028f93 100755 --- a/model/gmodel/fs_tags_logic.go +++ b/model/gmodel/fs_tags_logic.go @@ -31,9 +31,11 @@ func (t *FsTagsModel) GetAllByLevel(ctx context.Context, level int) (resp []FsTa } type GetAllTagByParamsReq struct { - Ids []int64 - Status *int64 - OrderBy string + Ids []int64 + Status *int64 + OrderBy string + LevelPrefixLeftLike string //右模糊 + WithChild bool //是否包含子层级 } func (t *FsTagsModel) GetAllTagByParams(ctx context.Context, req GetAllTagByParamsReq) (resp []FsTags, err error) { @@ -44,6 +46,14 @@ func (t *FsTagsModel) GetAllTagByParams(ctx context.Context, req GetAllTagByPara if req.Status != nil { db = db.Where("`status` = ?", *req.Status) } + if req.LevelPrefixLeftLike != "" { + //查询子集 + if req.WithChild { + db = db.Where("`level_prefix` like ?", req.LevelPrefixLeftLike+`%`) + } else { + db = db.Where("`level_prefix` = ?", req.LevelPrefixLeftLike+`%`) + } + } switch req.OrderBy { case "": db = db.Order("`id` DESC") diff --git a/server/product/internal/handler/gettagproductlisthandler.go b/server/product/internal/handler/gettagproductlisthandler.go index 1952ac42..18a2a96c 100644 --- a/server/product/internal/handler/gettagproductlisthandler.go +++ b/server/product/internal/handler/gettagproductlisthandler.go @@ -17,41 +17,41 @@ import ( func GetTagProductListHandler(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响应并记录错误消息 + + 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, // 返回401状态码,表示未授权 - Message: "unauthorized", // 返回未授权信息 + Code: 401, + Message: "unauthorized", }) - logx.Info("unauthorized:", err.Error()) // 记录错误日志 + 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} - }*/ + } else { + // 如果claims为nil,则认为用户身份为白板用户 + userinfo = &auth.UserInfo{UserId: 0, GuestId: 0} + } var req types.GetTagProductListReq // 如果端点有请求结构体,则使用httpx.Parse方法从HTTP请求体中解析请求数据 @@ -65,7 +65,7 @@ func GetTagProductListHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { } // 创建一个业务逻辑层实例 l := logic.NewGetTagProductListLogic(r.Context(), svcCtx) - resp := l.GetTagProductList(&req, &auth.UserInfo{39, 0}) + resp := l.GetTagProductList(&req, userinfo) // 如果响应不为nil,则使用httpx.OkJsonCtx方法返回JSON响应; if resp != nil { httpx.OkJsonCtx(r.Context(), w, resp) diff --git a/server/product/internal/logic/gettagproductlistlogic.go b/server/product/internal/logic/gettagproductlistlogic.go index e39bfb7f..cf4a3702 100644 --- a/server/product/internal/logic/gettagproductlistlogic.go +++ b/server/product/internal/logic/gettagproductlistlogic.go @@ -1,11 +1,13 @@ package logic import ( + "errors" "fusenapi/model/gmodel" "fusenapi/utils/auth" "fusenapi/utils/basic" "fusenapi/utils/format" "fusenapi/utils/image" + "gorm.io/gorm" "sort" "strings" @@ -37,20 +39,28 @@ func (l *GetTagProductListLogic) GetTagProductList(req *types.GetTagProductListR req.Size = image.GetCurrentSize(req.Size) } //查询用户信息(不用判断存在) - /*user, err := l.svcCtx.AllModels.FsUser.FindUserById(l.ctx, userinfo.UserId) + user, err := l.svcCtx.AllModels.FsUser.FindUserById(l.ctx, userinfo.UserId) if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) { logx.Error(err) return resp.SetStatusWithMessage(basic.CodeServiceErr, "get user info err") - }*/ + } //查询分类列表 tStatus := int64(1) tReq := gmodel.GetAllTagByParamsReq{ - Status: &tStatus, - OrderBy: "`sort` DESC", + Status: &tStatus, + OrderBy: "`sort` DESC", + WithChild: true, //需要子集 } //传入分类id if req.Cid > 0 { - tReq.Ids = []int64{req.Cid} + //获取该类行的levelprefix + tagData, err := l.svcCtx.AllModels.FsTags.FindOne(l.ctx, req.Cid) + if err != nil { + if errors.Is(err, gorm.ErrRecordNotFound) { + return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "param cid is invalid:record not found") + } + } + tReq.LevelPrefixLeftLike = *tagData.LevelPrefix } tagList, err := l.svcCtx.AllModels.FsTags.GetAllTagByParams(l.ctx, tReq) if err != nil { @@ -81,12 +91,8 @@ func (l *GetTagProductListLogic) GetTagProductList(req *types.GetTagProductListR logx.Error(err) return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get product list") } - productLen := len(productList) - if productLen == 0 { - return resp.SetStatusWithMessage(basic.CodeOK, "success") - } //提取产品ids - productIds := make([]int64, 0, productLen) + productIds := make([]int64, 0, len(productList)) for _, v := range productList { productIds = append(productIds, v.Id) } @@ -140,7 +146,7 @@ func (l *GetTagProductListLogic) GetTagProductList(req *types.GetTagProductListR minLevel = *tagInfo.Level } //获取分类产品列表 - /*productListRsp := l.getTagProducts(getTagProductsReq{ + productListRsp := l.getTagProducts(getTagProductsReq{ TagId: tagInfo.Id, ProductList: productList, MapProductMinPrice: mapProductMinPrice, @@ -148,18 +154,18 @@ func (l *GetTagProductListLogic) GetTagProductList(req *types.GetTagProductListR MapProductSizeCount: mapProductSizeCount, Size: req.Size, User: user, - })*/ + }) //加入分类 tagTem := types.TagItem{ - //TagProductList: productListRsp, - TypeName: *tagInfo.Title, - TypeId: tagInfo.Id, - Level: *tagInfo.Level, - LevelPrefix: *tagInfo.LevelPrefix, - Icon: *tagInfo.Icon, - Sort: *tagInfo.Sort, - Description: *tagInfo.Description, - ChildTagList: make([]*types.TagItem, 0, 100), + TagProductList: productListRsp, + TypeName: *tagInfo.Title, + TypeId: tagInfo.Id, + Level: *tagInfo.Level, + LevelPrefix: *tagInfo.LevelPrefix, + Icon: *tagInfo.Icon, + Sort: *tagInfo.Sort, + Description: *tagInfo.Description, + ChildTagList: make([]*types.TagItem, 0, 100), } //当前tag保存入map mapTagLevel[*tagInfo.LevelPrefix] = &tagTem From d122f9b0c0dcd6f5ddf7b1b65888764dccb09f1b Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Fri, 14 Jul 2023 14:43:59 +0800 Subject: [PATCH 08/11] fix --- server/product/internal/types/types.go | 12 ++++++------ server_api/product.api | 12 ++++++------ 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/server/product/internal/types/types.go b/server/product/internal/types/types.go index f97f220d..df63cc23 100644 --- a/server/product/internal/types/types.go +++ b/server/product/internal/types/types.go @@ -250,7 +250,7 @@ type GetTagProductListReq struct { } type GetTagProductListRsp struct { - TotalCategory int `json:"totalCategory"` + TotalCategory int `json:"total_category"` TagList []TagItem `json:"tag_list"` } @@ -273,11 +273,11 @@ type TagProduct struct { Cover string `json:"cover"` Intro string `json:"intro"` CoverImg string `json:"cover_img"` - IsEnv int64 `json:"isEnv"` - IsMicro int64 `json:"isMicro"` - SizeNum uint32 `json:"sizeNum"` - MiniPrice int64 `json:"miniPrice"` - CoverDefault string `json:"coverDefault"` + IsEnv int64 `json:"is_env"` + IsMicro int64 `json:"is_micro"` + SizeNum uint32 `json:"size_num"` + MiniPrice int64 `json:"mini_price"` + CoverDefault string `json:"cover_default"` } type Request struct { diff --git a/server_api/product.api b/server_api/product.api index 2c1ddf71..b522dcb8 100644 --- a/server_api/product.api +++ b/server_api/product.api @@ -266,7 +266,7 @@ type GetTagProductListReq { Size uint32 `form:"size,optional"` //尺寸 } type GetTagProductListRsp { - TotalCategory int `json:"totalCategory"` + TotalCategory int `json:"total_category"` TagList []TagItem `json:"tag_list"` } type TagItem { @@ -287,9 +287,9 @@ type TagProduct { Cover string `json:"cover"` Intro string `json:"intro"` CoverImg string `json:"cover_img"` - IsEnv int64 `json:"isEnv"` - IsMicro int64 `json:"isMicro"` - SizeNum uint32 `json:"sizeNum"` - MiniPrice int64 `json:"miniPrice"` - CoverDefault string `json:"coverDefault"` + IsEnv int64 `json:"is_env"` + IsMicro int64 `json:"is_micro"` + SizeNum uint32 `json:"size_num"` + MiniPrice int64 `json:"mini_price"` + CoverDefault string `json:"cover_default"` } \ No newline at end of file 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 09/11] 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 From 0db42491aecf6fac05ab925fe7885da1b57d6cc2 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Fri, 14 Jul 2023 16:48:04 +0800 Subject: [PATCH 10/11] fix --- model/gmodel/fs_product_logic.go | 8 +- model/gmodel/fs_product_model3d_logic.go | 8 +- model/gmodel/fs_product_size_logic.go | 15 ++-- .../internal/handler/getmodelbypidhandler.go | 78 +++++++++++++++++ server/product/internal/handler/routes.go | 5 ++ .../internal/logic/getmodelbypidlogic.go | 85 +++++++++++++++++++ .../internal/logic/getsizebyproductlogic.go | 2 +- server/product/internal/types/types.go | 4 + server_api/product.api | 8 ++ 9 files changed, 202 insertions(+), 11 deletions(-) create mode 100644 server/product/internal/handler/getmodelbypidhandler.go create mode 100644 server/product/internal/logic/getmodelbypidlogic.go diff --git a/model/gmodel/fs_product_logic.go b/model/gmodel/fs_product_logic.go index 6328daa4..16081b58 100755 --- a/model/gmodel/fs_product_logic.go +++ b/model/gmodel/fs_product_logic.go @@ -6,8 +6,12 @@ func (p *FsProductModel) FindOne(ctx context.Context, id int64) (resp *FsProduct err = p.db.WithContext(ctx).Model(&FsProduct{}).Where("`id` = ? ", id).First(&resp).Error return resp, err } -func (p *FsProductModel) FindOneBySn(ctx context.Context, sn string) (resp *FsProduct, err error) { - err = p.db.WithContext(ctx).Model(&FsProduct{}).Where("`sn` = ? ", sn).Take(&resp).Error +func (p *FsProductModel) FindOneBySn(ctx context.Context, sn string, fields ...string) (resp *FsProduct, err error) { + db := p.db.WithContext(ctx).Model(&FsProduct{}).Where("`sn` = ? ", sn) + if len(fields) != 0 { + db = db.Select(fields[0]) + } + err = db.Take(&resp).Error return resp, err } func (p *FsProductModel) GetProductListByIds(ctx context.Context, productIds []int64, sort string) (resp []FsProduct, err error) { diff --git a/model/gmodel/fs_product_model3d_logic.go b/model/gmodel/fs_product_model3d_logic.go index 4b8ab2b1..fde41568 100755 --- a/model/gmodel/fs_product_model3d_logic.go +++ b/model/gmodel/fs_product_model3d_logic.go @@ -60,11 +60,15 @@ func (d *FsProductModel3dModel) Get3dModelsByParam(ctx context.Context, req Get3 func (d *FsProductModel3dModel) Update(ctx context.Context, id int64, data *FsProductModel3d) error { return d.db.WithContext(ctx).Where("`id` = ? ", id).Updates(&data).Error } -func (d *FsProductModel3dModel) GetAllBySizeIdsTag(ctx context.Context, sizeIds []int64, tag int64) (resp []FsProductModel3d, err error) { +func (d *FsProductModel3dModel) GetAllBySizeIdsTag(ctx context.Context, sizeIds []int64, tag int64, fields ...string) (resp []FsProductModel3d, err error) { if len(sizeIds) == 0 { return } - err = d.db.WithContext(ctx).Model(&FsProductModel3d{}).Where("`size_id` in (?) and `tag` = ?", sizeIds, tag).Find(&resp).Error + db := d.db.WithContext(ctx).Model(&FsProductModel3d{}).Where("`size_id` in (?) and `tag` = ?", sizeIds, tag) + if len(fields) != 0 { + db = db.Select(fields[0]) + } + err = db.Find(&resp).Error return resp, err } func (d *FsProductModel3dModel) GetAll(ctx context.Context) (resp []FsProductModel3d, err error) { diff --git a/model/gmodel/fs_product_size_logic.go b/model/gmodel/fs_product_size_logic.go index 38d108a7..351fa22e 100755 --- a/model/gmodel/fs_product_size_logic.go +++ b/model/gmodel/fs_product_size_logic.go @@ -38,16 +38,19 @@ func (s *FsProductSizeModel) GetGroupProductSizeByStatus(ctx context.Context, pr Find(&resp).Error return resp, err } -func (s *FsProductSizeModel) GetAllByProductIds(ctx context.Context, productIds []int64, sort string) (resp []FsProductSize, err error) { +func (s *FsProductSizeModel) GetAllByProductIds(ctx context.Context, productIds []int64, orderBy string, fields ...string) (resp []FsProductSize, err error) { if len(productIds) == 0 { return nil, nil } db := s.db.WithContext(ctx).Model(&FsProductSize{}).Where("`product_id` in(?) and `status` = ?", productIds, 1) - switch sort { - case "sort-asc": - db = db.Order("`sort` ASC") - case "sort-desc": - db = db.Order("`sort` DESC") + if len(fields) != 0 { + db = db.Select(fields[0]) + } + switch orderBy { + case "": + db = db.Order("`id` ASC") + default: + db = db.Order(orderBy) } err = db.Find(&resp).Error if err != nil { diff --git a/server/product/internal/handler/getmodelbypidhandler.go b/server/product/internal/handler/getmodelbypidhandler.go new file mode 100644 index 00000000..f944508e --- /dev/null +++ b/server/product/internal/handler/getmodelbypidhandler.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 GetModelByPidHandler(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.GetModelByPidReq + // 如果端点有请求结构体,则使用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.NewGetModelByPidLogic(r.Context(), svcCtx) + resp := l.GetModelByPid(&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 a65b0205..43a10ab4 100644 --- a/server/product/internal/handler/routes.go +++ b/server/product/internal/handler/routes.go @@ -67,6 +67,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { Path: "/api/product/render_design", Handler: GetRenderDesignHandler(serverCtx), }, + { + Method: http.MethodGet, + Path: "/api/product/get_model_by_pid", + Handler: GetModelByPidHandler(serverCtx), + }, }, ) } diff --git a/server/product/internal/logic/getmodelbypidlogic.go b/server/product/internal/logic/getmodelbypidlogic.go new file mode 100644 index 00000000..51567f54 --- /dev/null +++ b/server/product/internal/logic/getmodelbypidlogic.go @@ -0,0 +1,85 @@ +package logic + +import ( + "encoding/json" + "errors" + "fusenapi/constants" + "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 GetModelByPidLogic struct { + logx.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewGetModelByPidLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetModelByPidLogic { + return &GetModelByPidLogic{ + Logger: logx.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +func (l *GetModelByPidLogic) GetModelByPid(req *types.GetModelByPidReq, userinfo *auth.UserInfo) (resp *basic.Response) { + req.Pid = strings.Trim(req.Pid, " ") + if req.Pid == "" { + return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "err param:pid is empty") + } + //获取产品信息(只是获取id) + productInfo, err := l.svcCtx.AllModels.FsProduct.FindOneBySn(l.ctx, req.Pid, "id") + if err != nil { + if errors.Is(err, gorm.ErrRecordNotFound) { + return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "the product is not exists") + } + logx.Error(err) + return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get product info") + } + //获取产品尺寸列表(只是获取id) + sizeList, err := l.svcCtx.AllModels.FsProductSize.GetAllByProductIds(l.ctx, []int64{productInfo.Id}, "id") + if err != nil { + logx.Error(err) + return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get size list") + } + if len(sizeList) == 0 { + return resp.SetStatusWithMessage(basic.CodeOK, "success:size list is empty", []interface{}{}) + } + sizeIds := make([]int64, 0, len(sizeList)) + for _, v := range sizeList { + sizeIds = append(sizeIds, v.Id) + } + //获取模型数据(只是获取model_info) + model3dList, err := l.svcCtx.AllModels.FsProductModel3d.GetAllBySizeIdsTag(l.ctx, sizeIds, constants.TAG_MODEL, "id,model_info") + if err != nil { + logx.Error(err) + return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get model list") + } + if len(model3dList) == 0 { + return resp.SetStatusWithMessage(basic.CodeOK, "success:model list is empty", []interface{}{}) + } + //处理数据 + mapModel := make(map[int64]interface{}) + for _, v := range model3dList { + if v.ModelInfo == nil || *v.ModelInfo == "" { + mapModel[v.Id] = nil + continue + } + var info interface{} + if err = json.Unmarshal([]byte(*v.ModelInfo), &info); err != nil { + logx.Error(err) + return resp.SetStatusWithMessage(basic.CodeJsonErr, "failed to parse model info json") + } + mapModel[v.Id] = info + } + return resp.SetStatusWithMessage(basic.CodeOK, "success", mapModel) +} diff --git a/server/product/internal/logic/getsizebyproductlogic.go b/server/product/internal/logic/getsizebyproductlogic.go index 644c7f94..3d111822 100644 --- a/server/product/internal/logic/getsizebyproductlogic.go +++ b/server/product/internal/logic/getsizebyproductlogic.go @@ -63,7 +63,7 @@ func (l *GetSizeByProductLogic) GetSizeByProduct(userinfo *auth.UserInfo) (resp productIds = append(productIds, v.Id) } productSizeModel := gmodel.NewFsProductSizeModel(l.svcCtx.MysqlConn) - productSizeList, err := productSizeModel.GetAllByProductIds(l.ctx, productIds, "sort-desc") + productSizeList, err := productSizeModel.GetAllByProductIds(l.ctx, productIds, "sort DESC") if err != nil { logx.Error(err) return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get product size list") diff --git a/server/product/internal/types/types.go b/server/product/internal/types/types.go index c8abf939..cd953a79 100644 --- a/server/product/internal/types/types.go +++ b/server/product/internal/types/types.go @@ -293,6 +293,10 @@ type GetRenderDesignRsp struct { LogoColor []string `json:"logo_color"` } +type GetModelByPidReq struct { + Pid string `form:"pid"` //实际上是产品sn +} + type Request struct { } diff --git a/server_api/product.api b/server_api/product.api index 9fbf6e05..80a5c41e 100644 --- a/server_api/product.api +++ b/server_api/product.api @@ -44,6 +44,9 @@ service product { //获取云渲染设计方案信息 @handler GetRenderDesignHandler get /api/product/render_design(GetRenderDesignReq) returns (response); + //获取产品模型信息 + @handler GetModelByPidHandler + get /api/product/get_model_by_pid(GetModelByPidReq) returns (response); //*********************产品详情分解接口结束*********************** } @@ -309,4 +312,9 @@ type GetRenderDesignRsp { SizeId int64 `json:"size_id"` TemplateId int64 `json:"template_id"` LogoColor []string `json:"logo_color"` +} + +//获取产品模型信息 +type GetModelByPidReq { + Pid string `form:"pid"` //实际上是产品sn } \ No newline at end of file From edd3d7353d2ad606562e2fd463d9d1ff3a4b6e82 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Fri, 14 Jul 2023 19:25:52 +0800 Subject: [PATCH 11/11] fix --- .../internal/handler/getpricebypidhandler.go | 78 +++++++++++++++++++ server/product/internal/handler/routes.go | 5 ++ .../internal/logic/getpricebypidlogic.go | 78 +++++++++++++++++++ server/product/internal/types/types.go | 4 + server_api/product.api | 7 ++ 5 files changed, 172 insertions(+) create mode 100644 server/product/internal/handler/getpricebypidhandler.go create mode 100644 server/product/internal/logic/getpricebypidlogic.go diff --git a/server/product/internal/handler/getpricebypidhandler.go b/server/product/internal/handler/getpricebypidhandler.go new file mode 100644 index 00000000..b4e55c9a --- /dev/null +++ b/server/product/internal/handler/getpricebypidhandler.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 GetPriceByPidHandler(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.GetPriceByPidReq + // 如果端点有请求结构体,则使用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.NewGetPriceByPidLogic(r.Context(), svcCtx) + resp := l.GetPriceByPid(&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 43a10ab4..74b64ce0 100644 --- a/server/product/internal/handler/routes.go +++ b/server/product/internal/handler/routes.go @@ -72,6 +72,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { Path: "/api/product/get_model_by_pid", Handler: GetModelByPidHandler(serverCtx), }, + { + Method: http.MethodGet, + Path: "/api/product/get_price_by_pid", + Handler: GetPriceByPidHandler(serverCtx), + }, }, ) } diff --git a/server/product/internal/logic/getpricebypidlogic.go b/server/product/internal/logic/getpricebypidlogic.go new file mode 100644 index 00000000..6af193da --- /dev/null +++ b/server/product/internal/logic/getpricebypidlogic.go @@ -0,0 +1,78 @@ +package logic + +import ( + "errors" + "fmt" + "fusenapi/utils/auth" + "fusenapi/utils/basic" + "fusenapi/utils/format" + "gorm.io/gorm" + "strings" + + "context" + + "fusenapi/server/product/internal/svc" + "fusenapi/server/product/internal/types" + + "github.com/zeromicro/go-zero/core/logx" +) + +type GetPriceByPidLogic struct { + logx.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewGetPriceByPidLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetPriceByPidLogic { + return &GetPriceByPidLogic{ + Logger: logx.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +func (l *GetPriceByPidLogic) GetPriceByPid(req *types.GetPriceByPidReq, userinfo *auth.UserInfo) (resp *basic.Response) { + req.Pid = strings.Trim(req.Pid, " ") + if req.Pid == "" { + return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "err param:pid is empty") + } + //获取产品信息(只是获取id) + productInfo, err := l.svcCtx.AllModels.FsProduct.FindOneBySn(l.ctx, req.Pid, "id") + if err != nil { + if errors.Is(err, gorm.ErrRecordNotFound) { + return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "the product is not exists") + } + logx.Error(err) + return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get product info") + } + //查询产品价格 + priceList, err := l.svcCtx.AllModels.FsProductPrice.GetPriceListByProductIds(l.ctx, []int64{productInfo.Id}) + if err != nil { + logx.Error(err) + return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get price list") + } + //处理价格信息 + mapRsp := make(map[string]interface{}) + for _, v := range priceList { + mapKey := fmt.Sprintf("_%d", v.Id) + stepNum, err := format.StrSlicToIntSlice(strings.Split(*v.StepNum, ",")) + if err != nil { + logx.Error(err) + return resp.SetStatusWithMessage(basic.CodeServiceErr, fmt.Sprintf("failed to parse step num,price_id=%d", v.Id)) + } + /*$price['step_num'] = explode(',', $price['step_num']); + $price['step_price'] = explode(',', $price['step_price']); + while ($price['min_buy_num'] < end($price['step_num']) + 5) { + $outData["{$price['size_id']}"]['items'][] = [ + 'num' => intval($price['min_buy_num']), + 'total_num' => $price['min_buy_num'] * $price['each_box_num'], + 'price' => ProductPriceService::getPrice($price['min_buy_num'], $price['step_num'], $price['step_price']) + ]; + $price['min_buy_num'] += 1; + } + + $outData["{$price['size_id']}"]['min_price'] = floatval(end($price['step_price']) / 100); + $outData["{$price['size_id']}"]['max_price'] = floatval(reset($price['step_price']) / 100);*/ + } + return resp.SetStatus(basic.CodeOK) +} diff --git a/server/product/internal/types/types.go b/server/product/internal/types/types.go index cd953a79..5b3bf41b 100644 --- a/server/product/internal/types/types.go +++ b/server/product/internal/types/types.go @@ -297,6 +297,10 @@ type GetModelByPidReq struct { Pid string `form:"pid"` //实际上是产品sn } +type GetPriceByPidReq struct { + Pid string `form:"pid"` +} + type Request struct { } diff --git a/server_api/product.api b/server_api/product.api index 80a5c41e..93653147 100644 --- a/server_api/product.api +++ b/server_api/product.api @@ -47,6 +47,9 @@ service product { //获取产品模型信息 @handler GetModelByPidHandler get /api/product/get_model_by_pid(GetModelByPidReq) returns (response); + //获取产品阶梯价格列表 + @handler GetPriceByPidHandler + get /api/product/get_price_by_pid(GetPriceByPidReq) returns (response); //*********************产品详情分解接口结束*********************** } @@ -317,4 +320,8 @@ type GetRenderDesignRsp { //获取产品模型信息 type GetModelByPidReq { Pid string `form:"pid"` //实际上是产品sn +} +//获取产品阶梯价格 +type GetPriceByPidReq { + Pid string `form:"pid"` } \ No newline at end of file