增加产品推荐接口

This commit is contained in:
laodaming
2023-06-06 18:17:03 +08:00
parent 4312d978fb
commit cf852c5a87
3 changed files with 44 additions and 38 deletions

View File

@@ -73,7 +73,7 @@ func (l *GetProductListLogic) GetProductList(req *types.GetProductListReq, login
}
//查询符合的产品列表
productModel := model.NewFsProductModel(l.svcCtx.MysqlConn)
productList, err := productModel.GetProductListByConditions(l.ctx, int(req.Cid), 0, 1, "sort-desc")
productList, err := productModel.GetProductListByConditions(l.ctx, int(req.Cid), "sort-desc")
if err != nil {
logx.Error(err)
resp.Set(constants.CODE_SERVICE_ERR, "failed to get product list")

View File

@@ -10,10 +10,10 @@ import (
"fusenapi/product/internal/types"
"fusenapi/utils/auth"
"fusenapi/utils/image"
"github.com/zeromicro/go-zero/core/stores/sqlx"
"math/rand"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/core/stores/sqlx"
"strings"
"time"
)
type GetSuccessRecommandLogic struct {
@@ -30,8 +30,10 @@ func NewGetSuccessRecommandLogic(ctx context.Context, svcCtx *svc.ServiceContext
}
}
// 获取推荐的产品列表
func (l *GetSuccessRecommandLogic) GetSuccessRecommand(req *types.GetSuccessRecommandReq, loginInfo auth.UserInfo) (resp *types.Response) {
resp = &types.Response{}
loginInfo.UserId = 83
//校验前台登录情况
if loginInfo.UserId == 0 {
resp.Set(constants.CODE_UNAUTH, "please sign in")
@@ -49,15 +51,15 @@ func (l *GetSuccessRecommandLogic) GetSuccessRecommand(req *types.GetSuccessReco
resp.Set(constants.CODE_UNAUTH, "failed to get user info")
return
}
if req.Num == 0 {
if req.Num == 0 || req.Num > 500 {
req.Num = 8
}
if req.Size > 0 {
req.Size = image.GetCurrentSize(req.Size)
}
//获取所有产品的ids
//随机取8个产品
productModel := model.NewFsProductModel(l.svcCtx.MysqlConn)
productList, err := productModel.GetAllProductList(l.ctx, 0, 1, "sort-asc")
productList, err := productModel.GetRandomProductList(l.ctx, int(req.Num))
if err != nil {
logx.Error(err)
resp.Set(constants.CODE_SERVICE_ERR, "failed to get product list")
@@ -68,23 +70,35 @@ func (l *GetSuccessRecommandLogic) GetSuccessRecommand(req *types.GetSuccessReco
resp.Set(constants.CODE_OK, "success")
return
}
productIds := make([]string, 0, len(productList))
list := make([]types.GetSuccessRecommandRsp, 0, len(productList))
for _, v := range productList {
productIds = append(productIds, fmt.Sprintf("%d", v.Id))
}
//随机取8个
if len(productIds) > int(req.Num) {
//打乱顺序
indexArr := rand.Perm(len(productIds))
tmpProductIds := make([]string, 0, int(req.Num))
for k, v := range indexArr {
if k == 8 {
break
}
tmpProductIds = append(tmpProductIds, productIds[v])
data := types.GetSuccessRecommandRsp{
Title: v.Title,
Cover: v.Cover,
CoverImg: v.CoverImg,
Sn: v.Sn,
Id: v.Id,
SkuId: 0, //???????
CoverDefault: v.CoverImg,
}
productIds = tmpProductIds
if req.Size > 0 {
coverImgSlice := strings.Split(v.CoverImg, ".")
coverSlice := strings.Split(v.Cover, ".")
if req.Size > 200 {
data.Cover = fmt.Sprintf("%s_%d.%s", coverSlice[0], req.Size, coverSlice[1])
data.CoverImg = fmt.Sprintf("%s_%d.%s", coverImgSlice[0], req.Size, coverImgSlice[1])
}
//千人千面
if userInfo.IsThousandFace == 1 {
data.Cover = ""
data.CoverImg = fmt.Sprintf("%s/test/%d/%d_%d.png?%d", constants.DOMAIN_RENDER_IMG_NAME, userInfo.Id, userInfo.Id, v.Id, time.Now().Unix())
if req.Size > 200 {
data.CoverDefault = fmt.Sprintf("%s_%d.%s", coverImgSlice[0], req.Size, coverImgSlice[1])
}
}
}
list = append(list, data)
}
return resp
resp.SetWithData(constants.CODE_OK, "success", list)
return
}