fix
This commit is contained in:
parent
52f65afe1a
commit
6d3a3b2dda
|
@ -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)
|
||||
|
|
|
@ -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]
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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"`
|
||||
|
|
Loading…
Reference in New Issue
Block a user