From 2eb8bf936f220f0124f42c330ef1f25dd13bd9f9 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Mon, 17 Jul 2023 15:38:41 +0800 Subject: [PATCH] fix --- .../handler/getfittingbypidhandler.go | 78 ++++++++++++ server/product/internal/handler/routes.go | 5 + .../internal/logic/getfittingbypidlogic.go | 116 ++++++++++++++++++ server/product/internal/types/types.go | 12 ++ server_api/product.api | 14 +++ 5 files changed, 225 insertions(+) create mode 100644 server/product/internal/handler/getfittingbypidhandler.go create mode 100644 server/product/internal/logic/getfittingbypidlogic.go diff --git a/server/product/internal/handler/getfittingbypidhandler.go b/server/product/internal/handler/getfittingbypidhandler.go new file mode 100644 index 00000000..2c276903 --- /dev/null +++ b/server/product/internal/handler/getfittingbypidhandler.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 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) + } + } +} diff --git a/server/product/internal/handler/routes.go b/server/product/internal/handler/routes.go index ffb78350..da1dd22a 100644 --- a/server/product/internal/handler/routes.go +++ b/server/product/internal/handler/routes.go @@ -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), + }, }, ) } diff --git a/server/product/internal/logic/getfittingbypidlogic.go b/server/product/internal/logic/getfittingbypidlogic.go new file mode 100644 index 00000000..471d9249 --- /dev/null +++ b/server/product/internal/logic/getfittingbypidlogic.go @@ -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) +} diff --git a/server/product/internal/types/types.go b/server/product/internal/types/types.go index b9c09827..54167801 100644 --- a/server/product/internal/types/types.go +++ b/server/product/internal/types/types.go @@ -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 { } diff --git a/server_api/product.api b/server_api/product.api index 3dcf4d8d..413386d5 100644 --- a/server_api/product.api +++ b/server_api/product.api @@ -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"` } \ No newline at end of file