Merge branch 'develop' of https://gitee.com/fusenpack/fusenapi into develop
This commit is contained in:
78
server/product/internal/handler/getmodelbypidhandler.go
Normal file
78
server/product/internal/handler/getmodelbypidhandler.go
Normal 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 GetModelByPidHandler(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.GetModelByPidReq
|
||||
// 如果端点有请求结构体,则使用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.NewGetModelByPidLogic(r.Context(), svcCtx)
|
||||
resp := l.GetModelByPid(&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)
|
||||
}
|
||||
}
|
||||
}
|
||||
78
server/product/internal/handler/getpricebypidhandler.go
Normal file
78
server/product/internal/handler/getpricebypidhandler.go
Normal 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 GetPriceByPidHandler(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.GetPriceByPidReq
|
||||
// 如果端点有请求结构体,则使用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.NewGetPriceByPidLogic(r.Context(), svcCtx)
|
||||
resp := l.GetPriceByPid(&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)
|
||||
}
|
||||
}
|
||||
}
|
||||
78
server/product/internal/handler/getrenderdesignhandler.go
Normal file
78
server/product/internal/handler/getrenderdesignhandler.go
Normal 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 GetRenderDesignHandler(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.GetRenderDesignReq
|
||||
// 如果端点有请求结构体,则使用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.NewGetRenderDesignLogic(r.Context(), svcCtx)
|
||||
resp := l.GetRenderDesign(&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)
|
||||
}
|
||||
}
|
||||
}
|
||||
78
server/product/internal/handler/gettagproductlisthandler.go
Normal file
78
server/product/internal/handler/gettagproductlisthandler.go
Normal 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 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响应并记录错误消息
|
||||
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请求体中解析请求数据
|
||||
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.NewGetTagProductListLogic(r.Context(), svcCtx)
|
||||
resp := l.GetTagProductList(&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)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -57,6 +57,26 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
|
||||
Path: "/api/product/recommand",
|
||||
Handler: GetRecommandProductListHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
Method: http.MethodGet,
|
||||
Path: "/api/product/tag_product_list",
|
||||
Handler: GetTagProductListHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
Method: http.MethodGet,
|
||||
Path: "/api/product/render_design",
|
||||
Handler: GetRenderDesignHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
Method: http.MethodGet,
|
||||
Path: "/api/product/get_model_by_pid",
|
||||
Handler: GetModelByPidHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
Method: http.MethodGet,
|
||||
Path: "/api/product/get_price_by_pid",
|
||||
Handler: GetPriceByPidHandler(serverCtx),
|
||||
},
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
85
server/product/internal/logic/getmodelbypidlogic.go
Normal file
85
server/product/internal/logic/getmodelbypidlogic.go
Normal file
@@ -0,0 +1,85 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fusenapi/constants"
|
||||
"fusenapi/utils/auth"
|
||||
"fusenapi/utils/basic"
|
||||
"gorm.io/gorm"
|
||||
"strings"
|
||||
|
||||
"context"
|
||||
|
||||
"fusenapi/server/product/internal/svc"
|
||||
"fusenapi/server/product/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type GetModelByPidLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewGetModelByPidLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetModelByPidLogic {
|
||||
return &GetModelByPidLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *GetModelByPidLogic) GetModelByPid(req *types.GetModelByPidReq, userinfo *auth.UserInfo) (resp *basic.Response) {
|
||||
req.Pid = strings.Trim(req.Pid, " ")
|
||||
if req.Pid == "" {
|
||||
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "err param:pid is empty")
|
||||
}
|
||||
//获取产品信息(只是获取id)
|
||||
productInfo, err := l.svcCtx.AllModels.FsProduct.FindOneBySn(l.ctx, req.Pid, "id")
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "the product is not exists")
|
||||
}
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get product info")
|
||||
}
|
||||
//获取产品尺寸列表(只是获取id)
|
||||
sizeList, err := l.svcCtx.AllModels.FsProductSize.GetAllByProductIds(l.ctx, []int64{productInfo.Id}, "id")
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get size list")
|
||||
}
|
||||
if len(sizeList) == 0 {
|
||||
return resp.SetStatusWithMessage(basic.CodeOK, "success:size list is empty", []interface{}{})
|
||||
}
|
||||
sizeIds := make([]int64, 0, len(sizeList))
|
||||
for _, v := range sizeList {
|
||||
sizeIds = append(sizeIds, v.Id)
|
||||
}
|
||||
//获取模型数据(只是获取model_info)
|
||||
model3dList, err := l.svcCtx.AllModels.FsProductModel3d.GetAllBySizeIdsTag(l.ctx, sizeIds, constants.TAG_MODEL, "id,model_info")
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get model list")
|
||||
}
|
||||
if len(model3dList) == 0 {
|
||||
return resp.SetStatusWithMessage(basic.CodeOK, "success:model list is empty", []interface{}{})
|
||||
}
|
||||
//处理数据
|
||||
mapModel := make(map[int64]interface{})
|
||||
for _, v := range model3dList {
|
||||
if v.ModelInfo == nil || *v.ModelInfo == "" {
|
||||
mapModel[v.Id] = nil
|
||||
continue
|
||||
}
|
||||
var info interface{}
|
||||
if err = json.Unmarshal([]byte(*v.ModelInfo), &info); err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeJsonErr, "failed to parse model info json")
|
||||
}
|
||||
mapModel[v.Id] = info
|
||||
}
|
||||
return resp.SetStatusWithMessage(basic.CodeOK, "success", mapModel)
|
||||
}
|
||||
78
server/product/internal/logic/getpricebypidlogic.go
Normal file
78
server/product/internal/logic/getpricebypidlogic.go
Normal file
@@ -0,0 +1,78 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"fusenapi/utils/auth"
|
||||
"fusenapi/utils/basic"
|
||||
"fusenapi/utils/format"
|
||||
"gorm.io/gorm"
|
||||
"strings"
|
||||
|
||||
"context"
|
||||
|
||||
"fusenapi/server/product/internal/svc"
|
||||
"fusenapi/server/product/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type GetPriceByPidLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewGetPriceByPidLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetPriceByPidLogic {
|
||||
return &GetPriceByPidLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *GetPriceByPidLogic) GetPriceByPid(req *types.GetPriceByPidReq, userinfo *auth.UserInfo) (resp *basic.Response) {
|
||||
req.Pid = strings.Trim(req.Pid, " ")
|
||||
if req.Pid == "" {
|
||||
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "err param:pid is empty")
|
||||
}
|
||||
//获取产品信息(只是获取id)
|
||||
productInfo, err := l.svcCtx.AllModels.FsProduct.FindOneBySn(l.ctx, req.Pid, "id")
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "the product is not exists")
|
||||
}
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get product info")
|
||||
}
|
||||
//查询产品价格
|
||||
priceList, err := l.svcCtx.AllModels.FsProductPrice.GetPriceListByProductIds(l.ctx, []int64{productInfo.Id})
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get price list")
|
||||
}
|
||||
//处理价格信息
|
||||
mapRsp := make(map[string]interface{})
|
||||
for _, v := range priceList {
|
||||
mapKey := fmt.Sprintf("_%d", v.Id)
|
||||
stepNum, err := format.StrSlicToIntSlice(strings.Split(*v.StepNum, ","))
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeServiceErr, fmt.Sprintf("failed to parse step num,price_id=%d", v.Id))
|
||||
}
|
||||
/*$price['step_num'] = explode(',', $price['step_num']);
|
||||
$price['step_price'] = explode(',', $price['step_price']);
|
||||
while ($price['min_buy_num'] < end($price['step_num']) + 5) {
|
||||
$outData["{$price['size_id']}"]['items'][] = [
|
||||
'num' => intval($price['min_buy_num']),
|
||||
'total_num' => $price['min_buy_num'] * $price['each_box_num'],
|
||||
'price' => ProductPriceService::getPrice($price['min_buy_num'], $price['step_num'], $price['step_price'])
|
||||
];
|
||||
$price['min_buy_num'] += 1;
|
||||
}
|
||||
|
||||
$outData["{$price['size_id']}"]['min_price'] = floatval(end($price['step_price']) / 100);
|
||||
$outData["{$price['size_id']}"]['max_price'] = floatval(reset($price['step_price']) / 100);*/
|
||||
}
|
||||
return resp.SetStatus(basic.CodeOK)
|
||||
}
|
||||
@@ -123,14 +123,11 @@ func (l *GetProductInfoLogic) GetProductInfo(req *types.GetProductInfoReq, useri
|
||||
for _, v := range sizeList {
|
||||
model3dInfo, ok := mapModel3dWithSizeIdIndex[v.Id]
|
||||
if !ok {
|
||||
fmt.Println("没有模型:", v.Id)
|
||||
continue
|
||||
}
|
||||
if _, ok = mapTemplateModelId[model3dInfo.Id]; !ok {
|
||||
fmt.Println("没有模板:", v.Id)
|
||||
continue
|
||||
}
|
||||
fmt.Println("剩下的:", v.Id)
|
||||
var title types.SizeTitle
|
||||
if err = json.Unmarshal([]byte(*v.Title), &title); err != nil {
|
||||
logx.Error(err)
|
||||
@@ -434,7 +431,7 @@ func (l *GetProductInfoLogic) getRenderDesign(clientNo string) interface{} {
|
||||
renderDesign, err := l.svcCtx.AllModels.FsProductRenderDesign.FindOneRenderDesignByParams(l.ctx, gmodel.FindOneRenderDesignByParamsReq{
|
||||
ClientNo: &clientNo,
|
||||
Fields: "id,info,material_id,optional_id,size_id,template_id",
|
||||
SortType: 2,
|
||||
OrderBy: "`id` DESC",
|
||||
})
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
|
||||
@@ -15,6 +15,7 @@ import (
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlc"
|
||||
"gorm.io/gorm"
|
||||
"math"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
@@ -44,13 +45,14 @@ func (l *GetProductListLogic) GetProductList(req *types.GetProductListReq, useri
|
||||
}
|
||||
return resp.SetStatusWithMessage(basic.CodeOK, "success", demo)
|
||||
}
|
||||
if req.Page <= 0 {
|
||||
req.Page = 1
|
||||
if req.Page <= 0 && req.Page > 10000 {
|
||||
req.Page = constants.DEFAULT_PAGE
|
||||
}
|
||||
//获取合适尺寸
|
||||
if req.Size > 0 {
|
||||
req.Size = image.GetCurrentSize(req.Size)
|
||||
}
|
||||
pageSize := constants.DEFAULT_PAGE_SIZE
|
||||
//查询用户信息(不用判断存在)
|
||||
userModel := gmodel.NewFsUserModel(l.svcCtx.MysqlConn)
|
||||
user, err := userModel.FindUserById(l.ctx, userinfo.UserId)
|
||||
@@ -60,7 +62,7 @@ func (l *GetProductListLogic) GetProductList(req *types.GetProductListReq, useri
|
||||
}
|
||||
//查询符合的产品列表
|
||||
productModel := gmodel.NewFsProductModel(l.svcCtx.MysqlConn)
|
||||
productList, err := productModel.GetProductListByTypeIds(l.ctx, []int64{req.Cid}, "sort-desc")
|
||||
productList, total, err := productModel.GetProductListByTypeIds(l.ctx, []int64{req.Cid}, int(req.Page), pageSize, "sort-desc")
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get product list")
|
||||
@@ -172,6 +174,14 @@ func (l *GetProductListLogic) GetProductList(req *types.GetProductListReq, useri
|
||||
return resp.SetStatusWithMessage(basic.CodeOK, "success", types.GetProductListRsp{
|
||||
Ob: types.Ob{
|
||||
Items: itemList,
|
||||
}, TypeName: *tagInfo.Title, Description: *tagInfo.Description,
|
||||
Meta: types.Meta{
|
||||
TotalCount: total,
|
||||
PageCount: int64(math.Ceil(float64(total) / float64(pageSize))),
|
||||
CurrentPage: int(req.Page),
|
||||
PerPage: pageSize,
|
||||
},
|
||||
},
|
||||
TypeName: *tagInfo.Title,
|
||||
Description: *tagInfo.Description,
|
||||
})
|
||||
}
|
||||
|
||||
73
server/product/internal/logic/getrenderdesignlogic.go
Normal file
73
server/product/internal/logic/getrenderdesignlogic.go
Normal file
@@ -0,0 +1,73 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fusenapi/model/gmodel"
|
||||
"fusenapi/utils/auth"
|
||||
"fusenapi/utils/basic"
|
||||
"gorm.io/gorm"
|
||||
"strings"
|
||||
|
||||
"context"
|
||||
|
||||
"fusenapi/server/product/internal/svc"
|
||||
"fusenapi/server/product/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type GetRenderDesignLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewGetRenderDesignLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetRenderDesignLogic {
|
||||
return &GetRenderDesignLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *GetRenderDesignLogic) GetRenderDesign(req *types.GetRenderDesignReq, userinfo *auth.UserInfo) (resp *basic.Response) {
|
||||
req.Sn = strings.Trim(req.Sn, " ")
|
||||
if req.Sn == "" {
|
||||
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "err param:sn is empty")
|
||||
}
|
||||
renderDesign, err := l.svcCtx.AllModels.FsProductRenderDesign.FindOneRenderDesignByParams(l.ctx, gmodel.FindOneRenderDesignByParamsReq{
|
||||
Sn: &req.Sn,
|
||||
Fields: "id,info,material_id,optional_id,size_id,template_id",
|
||||
OrderBy: "`id` DESC",
|
||||
})
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "render design is not exists")
|
||||
}
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get render design info")
|
||||
}
|
||||
var color []string
|
||||
if renderDesign.LogoColor != nil && *renderDesign.LogoColor != "" {
|
||||
if err = json.Unmarshal([]byte(*renderDesign.LogoColor), &color); err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeJsonErr, "failed to parse render logo color")
|
||||
}
|
||||
}
|
||||
var info interface{}
|
||||
if renderDesign.Info != nil && *renderDesign.Info != "" {
|
||||
if err = json.Unmarshal([]byte(*renderDesign.Info), &info); err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeJsonErr, "failed to parse render design info")
|
||||
}
|
||||
}
|
||||
return resp.SetStatusWithMessage(basic.CodeOK, "success", types.GetRenderDesignRsp{
|
||||
Id: renderDesign.Id,
|
||||
Info: info,
|
||||
OptionalId: *renderDesign.OptionalId,
|
||||
SizeId: *renderDesign.SizeId,
|
||||
TemplateId: *renderDesign.TemplateId,
|
||||
LogoColor: color,
|
||||
})
|
||||
}
|
||||
@@ -63,7 +63,7 @@ func (l *GetSizeByProductLogic) GetSizeByProduct(userinfo *auth.UserInfo) (resp
|
||||
productIds = append(productIds, v.Id)
|
||||
}
|
||||
productSizeModel := gmodel.NewFsProductSizeModel(l.svcCtx.MysqlConn)
|
||||
productSizeList, err := productSizeModel.GetAllByProductIds(l.ctx, productIds, "sort-desc")
|
||||
productSizeList, err := productSizeModel.GetAllByProductIds(l.ctx, productIds, "sort DESC")
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get product size list")
|
||||
|
||||
270
server/product/internal/logic/gettagproductlistlogic.go
Normal file
270
server/product/internal/logic/gettagproductlistlogic.go
Normal file
@@ -0,0 +1,270 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fusenapi/model/gmodel"
|
||||
"fusenapi/utils/auth"
|
||||
"fusenapi/utils/basic"
|
||||
"fusenapi/utils/format"
|
||||
"fusenapi/utils/image"
|
||||
"gorm.io/gorm"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"context"
|
||||
|
||||
"fusenapi/server/product/internal/svc"
|
||||
"fusenapi/server/product/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type GetTagProductListLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewGetTagProductListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetTagProductListLogic {
|
||||
return &GetTagProductListLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *GetTagProductListLogic) GetTagProductList(req *types.GetTagProductListReq, userinfo *auth.UserInfo) (resp *basic.Response) {
|
||||
//获取合适尺寸
|
||||
if req.Size > 0 {
|
||||
req.Size = image.GetCurrentSize(req.Size)
|
||||
}
|
||||
//查询用户信息(不用判断存在)
|
||||
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{
|
||||
Status: &tStatus,
|
||||
OrderBy: "`sort` DESC",
|
||||
WithChild: true, //需要子集
|
||||
}
|
||||
//传入分类id
|
||||
if req.Cid > 0 {
|
||||
//获取该类行的levelprefix
|
||||
tagData, err := l.svcCtx.AllModels.FsTags.FindOne(l.ctx, req.Cid)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "param cid is invalid:record not found")
|
||||
}
|
||||
}
|
||||
tReq.LevelPrefixLeftLike = *tagData.LevelPrefix
|
||||
}
|
||||
tagList, err := l.svcCtx.AllModels.FsTags.GetAllTagByParams(l.ctx, tReq)
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get tag list")
|
||||
}
|
||||
if len(tagList) == 0 {
|
||||
return resp.SetStatusWithMessage(basic.CodeOK, "success", []interface{}{})
|
||||
}
|
||||
typeIds := make([]int64, 0, len(tagList))
|
||||
for _, v := range tagList {
|
||||
typeIds = append(typeIds, v.Id)
|
||||
}
|
||||
//查询符合的产品列表
|
||||
pIsDel := int64(0)
|
||||
pStatus := int64(1)
|
||||
pIsShelf := int64(1)
|
||||
pReq := gmodel.GetProductListByParamsReq{
|
||||
Type: typeIds,
|
||||
IsDel: &pIsDel,
|
||||
IsShelf: &pIsShelf,
|
||||
Status: &pStatus,
|
||||
OrderBy: "`sort` DESC",
|
||||
}
|
||||
//获取产品列表
|
||||
productList, err := l.svcCtx.AllModels.FsProduct.GetProductListByParams(l.ctx, pReq)
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get product list")
|
||||
}
|
||||
//提取产品ids
|
||||
productIds := make([]int64, 0, len(productList))
|
||||
for _, v := range productList {
|
||||
productIds = append(productIds, v.Id)
|
||||
}
|
||||
productPriceList, err := l.svcCtx.AllModels.FsProductPrice.GetSimplePriceListByProductIds(l.ctx, productIds)
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get product min price list")
|
||||
}
|
||||
//存储产品最小价格
|
||||
mapProductMinPrice := make(map[int64]int64)
|
||||
for _, v := range productPriceList {
|
||||
priceStrSlic := strings.Split(v.Price, ",")
|
||||
priceSlice, err := format.StrSlicToIntSlice(priceStrSlic)
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeServiceErr, err.Error())
|
||||
}
|
||||
if len(priceSlice) == 0 {
|
||||
continue
|
||||
}
|
||||
sort.Ints(priceSlice)
|
||||
mapProductMinPrice[v.ProductId] = int64(priceSlice[0])
|
||||
}
|
||||
//获取模板
|
||||
productTemplatesV2, err := l.svcCtx.AllModels.FsProductTemplateV2.FindAllByProductIds(l.ctx, productIds)
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeServiceErr, "get product template_v2 err")
|
||||
}
|
||||
mapProductTemplate := make(map[int64]struct{})
|
||||
for _, v := range productTemplatesV2 {
|
||||
mapProductTemplate[*v.ProductId] = struct{}{}
|
||||
}
|
||||
//获取产品尺寸数量
|
||||
productSizeCountList, err := l.svcCtx.AllModels.FsProductSize.GetGroupProductSizeByStatus(l.ctx, productIds, 1)
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeServiceErr, "get product size count err")
|
||||
}
|
||||
mapProductSizeCount := make(map[int64]int64)
|
||||
for _, v := range productSizeCountList {
|
||||
mapProductSizeCount[v.ProductId] = v.Num
|
||||
}
|
||||
mapTagLevel := make(map[string]*types.TagItem)
|
||||
minLevel := int64(0) //记录最小等级数字
|
||||
for _, tagInfo := range tagList {
|
||||
if minLevel == 0 && *tagInfo.Level > 0 {
|
||||
minLevel = *tagInfo.Level
|
||||
}
|
||||
if minLevel > *tagInfo.Level {
|
||||
minLevel = *tagInfo.Level
|
||||
}
|
||||
//获取分类产品列表
|
||||
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,
|
||||
LevelPrefix: *tagInfo.LevelPrefix,
|
||||
Icon: *tagInfo.Icon,
|
||||
Sort: *tagInfo.Sort,
|
||||
Description: *tagInfo.Description,
|
||||
ChildTagList: make([]*types.TagItem, 0, 100),
|
||||
}
|
||||
//当前tag保存入map
|
||||
mapTagLevel[*tagInfo.LevelPrefix] = &tagTem
|
||||
}
|
||||
//组装等级从属关系
|
||||
for prefix, tagItem := range mapTagLevel {
|
||||
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
|
||||
}
|
||||
}
|
||||
//最终值提取最高级别那一层出来
|
||||
tagListRsp := make([]types.TagItem, 0, len(mapTagLevel))
|
||||
for _, v := range mapTagLevel {
|
||||
if v.Level != minLevel {
|
||||
continue
|
||||
}
|
||||
tagListRsp = append(tagListRsp, *v)
|
||||
}
|
||||
//排序
|
||||
sort.Slice(tagListRsp, func(i, j int) bool {
|
||||
return tagListRsp[i].Sort < tagListRsp[j].Sort
|
||||
})
|
||||
return resp.SetStatusWithMessage(basic.CodeOK, "success", types.GetTagProductListRsp{
|
||||
TotalCategory: len(mapTagLevel),
|
||||
TagList: tagListRsp,
|
||||
})
|
||||
}
|
||||
|
||||
// 获取对应tag的产品列表
|
||||
type getTagProductsReq struct {
|
||||
TagId int64
|
||||
ProductList []gmodel.FsProduct
|
||||
MapProductMinPrice map[int64]int64
|
||||
MapProductTemplate map[int64]struct{}
|
||||
MapProductSizeCount map[int64]int64
|
||||
Size uint32
|
||||
User gmodel.FsUser
|
||||
}
|
||||
|
||||
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.TagId {
|
||||
continue
|
||||
}
|
||||
minPrice, ok := req.MapProductMinPrice[productInfo.Id]
|
||||
_, tmpOk := req.MapProductTemplate[productInfo.Id]
|
||||
//无最小价格则不显示 || 没有模板也不显示
|
||||
if !ok || !tmpOk {
|
||||
continue
|
||||
}
|
||||
sizeNum := int64(0)
|
||||
if mapSizeNum, ok := req.MapProductSizeCount[productInfo.Id]; ok {
|
||||
sizeNum = mapSizeNum
|
||||
}
|
||||
item := types.TagProduct{
|
||||
ProductId: productInfo.Id,
|
||||
Sn: *productInfo.Sn,
|
||||
Title: *productInfo.Title,
|
||||
Intro: *productInfo.Intro,
|
||||
IsEnv: *productInfo.IsProtection,
|
||||
IsMicro: *productInfo.IsMicrowave,
|
||||
SizeNum: uint32(sizeNum),
|
||||
MiniPrice: minPrice,
|
||||
}
|
||||
//千人千面处理
|
||||
r := image.ThousandFaceImageFormatReq{
|
||||
Size: int(req.Size),
|
||||
IsThousandFace: 0,
|
||||
Cover: *productInfo.Cover,
|
||||
CoverImg: *productInfo.CoverImg,
|
||||
CoverDefault: *productInfo.CoverImg,
|
||||
ProductId: productInfo.Id,
|
||||
UserId: req.User.Id,
|
||||
}
|
||||
if req.User.Id != 0 {
|
||||
r.IsThousandFace = int(*req.User.IsThousandFace)
|
||||
}
|
||||
image.ThousandFaceImageFormat(&r)
|
||||
item.Cover = r.Cover
|
||||
item.CoverImg = r.CoverImg
|
||||
item.CoverDefault = r.CoverDefault
|
||||
//加入分类产品切片
|
||||
productListRsp = append(productListRsp, item)
|
||||
}
|
||||
return productListRsp
|
||||
}
|
||||
@@ -23,10 +23,6 @@ type Ob struct {
|
||||
Meta Meta `json:"_meta"`
|
||||
}
|
||||
|
||||
type HrefUrl struct {
|
||||
Href string `json:"href"`
|
||||
}
|
||||
|
||||
type Items struct {
|
||||
Id int64 `json:"id"`
|
||||
Sn string `json:"sn"`
|
||||
@@ -248,6 +244,63 @@ type GetRecommandProductListRsp struct {
|
||||
Intro string `json:"intro"`
|
||||
}
|
||||
|
||||
type GetTagProductListReq struct {
|
||||
Cid int64 `form:"cid,optional"` //分类id
|
||||
Size uint32 `form:"size,optional"` //尺寸
|
||||
}
|
||||
|
||||
type GetTagProductListRsp struct {
|
||||
TotalCategory int `json:"total_category"`
|
||||
TagList []TagItem `json:"tag_list"`
|
||||
}
|
||||
|
||||
type TagItem struct {
|
||||
TypeName string `json:"type_name"`
|
||||
TypeId int64 `json:"type_id"`
|
||||
Description string `json:"description"`
|
||||
Level int64 `json:"level"`
|
||||
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"`
|
||||
}
|
||||
|
||||
type TagProduct struct {
|
||||
ProductId int64 `json:"product_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:"is_env"`
|
||||
IsMicro int64 `json:"is_micro"`
|
||||
SizeNum uint32 `json:"size_num"`
|
||||
MiniPrice int64 `json:"mini_price"`
|
||||
CoverDefault string `json:"cover_default"`
|
||||
}
|
||||
|
||||
type GetRenderDesignReq struct {
|
||||
Sn string `form:"sn"`
|
||||
}
|
||||
|
||||
type GetRenderDesignRsp struct {
|
||||
Id int64 `json:"id"`
|
||||
Info interface{} `json:"info"`
|
||||
OptionalId int64 `json:"optional_id"`
|
||||
SizeId int64 `json:"size_id"`
|
||||
TemplateId int64 `json:"template_id"`
|
||||
LogoColor []string `json:"logo_color"`
|
||||
}
|
||||
|
||||
type GetModelByPidReq struct {
|
||||
Pid string `form:"pid"` //实际上是产品sn
|
||||
}
|
||||
|
||||
type GetPriceByPidReq struct {
|
||||
Pid string `form:"pid"`
|
||||
}
|
||||
|
||||
type Request struct {
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user