diff --git a/model/gmodel/fs_product_logic.go b/model/gmodel/fs_product_logic.go index 14b1bf3e..5f067e18 100755 --- a/model/gmodel/fs_product_logic.go +++ b/model/gmodel/fs_product_logic.go @@ -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 +} \ No newline at end of file diff --git a/server/product/internal/handler/otherproductlisthandler.go b/server/product/internal/handler/otherproductlisthandler.go new file mode 100644 index 00000000..8510f6f9 --- /dev/null +++ b/server/product/internal/handler/otherproductlisthandler.go @@ -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) + } + } +} diff --git a/server/product/internal/handler/routes.go b/server/product/internal/handler/routes.go index d644038e..2977ab20 100644 --- a/server/product/internal/handler/routes.go +++ b/server/product/internal/handler/routes.go @@ -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), + }, }, ) } diff --git a/server/product/internal/logic/getproductlistlogic.go b/server/product/internal/logic/getproductlistlogic.go index 30b966bd..38ef0b96 100644 --- a/server/product/internal/logic/getproductlistlogic.go +++ b/server/product/internal/logic/getproductlistlogic.go @@ -154,7 +154,7 @@ func (l *GetProductListLogic) GetProductList(req *types.GetProductListReq, useri IsEnv: *v.IsProtection, IsMicro: *v.IsMicrowave, SizeNum: uint32(sizeNum), - MiniPrice: format.CentoDollar(minPrice), + MiniPrice: minPrice, } //千人千面处理 thousandFaceImageFormatReq := image.ThousandFaceImageFormatReq{ diff --git a/server/product/internal/logic/otherproductlistlogic.go b/server/product/internal/logic/otherproductlistlogic.go new file mode 100644 index 00000000..6a30086f --- /dev/null +++ b/server/product/internal/logic/otherproductlistlogic.go @@ -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) +} diff --git a/server/product/internal/types/types.go b/server/product/internal/types/types.go index 46541b55..83b37ee2 100644 --- a/server/product/internal/types/types.go +++ b/server/product/internal/types/types.go @@ -28,17 +28,17 @@ type HrefUrl struct { } type Items struct { - Id int64 `json:"id"` - Sn string `json:"sn"` - Title string `json:"title"` - Cover string `json:"cover"` - Intro string `json:"intro"` - CoverImg string `json:"cover_img"` - IsEnv int64 `json:"isEnv"` - IsMicro int64 `json:"isMicro"` - SizeNum uint32 `json:"sizeNum"` - MiniPrice float64 `json:"miniPrice"` - CoverDefault string `json:"coverDefault"` + Id int64 `json:"id"` + Sn string `json:"sn"` + Title string `json:"title"` + Cover string `json:"cover"` + Intro string `json:"intro"` + CoverImg string `json:"cover_img"` + IsEnv int64 `json:"isEnv"` + IsMicro int64 `json:"isMicro"` + SizeNum uint32 `json:"sizeNum"` + MiniPrice int64 `json:"miniPrice"` + CoverDefault string `json:"coverDefault"` } type GetSuccessRecommandReq struct { @@ -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 { } diff --git a/server_api/product.api b/server_api/product.api index 9662c112..0ad14b7b 100644 --- a/server_api/product.api +++ b/server_api/product.api @@ -31,6 +31,9 @@ service product { //保存设计信息 @handler SaveDesignHandler post /product/save-design(SaveDesignReq) returns (response); + //其他产品推荐列表 + @handler OtherProductListHandler + post /product/other-list (OtherProductListReq) returns (response); } //获取产品列表 @@ -53,17 +56,17 @@ type HrefUrl { Href string `json:"href"` } type Items { - Id int64 `json:"id"` - Sn string `json:"sn"` - Title string `json:"title"` - Cover string `json:"cover"` - Intro string `json:"intro"` - CoverImg string `json:"cover_img"` - IsEnv int64 `json:"isEnv"` - IsMicro int64 `json:"isMicro"` - SizeNum uint32 `json:"sizeNum"` - MiniPrice float64 `json:"miniPrice"` - CoverDefault string `json:"coverDefault"` + Id int64 `json:"id"` + Sn string `json:"sn"` + Title string `json:"title"` + Cover string `json:"cover"` + Intro string `json:"intro"` + CoverImg string `json:"cover_img"` + IsEnv int64 `json:"isEnv"` + IsMicro int64 `json:"isMicro"` + SizeNum uint32 `json:"sizeNum"` + MiniPrice int64 `json:"miniPrice"` + CoverDefault string `json:"coverDefault"` } //获取成功后的推荐产品 type GetSuccessRecommandReq { @@ -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"` } \ No newline at end of file