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] 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