This commit is contained in:
laodaming 2023-07-10 19:42:55 +08:00
parent f9bfbf28ae
commit f07e4a2084
6 changed files with 181 additions and 4 deletions

View File

@ -64,13 +64,15 @@ func (p *FsProductModel) GetProductListByTypeIds(ctx context.Context, productTyp
func (p *FsProductModel) GetRandomProductList(ctx context.Context, limit int) (resp []FsProduct, err error) {
err = p.db.WithContext(ctx).Model(&FsProduct{}).
Where("`is_del` =? and `is_shelf` = ?", 0, 1).Order("RAND()").Limit(limit).Find(&resp).Error
if err != nil {
return nil, err
}
return
return resp,err
}
func (p *FsProductModel) FindAllOnlyByIds(ctx context.Context, ids []int64) (resp []*FsProduct, err error) {
err = p.db.WithContext(ctx).Model(&FsProduct{}).Where("`id` IN (?)", ids).Find(&resp).Error
return resp, err
}
func (p *FsProductModel) GetRandomProductListInIds(ctx context.Context,ids []int64, limit int) (resp []FsProduct, err error) {
err = p.db.WithContext(ctx).Model(&FsProduct{}).
Where("`id` in (?) and `is_del` =? and `is_shelf` = ?", ids,0, 1).Order("RAND()").Limit(limit).Find(&resp).Error
return resp,err
}

View File

@ -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 OtherProductListHandler(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.OtherProductListReq
// 如果端点有请求结构体则使用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.NewOtherProductListLogic(r.Context(), svcCtx)
resp := l.OtherProductList(&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)
}
}
}

View File

@ -47,6 +47,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
Path: "/product/save-design",
Handler: SaveDesignHandler(serverCtx),
},
{
Method: http.MethodPost,
Path: "/product/other-list",
Handler: OtherProductListHandler(serverCtx),
},
},
)
}

View File

@ -0,0 +1,60 @@
package logic
import (
"errors"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"fusenapi/utils/image"
"gorm.io/gorm"
"context"
"fusenapi/server/product/internal/svc"
"fusenapi/server/product/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type OtherProductListLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewOtherProductListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *OtherProductListLogic {
return &OtherProductListLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *OtherProductListLogic) OtherProductList(req *types.OtherProductListReq, userinfo *auth.UserInfo) (resp *basic.Response) {
if userinfo.GetIdType() != auth.IDTYPE_User {
return resp.SetStatusWithMessage(basic.CodeUnAuth, "please login first")
}
if req.Num <= 0 {
req.Num = 4
}
if req.Size > 0{
req.Size = image.GetCurrentSize(req.Size)
}
tagInfo,err := l.svcCtx.AllModels.FsTags.FindOne(l.ctx,req.Cid)
if err != nil{
if errors.Is(err,gorm.ErrRecordNotFound){
return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr,"the tag info is not exists")
}
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeDbSqlErr,"failed to get tage info")
}
if *tagInfo.Status != 1{
return resp.SetStatusWithMessage(basic.CodeDbSqlErr,"tage info is not available")
}
if tagInfo.RecommendProduct == nil || *tagInfo.RecommendProduct == "" {
return resp.SetStatusWithMessage(basic.CodeOK,"success")
}
//获取推荐产品信息
l.svcCtx.AllModels.FsProduct.GetRandomProductList(l.ctx,)
//TODO 明天再做
return resp.SetStatus(basic.CodeOK)
}

View File

@ -215,6 +215,21 @@ type ColorFill struct {
Fill string `json:"fill"`
}
type OtherProductListReq struct {
Cid int64 `json:"cid"`
Num int64 `json:"num"`
Size uint32 `json:"size"`
}
type OtherProductListRsp struct {
Title string `json:"title"`
Cover string `json:"cover"`
CoverImg string `json:"cover_img"`
Sn string `json:"sn"`
Id int64 `json:"id"`
SkuId int64 `json:"sku_id"`
}
type Request struct {
}

View File

@ -31,6 +31,9 @@ service product {
//保存设计信息
@handler SaveDesignHandler
post /product/save-design(SaveDesignReq) returns (response);
//其他产品推荐列表
@handler OtherProductListHandler
post /product/other-list (OtherProductListReq) returns (response);
}
//获取产品列表
@ -222,4 +225,18 @@ type DesignLogo {
}
type ColorFill {
Fill string `json:"fill"`
}
//其他产品推荐列表
type OtherProductListReq {
Cid int64 `json:"cid"`
Num int64 `json:"num"`
Size uint32 `json:"size"`
}
type OtherProductListRsp {
Title string `json:"title"`
Cover string `json:"cover"`
CoverImg string `json:"cover_img"`
Sn string `json:"sn"`
Id int64 `json:"id"`
SkuId int64 `json:"sku_id"`
}