Merge branch 'develop' of https://gitee.com/fusenpack/fusenapi into develop

This commit is contained in:
eson
2023-06-19 15:52:37 +08:00
15 changed files with 265 additions and 138 deletions

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 GetProductDesignHandler(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.GetProductDesignReq
// 如果端点有请求结构体则使用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.NewGetProductDesignLogic(r.Context(), svcCtx)
resp := l.GetProductDesign(&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

@@ -27,6 +27,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
Path: "/product/get-size-by-product",
Handler: GetSizeByProductHandler(serverCtx),
},
{
Method: http.MethodGet,
Path: "/product/design",
Handler: GetProductDesignHandler(serverCtx),
},
},
)
}

View File

@@ -0,0 +1,90 @@
package logic
import (
"fusenapi/model/gmodel"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"context"
"fusenapi/server/product/internal/svc"
"fusenapi/server/product/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type GetProductDesignLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewGetProductDesignLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetProductDesignLogic {
return &GetProductDesignLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *GetProductDesignLogic) GetProductDesign(req *types.GetProductDesignReq, userinfo *auth.UserInfo) (resp *basic.Response) {
if userinfo.GetIdType() != auth.IDTYPE_User {
return resp.SetStatusWithMessage(basic.CodeUnAuth, "please login first")
}
if req.Sn == "" {
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "param sn is required")
}
//获取设计数据
productDesignModel := gmodel.NewFsProductDesignModel(l.svcCtx.MysqlConn)
designInfo, err := productDesignModel.FindOneBySn(l.ctx, req.Sn, userinfo.UserId)
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get design info")
}
if designInfo.Id == 0 {
return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "design info is not exists")
}
//获取产品尺寸信息
productSizeModel := gmodel.NewFsProductSizeModel(l.svcCtx.MysqlConn)
sizeInfo, err := productSizeModel.FindOne(l.ctx, *designInfo.SizeId)
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get product size info")
}
if sizeInfo.Id == 0 {
return resp.SetStatusWithMessage(basic.CodeServiceErr, "product size info is not exists")
}
//获取模板信息
productTemplateV2Model := gmodel.NewFsProductTemplateV2Model(l.svcCtx.MysqlConn)
productTemplateV2Info, err := productTemplateV2Model.FindOne(l.ctx, *designInfo.TemplateId)
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get template info")
}
if productTemplateV2Info.Id == 0 {
return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "template info is not exists")
}
//获取产品模型信息
productModel3dModel := gmodel.NewFsProductModel3dModel(l.svcCtx.MysqlConn)
model3dInfo, err := productModel3dModel.FindOne(l.ctx, *designInfo.OptionalId)
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get product 3D model info")
}
if model3dInfo.Id == 0 {
return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "product 3D model info is not exists")
}
optionalId := *designInfo.OptionalId
if *model3dInfo.Status == 0 && *sizeInfo.Status == 1 && *productTemplateV2Info.Status == 1 && *productTemplateV2Info.IsDel == 0 {
optionalId = 0
}
return resp.SetStatusWithMessage(basic.CodeOK, "success", types.GetProductDesignRsp{
ProductId: *designInfo.ProductId,
TemplateId: *designInfo.TemplateId,
MaterialId: *designInfo.MaterialId,
SizeId: *designInfo.SizeId,
OptionalId: optionalId,
Cover: *designInfo.Cover,
Info: *designInfo.Info,
})
}

View File

@@ -96,6 +96,20 @@ type PriceObj struct {
Price float64 `json:"price"`
}
type GetProductDesignReq struct {
Sn string `form:"sn"`
}
type GetProductDesignRsp struct {
ProductId int64 `json:"product_id"`
TemplateId int64 `json:"template_id"`
MaterialId int64 `json:"material_id"`
SizeId int64 `json:"size_id"`
OptionalId int64 `json:"optional_id"`
Cover string `json:"cover"`
Info string `json:"info"`
}
type Response struct {
Code int `json:"code"`
Message string `json:"msg"`