From edd3d7353d2ad606562e2fd463d9d1ff3a4b6e82 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Fri, 14 Jul 2023 19:25:52 +0800 Subject: [PATCH] fix --- .../internal/handler/getpricebypidhandler.go | 78 +++++++++++++++++++ server/product/internal/handler/routes.go | 5 ++ .../internal/logic/getpricebypidlogic.go | 78 +++++++++++++++++++ server/product/internal/types/types.go | 4 + server_api/product.api | 7 ++ 5 files changed, 172 insertions(+) create mode 100644 server/product/internal/handler/getpricebypidhandler.go create mode 100644 server/product/internal/logic/getpricebypidlogic.go diff --git a/server/product/internal/handler/getpricebypidhandler.go b/server/product/internal/handler/getpricebypidhandler.go new file mode 100644 index 00000000..b4e55c9a --- /dev/null +++ b/server/product/internal/handler/getpricebypidhandler.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 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) + } + } +} diff --git a/server/product/internal/handler/routes.go b/server/product/internal/handler/routes.go index 43a10ab4..74b64ce0 100644 --- a/server/product/internal/handler/routes.go +++ b/server/product/internal/handler/routes.go @@ -72,6 +72,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { Path: "/api/product/get_model_by_pid", Handler: GetModelByPidHandler(serverCtx), }, + { + Method: http.MethodGet, + Path: "/api/product/get_price_by_pid", + Handler: GetPriceByPidHandler(serverCtx), + }, }, ) } diff --git a/server/product/internal/logic/getpricebypidlogic.go b/server/product/internal/logic/getpricebypidlogic.go new file mode 100644 index 00000000..6af193da --- /dev/null +++ b/server/product/internal/logic/getpricebypidlogic.go @@ -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) +} diff --git a/server/product/internal/types/types.go b/server/product/internal/types/types.go index cd953a79..5b3bf41b 100644 --- a/server/product/internal/types/types.go +++ b/server/product/internal/types/types.go @@ -297,6 +297,10 @@ type GetModelByPidReq struct { Pid string `form:"pid"` //实际上是产品sn } +type GetPriceByPidReq struct { + Pid string `form:"pid"` +} + type Request struct { } diff --git a/server_api/product.api b/server_api/product.api index 80a5c41e..93653147 100644 --- a/server_api/product.api +++ b/server_api/product.api @@ -47,6 +47,9 @@ service product { //获取产品模型信息 @handler GetModelByPidHandler get /api/product/get_model_by_pid(GetModelByPidReq) returns (response); + //获取产品阶梯价格列表 + @handler GetPriceByPidHandler + get /api/product/get_price_by_pid(GetPriceByPidReq) returns (response); //*********************产品详情分解接口结束*********************** } @@ -317,4 +320,8 @@ type GetRenderDesignRsp { //获取产品模型信息 type GetModelByPidReq { Pid string `form:"pid"` //实际上是产品sn +} +//获取产品阶梯价格 +type GetPriceByPidReq { + Pid string `form:"pid"` } \ No newline at end of file