fix
This commit is contained in:
parent
0db42491ae
commit
edd3d7353d
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -72,6 +72,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
|
|||||||
Path: "/api/product/get_model_by_pid",
|
Path: "/api/product/get_model_by_pid",
|
||||||
Handler: GetModelByPidHandler(serverCtx),
|
Handler: GetModelByPidHandler(serverCtx),
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
Method: http.MethodGet,
|
||||||
|
Path: "/api/product/get_price_by_pid",
|
||||||
|
Handler: GetPriceByPidHandler(serverCtx),
|
||||||
|
},
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
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)
|
||||||
|
}
|
@ -297,6 +297,10 @@ type GetModelByPidReq struct {
|
|||||||
Pid string `form:"pid"` //实际上是产品sn
|
Pid string `form:"pid"` //实际上是产品sn
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type GetPriceByPidReq struct {
|
||||||
|
Pid string `form:"pid"`
|
||||||
|
}
|
||||||
|
|
||||||
type Request struct {
|
type Request struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -47,6 +47,9 @@ service product {
|
|||||||
//获取产品模型信息
|
//获取产品模型信息
|
||||||
@handler GetModelByPidHandler
|
@handler GetModelByPidHandler
|
||||||
get /api/product/get_model_by_pid(GetModelByPidReq) returns (response);
|
get /api/product/get_model_by_pid(GetModelByPidReq) returns (response);
|
||||||
|
//获取产品阶梯价格列表
|
||||||
|
@handler GetPriceByPidHandler
|
||||||
|
get /api/product/get_price_by_pid(GetPriceByPidReq) returns (response);
|
||||||
//*********************产品详情分解接口结束***********************
|
//*********************产品详情分解接口结束***********************
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -318,3 +321,7 @@ type GetRenderDesignRsp {
|
|||||||
type GetModelByPidReq {
|
type GetModelByPidReq {
|
||||||
Pid string `form:"pid"` //实际上是产品sn
|
Pid string `form:"pid"` //实际上是产品sn
|
||||||
}
|
}
|
||||||
|
//获取产品阶梯价格
|
||||||
|
type GetPriceByPidReq {
|
||||||
|
Pid string `form:"pid"`
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user