This commit is contained in:
laodaming 2023-07-14 12:25:02 +08:00
parent 52f65afe1a
commit 6d3a3b2dda
4 changed files with 82 additions and 66 deletions

View File

@ -17,41 +17,41 @@ import (
func GetTagProductListHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { func GetTagProductListHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
/*
var ( var (
// 定义错误变量 // 定义错误变量
err error err error
// 定义用户信息变量 // 定义用户信息变量
userinfo *auth.UserInfo userinfo *auth.UserInfo
) )
// 解析JWT token,并对空用户进行判断 // 解析JWT token,并对空用户进行判断
claims, err := svcCtx.ParseJwtToken(r) claims, err := svcCtx.ParseJwtToken(r)
// 如果解析JWT token出错,则返回未授权的JSON响应并记录错误消息 // 如果解析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 { if err != nil {
httpx.OkJsonCtx(r.Context(), w, &basic.Response{ httpx.OkJsonCtx(r.Context(), w, &basic.Response{
Code: 401, Code: 401, // 返回401状态码,表示未授权
Message: "unauthorized", Message: "unauthorized", // 返回未授权信息
}) })
logx.Info("unauthorized:", err.Error()) logx.Info("unauthorized:", err.Error()) // 记录错误日志
return return
} }
} else {
// 如果claims为nil,则认为用户身份为白板用户 if claims != nil {
userinfo = &auth.UserInfo{UserId: 0, GuestId: 0} // 从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 var req types.GetTagProductListReq
// 如果端点有请求结构体则使用httpx.Parse方法从HTTP请求体中解析请求数据 // 如果端点有请求结构体则使用httpx.Parse方法从HTTP请求体中解析请求数据
@ -65,7 +65,7 @@ func GetTagProductListHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
} }
// 创建一个业务逻辑层实例 // 创建一个业务逻辑层实例
l := logic.NewGetTagProductListLogic(r.Context(), svcCtx) l := logic.NewGetTagProductListLogic(r.Context(), svcCtx)
resp := l.GetTagProductList(&req, userinfo) resp := l.GetTagProductList(&req, &auth.UserInfo{39, 0})
// 如果响应不为nil则使用httpx.OkJsonCtx方法返回JSON响应; // 如果响应不为nil则使用httpx.OkJsonCtx方法返回JSON响应;
if resp != nil { if resp != nil {
httpx.OkJsonCtx(r.Context(), w, resp) httpx.OkJsonCtx(r.Context(), w, resp)

View File

@ -1,13 +1,11 @@
package logic package logic
import ( import (
"errors"
"fusenapi/model/gmodel" "fusenapi/model/gmodel"
"fusenapi/utils/auth" "fusenapi/utils/auth"
"fusenapi/utils/basic" "fusenapi/utils/basic"
"fusenapi/utils/format" "fusenapi/utils/format"
"fusenapi/utils/image" "fusenapi/utils/image"
"gorm.io/gorm"
"sort" "sort"
"strings" "strings"
@ -39,11 +37,11 @@ func (l *GetTagProductListLogic) GetTagProductList(req *types.GetTagProductListR
req.Size = image.GetCurrentSize(req.Size) 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) { if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
logx.Error(err) logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "get user info err") return resp.SetStatusWithMessage(basic.CodeServiceErr, "get user info err")
} }*/
//查询分类列表 //查询分类列表
tStatus := int64(1) tStatus := int64(1)
tReq := gmodel.GetAllTagByParamsReq{ tReq := gmodel.GetAllTagByParamsReq{
@ -132,62 +130,77 @@ func (l *GetTagProductListLogic) GetTagProductList(req *types.GetTagProductListR
for _, v := range productSizeCountList { for _, v := range productSizeCountList {
mapProductSizeCount[v.ProductId] = v.Num mapProductSizeCount[v.ProductId] = v.Num
} }
mapTagLevel := make(map[string]types.TagItem) mapTagLevel := make(map[string]*types.TagItem)
minLevel := int64(0) //记录最小等级数字 minLevel := int64(0) //记录最小等级数字
for _, tagInfo := range tagList { 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 minLevel = *tagInfo.Level
} }
//获取分类产品列表 //获取分类产品列表
productListRsp := l.getTagProductList(getTagProductListReq{ /*productListRsp := l.getTagProducts(getTagProductsReq{
TagInfo: tagInfo, TagId: tagInfo.Id,
ProductList: productList, ProductList: productList,
MapProductMinPrice: mapProductMinPrice, MapProductMinPrice: mapProductMinPrice,
MapProductTemplate: mapProductTemplate, MapProductTemplate: mapProductTemplate,
MapProductSizeCount: mapProductSizeCount, MapProductSizeCount: mapProductSizeCount,
Size: req.Size, Size: req.Size,
User: user, User: user,
}) })*/
//加入分类 //加入分类
tagTem := types.TagItem{ tagTem := types.TagItem{
TagProductList: productListRsp, //TagProductList: productListRsp,
TypeName: *tagInfo.Title, TypeName: *tagInfo.Title,
TypeId: tagInfo.Id, TypeId: tagInfo.Id,
Level: *tagInfo.Level, Level: *tagInfo.Level,
BelongPrefix: *tagInfo.LevelPrefix, LevelPrefix: *tagInfo.LevelPrefix,
Description: *tagInfo.Description, Icon: *tagInfo.Icon,
ChildTagList: []types.TagItem{}, Sort: *tagInfo.Sort,
Description: *tagInfo.Description,
ChildTagList: make([]*types.TagItem, 0, 100),
} }
//当前tag保存入map //当前tag保存入map
mapTagLevel[*tagInfo.LevelPrefix] = tagTem mapTagLevel[*tagInfo.LevelPrefix] = &tagTem
} }
//组装等级从属关系 //组装等级从属关系
for prefix, tagItem := range mapTagLevel { for prefix, tagItem := range mapTagLevel {
//查看有没有直接父级有的话则把当前的append到父级的ChildTagList中 prefix = strings.Trim(prefix, " ")
prefixSlice := strings.Split(prefix, "/") //最上级没有父级
//等于1表示自己是最上级 if !strings.Contains(prefix, "/") {
if len(prefixSlice) == 1 {
continue continue
} }
prefixSlice := strings.Split(prefix, "/")
//有父级
parentPrefix := strings.Join(prefixSlice[:len(prefixSlice)-1], "/") parentPrefix := strings.Join(prefixSlice[:len(prefixSlice)-1], "/")
if parent, ok := mapTagLevel[parentPrefix]; ok { if parent, ok := mapTagLevel[parentPrefix]; ok {
parent.ChildTagList = append(parent.ChildTagList, tagItem) 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 mapTagLevel[parentPrefix] = parent
} }
} }
//最终值提取最高级别那一层出来 //最终值提取最高级别那一层出来
finalSlice := make([]interface{}, 0, len(mapTagLevel)) menus := make([]types.TagItem, 0, len(mapTagLevel))
for _, v := range mapTagLevel { for _, v := range mapTagLevel {
if v.Level != minLevel { if v.Level != minLevel {
continue 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 { // 获取对应tag的产品列表
TagInfo gmodel.FsTags type getTagProductsReq struct {
TagId int64
ProductList []gmodel.FsProduct ProductList []gmodel.FsProduct
MapProductMinPrice map[int64]int64 MapProductMinPrice map[int64]int64
MapProductTemplate map[int64]struct{} MapProductTemplate map[int64]struct{}
@ -196,13 +209,12 @@ type getTagProductListReq struct {
User gmodel.FsUser User gmodel.FsUser
} }
// 获取对应tag的产品列表 func (l *GetTagProductListLogic) getTagProducts(req getTagProductsReq) (productListRsp []types.TagProduct) {
func (l *GetTagProductListLogic) getTagProductList(req getTagProductListReq) (productListRsp []types.TagProduct) {
//默认给50个容量 //默认给50个容量
productListRsp = make([]types.TagProduct, 0, 50) productListRsp = make([]types.TagProduct, 0, 50)
for _, productInfo := range req.ProductList { for _, productInfo := range req.ProductList {
//不属于一个类型则跳过 //不属于一个类型则跳过
if *productInfo.Type != req.TagInfo.Id { if *productInfo.Type != req.TagId {
continue continue
} }
minPrice, ok := req.MapProductMinPrice[productInfo.Id] minPrice, ok := req.MapProductMinPrice[productInfo.Id]

View File

@ -254,13 +254,15 @@ type GetTagProductListRsp struct {
} }
type TagItem struct { type TagItem struct {
TypeName string `json:"typeName"` TypeName string `json:"type_name"`
TypeId int64 `json:"type_id"` TypeId int64 `json:"type_id"`
Description string `json:"description"` Description string `json:"description"`
Level int64 `json:"level"` 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"` TagProductList []TagProduct `json:"tag_product_list"`
ChildTagList []TagItem `json:"child_tag_list"` ChildTagList []*TagItem `json:"child_tag_list"`
} }
type TagProduct struct { type TagProduct struct {

View File

@ -269,13 +269,15 @@ type GetTagProductListRsp {
TagList []TagItem `json:"tag_list"` TagList []TagItem `json:"tag_list"`
} }
type TagItem { type TagItem {
TypeName string `json:"typeName"` TypeName string `json:"type_name"`
TypeId int64 `json:"type_id"` TypeId int64 `json:"type_id"`
Description string `json:"description"` Description string `json:"description"`
Level int64 `json:"level"` 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"` TagProductList []TagProduct `json:"tag_product_list"`
ChildTagList []TagItem `json:"child_tag_list"` ChildTagList []*TagItem `json:"child_tag_list"`
} }
type TagProduct { type TagProduct {
ProductId int64 `json:"product_id"` ProductId int64 `json:"product_id"`