This commit is contained in:
laodaming 2023-07-17 15:38:41 +08:00
parent 73ce1e6db3
commit 2eb8bf936f
5 changed files with 225 additions and 0 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 GetFittingByPidHandler(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.GetFittingByPidReq
// 如果端点有请求结构体则使用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.NewGetFittingByPidLogic(r.Context(), svcCtx)
resp := l.GetFittingByPid(&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

@ -87,6 +87,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
Path: "/api/product/get_template_by_pid",
Handler: GetTemplateByPidHandler(serverCtx),
},
{
Method: http.MethodGet,
Path: "/api/product/get_fitting_by_pid",
Handler: GetFittingByPidHandler(serverCtx),
},
},
)
}

View File

@ -0,0 +1,116 @@
package logic
import (
"encoding/json"
"errors"
"fusenapi/constants"
"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 GetFittingByPidLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewGetFittingByPidLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetFittingByPidLogic {
return &GetFittingByPidLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *GetFittingByPidLogic) GetFittingByPid(req *types.GetFittingByPidReq, 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.SetStatusAddMessage(basic.CodeOK, "success:size list is empty")
}
sizeIds := make([]int64, 0, len(sizeList))
for _, v := range sizeList {
sizeIds = append(sizeIds, v.Id)
}
//获取配件id
modelList, err := l.svcCtx.AllModels.FsProductModel3d.GetAllBySizeIdsTag(l.ctx, sizeIds, constants.TAG_MODEL, "part_id")
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get model list")
}
if len(modelList) == 0 {
return resp.SetStatusAddMessage(basic.CodeOK, "success:model list is empty")
}
partIds := make([]int64, 0, len(modelList))
for _, v := range modelList {
partIds = append(partIds, *v.PartId)
}
//获取配件数据
fittingList, err := l.svcCtx.AllModels.FsProductModel3d.GetAllByIds(l.ctx, partIds)
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get part list")
}
if len(fittingList) == 0 {
return resp.SetStatusAddMessage(basic.CodeOK, "success:fitting list is empty")
}
//处理组装数据返回
listRsp := make([]types.GetFittingByPidRsp, 0, len(fittingList))
for _, fitting := range fittingList {
materialImg := ""
var tInfo *gmodel.FsProductTemplateV2
//贴图,如果绑定了公共模板,则获取公共模板的贴图数据
if *fitting.OptionTemplate > 0 {
tInfo, err = l.svcCtx.AllModels.FsProductTemplateV2.FindOne(l.ctx, *fitting.OptionTemplate)
} else { //否则取该配件下的模板贴图
tInfo, err = l.svcCtx.AllModels.FsProductTemplateV2.FindOneByModelId(l.ctx, fitting.Id)
}
if err == nil {
materialImg = *tInfo.MaterialImg
} else {
logx.Error(err)
}
var modelInfo interface{}
if fitting.ModelInfo != nil && *fitting.ModelInfo != "" {
if err = json.Unmarshal([]byte(*fitting.ModelInfo), &modelInfo); err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeJsonErr, "failed to parse model json info")
}
}
listRsp = append(listRsp, types.GetFittingByPidRsp{
Id: fitting.Id,
MaterialImg: materialImg,
Title: *fitting.Title,
Price: *fitting.Price,
ModelInfo: modelInfo,
})
}
return resp.SetStatusWithMessage(basic.CodeOK, "success", listRsp)
}

View File

@ -331,6 +331,18 @@ type GetTemplateByPidReq struct {
Size uint32 `form:"size"`
}
type GetFittingByPidReq struct {
Pid string `form:"pid"`
}
type GetFittingByPidRsp struct {
Id int64 `json:"id"`
MaterialImg string `json:"material_img"`
Title string `json:"title"`
Price int64 `json:"price"`
ModelInfo interface{} `json:"model_info"`
}
type Request struct {
}

View File

@ -56,6 +56,9 @@ service product {
//获取产品模板列表
@handler GetTemplateByPidHandler
get /api/product/get_template_by_pid(GetTemplateByPidReq) returns (response);
//获取产品配件数据
@handler GetFittingByPidHandler
get /api/product/get_fitting_by_pid(GetFittingByPidReq) returns (response);
//*********************产品详情分解接口结束***********************
}
@ -357,4 +360,15 @@ type GetSizeByPidRsp {
type GetTemplateByPidReq {
Pid string `form:"pid"`
Size uint32 `form:"size"`
}
//获取产品配件数据
type GetFittingByPidReq {
Pid string `form:"pid"`
}
type GetFittingByPidRsp {
Id int64 `json:"id"`
MaterialImg string `json:"material_img"`
Title string `json:"title"`
Price int64 `json:"price"`
ModelInfo interface{} `json:"model_info"`
}